From 23edff5e1f30dcdd0c2f1606351344c58d13df1b Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Fri, 2 Dec 2016 16:33:27 -0800 Subject: [PATCH 1/2] Set up failing test case --- tests/acceptance/run-loop-test.js | 30 +++++++++++++++++++ tests/dummy/app/router.js | 1 + tests/dummy/app/routes/application.js | 16 ++++++++++ tests/dummy/app/templates/application.hbs | 2 +- tests/dummy/app/templates/index.hbs | 3 ++ .../dummy/app/templates/some-other-route.hbs | 2 ++ 6 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/acceptance/run-loop-test.js create mode 100644 tests/dummy/app/routes/application.js create mode 100644 tests/dummy/app/templates/index.hbs create mode 100644 tests/dummy/app/templates/some-other-route.hbs diff --git a/tests/acceptance/run-loop-test.js b/tests/acceptance/run-loop-test.js new file mode 100644 index 0000000..81381e1 --- /dev/null +++ b/tests/acceptance/run-loop-test.js @@ -0,0 +1,30 @@ +import { test } from 'qunit'; +import moduleForAcceptance from '../../tests/helpers/module-for-acceptance'; + +import nprogress from 'ember-cli-nprogress'; + +// Note: This somewhat unorthidox test setup will throw an error when running the tests in Chrome, +// if `nprogress` isn't configured to run within the Ember run loop +moduleForAcceptance('Acceptance | run loop', { + beforeEach() { + nprogress.configure({ + parent: '#nprogress-parent' + }); + } +}); + +test('loading an initial route', function(assert) { + assert.expect(0); + + visit('/'); +}); + +test('it fires the `start` event within the run loop', function(assert) { + visit('/'); + + click('a'); + + andThen(function() { + assert.equal(currentURL(), '/some-other-route'); + }); +}); diff --git a/tests/dummy/app/router.js b/tests/dummy/app/router.js index cdc2578..26d757a 100644 --- a/tests/dummy/app/router.js +++ b/tests/dummy/app/router.js @@ -7,6 +7,7 @@ const Router = Ember.Router.extend({ }); Router.map(function() { + this.route('some-other-route'); }); export default Router; diff --git a/tests/dummy/app/routes/application.js b/tests/dummy/app/routes/application.js new file mode 100644 index 0000000..fd9d00f --- /dev/null +++ b/tests/dummy/app/routes/application.js @@ -0,0 +1,16 @@ +import Ember from 'ember'; +import nprogress from 'ember-cli-nprogress'; + +const { Route } = Ember; + +export default Route.extend({ + actions: { + willTransition() { + nprogress.start(); + }, + + didTransition() { + nprogress.done(); + } + } +}); diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index f8bc38e..1a41fa0 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -1,3 +1,3 @@ -

Welcome to Ember

+
{{outlet}} diff --git a/tests/dummy/app/templates/index.hbs b/tests/dummy/app/templates/index.hbs new file mode 100644 index 0000000..1be8b17 --- /dev/null +++ b/tests/dummy/app/templates/index.hbs @@ -0,0 +1,3 @@ +

Hello, world!

+ +{{link-to 'Some other route' 'some-other-route'}} diff --git a/tests/dummy/app/templates/some-other-route.hbs b/tests/dummy/app/templates/some-other-route.hbs new file mode 100644 index 0000000..557c7ab --- /dev/null +++ b/tests/dummy/app/templates/some-other-route.hbs @@ -0,0 +1,2 @@ +

Some Other Route

+ From 20d68b8809ccbdc7238bc1ad53edb74c4374c797 Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Fri, 2 Dec 2016 17:36:52 -0800 Subject: [PATCH 2/2] =?UTF-8?q?Make=20NProgress=20play=20nicely=20with=20E?= =?UTF-8?q?mber=E2=80=99s=20run=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addon/initializers/nprogress.js | 15 +++++++++++++++ app/initializers/nprogress.js | 1 + blueprints/ember-cli-nprogress/index.js | 2 +- bower.json | 2 +- tests/acceptance/run-loop-test.js | 3 +++ tests/unit/initializers/nprogress-test.js | 23 +++++++++++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 addon/initializers/nprogress.js create mode 100644 app/initializers/nprogress.js create mode 100644 tests/unit/initializers/nprogress-test.js diff --git a/addon/initializers/nprogress.js b/addon/initializers/nprogress.js new file mode 100644 index 0000000..4965acc --- /dev/null +++ b/addon/initializers/nprogress.js @@ -0,0 +1,15 @@ +import Ember from 'ember'; +import nprogress from 'ember-cli-nprogress'; + +const { run } = Ember; + +export const scheduler = run.later.bind(undefined, undefined); + +export function initialize() { + nprogress.configure({ scheduler }); +} + +export default { + name: 'nprogress', + initialize +}; diff --git a/app/initializers/nprogress.js b/app/initializers/nprogress.js new file mode 100644 index 0000000..f413475 --- /dev/null +++ b/app/initializers/nprogress.js @@ -0,0 +1 @@ +export { default, initialize } from 'ember-cli-nprogress/initializers/nprogress'; diff --git a/blueprints/ember-cli-nprogress/index.js b/blueprints/ember-cli-nprogress/index.js index 8a5b589..a0d0352 100644 --- a/blueprints/ember-cli-nprogress/index.js +++ b/blueprints/ember-cli-nprogress/index.js @@ -2,6 +2,6 @@ module.exports = { normalizeEntityName: function() {}, // no-op since we're just adding dependencies afterInstall: function() { - return this.addBowerPackageToProject('nprogress', '^0.2.0'); + return this.addBowerPackageToProject('nprogress', 'alexlafroscia/nprogress#allow-alternate-scheduler'); } }; diff --git a/bower.json b/bower.json index 1e620ee..409021a 100644 --- a/bower.json +++ b/bower.json @@ -3,6 +3,6 @@ "dependencies": { "ember": "~2.10.0", "ember-cli-shims": "0.1.3", - "nprogress": "^0.2.0" + "nprogress": "alexlafroscia/nprogress#allow-alternate-scheduler" } } diff --git a/tests/acceptance/run-loop-test.js b/tests/acceptance/run-loop-test.js index 81381e1..8979d80 100644 --- a/tests/acceptance/run-loop-test.js +++ b/tests/acceptance/run-loop-test.js @@ -13,12 +13,15 @@ moduleForAcceptance('Acceptance | run loop', { } }); +// This is required to get the app set up test('loading an initial route', function(assert) { assert.expect(0); visit('/'); }); +// This test just verifies that an error is not produced in cases where `nprogress` +// is running while the app is being torn down test('it fires the `start` event within the run loop', function(assert) { visit('/'); diff --git a/tests/unit/initializers/nprogress-test.js b/tests/unit/initializers/nprogress-test.js new file mode 100644 index 0000000..664fb12 --- /dev/null +++ b/tests/unit/initializers/nprogress-test.js @@ -0,0 +1,23 @@ +import Ember from 'ember'; +import { module, test } from 'qunit'; + +import NprogressInitializer from 'dummy/initializers/nprogress'; +import { scheduler } from 'ember-cli-nprogress/initializers/nprogress'; +import nprogress from 'ember-cli-nprogress'; + +let application; + +module('Unit | Initializer | nprogress', { + beforeEach() { + Ember.run(function() { + application = Ember.Application.create(); + application.deferReadiness(); + }); + } +}); + +test('it sets up an Ember-friendly scheduler function', function(assert) { + NprogressInitializer.initialize(application); + + assert.equal(nprogress.settings.scheduler, scheduler); +});