From ce07c01882ee7f8cf72359b324a548f3e4b3d99e Mon Sep 17 00:00:00 2001 From: Trent Willis Date: Wed, 24 Aug 2016 20:01:09 -0700 Subject: [PATCH] Introduce preload-assets helper for testing --- README.md | 13 +++++++++++++ addon-test-support/preload-assets.js | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 addon-test-support/preload-assets.js diff --git a/README.md b/README.md index 6ff9fb7..42b6c63 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,19 @@ var app = new EmberApp(defaults, { }); ``` +## Pre-loading Assets During Testing + +For test environments it is often useful to load all of the assets in a manifest upfront. You can do this by using the +`preloadAssets` helper, like so: + +```js +// tests/test-helper.js +import preloadAssets from 'ember-asset-loader/test-support/preload-assets'; +import manifest from 'app/asset-manifest'; + +preloadAssets(manifest); +``` + ## Installation * `git clone https://github.com/trentmwillis/ember-asset-loader` diff --git a/addon-test-support/preload-assets.js b/addon-test-support/preload-assets.js new file mode 100644 index 0000000..762cddd --- /dev/null +++ b/addon-test-support/preload-assets.js @@ -0,0 +1,23 @@ +import Ember from 'ember'; +import AssetLoader from 'ember-asset-loader/services/asset-loader'; + +const { Test, RSVP } = Ember; + +/** + * Preloads all the bundles specified in an asset manifest + * to make sure all files are available for testing. + * + * Uses the Ember.Test.Promise class to make sure tests + * wait for the assets to load first. + * + * @return {Promise} + */ +export default function preloadAssets(manifest) { + const loader = AssetLoader.create(); + loader.pushManifest(manifest); + + const bundlePromises = Object.keys(manifest.bundles).map((bundle) => loader.loadBundle(bundle)); + const allBundles = RSVP.all(bundlePromises); + + return Test.resolve(allBundles); +}