-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add failing test for lazy engine
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
|