Skip to content

Commit

Permalink
test: add failing test for lazy engine
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Nov 13, 2019
1 parent c338d79 commit de82c16
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/acceptance/engine-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit, currentURL } from '@ember/test-helpers';
import Route from '@ember/routing/route';
import Engine from '@ember/engine';
import Controller from '@ember/controller';
import hbs from 'htmlbars-inline-precompile';
import AppRouter from 'dummy/router';
import setupRouterForEngine from '../helpers/setup-router-for-engine';

module('lazy engine loading', function(hooks) {
setupApplicationTest(hooks);
setupRouterForEngine(hooks);

hooks.beforeEach(function(assert) {
this.owner.register(
'route:application',
Route.extend({
prefetch() {
assert.step('application');
},
})
);
this.owner.register('template:application', hbs`{{outlet}}`);
this.owner.register('controller:application', Controller.extend());
debugger;
AppRouter.map(function() {
this.mount('blog');
});
this.owner.register('route-map:blog', function() {
this.route('post');
});
this.owner.register('engine:blog', Engine.extend({
init() {
this._super(...arguments);
this.register('template:application', hbs`{{outlet}}`);
this.register('controller:application', Controller.extend());

this.register('template:post', hbs`Post {{this.model}}`);
this.register('route:application', Route.extend({
prefetch() {
assert.step('blog-application');
}
}));
this.register('route:post', Route.extend({
prefetch() {
assert.step('blog-post');
}
}));
}
}))
});

test('prefetch hook on lazy engine is called', async function(assert) {
await visit('/blog/post');
assert.verifySteps(['application', 'blog-application', 'blog-post']);
assert.equal(currentURL(), '/blog/post');
});
});

61 changes: 61 additions & 0 deletions tests/helpers/setup-router-for-engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import RSVP from 'rsvp';

/**
* Modifies the Router to return a Promise from it's `getHandler` method. This
* is used to simulate what happens when loading a lazy Engine.
*/
export default function setupRouterForEngine(hooks) {
let getRoute;
hooks.beforeEach(function() {
let router = this.owner.lookup('router:main');
router.reopen({
setupRouter() {
this._super(...arguments);

getRoute = this._routerMicrolib.getRoute;
this._enginePromises = Object.create(null);
this._resolvedEngines = Object.create(null);

this._routerMicrolib.getRoute = name => {
let engineInfo = this._engineInfoByRoute[name];
if (!engineInfo) {
return getRoute(name);
}

let engineName = engineInfo.name;
if (this._resolvedEngines[engineName]) {
return getRoute(name);
}

let enginePromise = this._enginePromises[engineName];

if (!enginePromise) {
enginePromise = new RSVP.Promise(resolve => {
setTimeout(() => {
this._resolvedEngines[engineName] = true;

resolve();
}, 1);
});
this._enginePromises[engineName] = enginePromise;
}

return enginePromise.then(() => getRoute(name));
};
}
});

/**
* This override is copied from what ember-engines does. It allows handlers to
* be loaded asynchronously by not checking the handler directly for meta info
*/
router._getQPMeta = function _newGetQPMeta(handlerInfo) {
return this._bucketCache.lookup('route-meta', handlerInfo.name);
};
});

hooks.afterEach(function() {
this.owner.lookup('router:main')._routerMicrolib.getRoute = getRoute;
});
}

0 comments on commit de82c16

Please sign in to comment.