|
| 1 | +import Router, { Route, Transition } from 'router'; |
| 2 | +import { Dict } from 'router/core'; |
| 3 | +import { createHandler, TestRouter } from './test_helpers'; |
| 4 | + |
| 5 | +function sleep(ms: number) { |
| 6 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 7 | +} |
| 8 | + |
| 9 | +QUnit.module('native async', function (hooks) { |
| 10 | + let router: Router<Route>; |
| 11 | + let url: string | undefined; |
| 12 | + let routes: Dict<Route>; |
| 13 | + |
| 14 | + class LocalRouter extends TestRouter { |
| 15 | + routeDidChange() {} |
| 16 | + routeWillChange() {} |
| 17 | + didTransition() {} |
| 18 | + willTransition() {} |
| 19 | + |
| 20 | + getRoute(name: string) { |
| 21 | + if (routes[name] === undefined) { |
| 22 | + routes[name] = createHandler('empty'); |
| 23 | + } |
| 24 | + |
| 25 | + return routes[name]; |
| 26 | + } |
| 27 | + |
| 28 | + getSerializer(_name: string) { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + |
| 32 | + replaceURL(name: string) { |
| 33 | + this.updateURL(name); |
| 34 | + } |
| 35 | + updateURL(newUrl: string) { |
| 36 | + url = newUrl; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + hooks.beforeEach(() => { |
| 41 | + router = new LocalRouter(); |
| 42 | + }); |
| 43 | + |
| 44 | + QUnit.test('returning a transition does not reject with TransitionAborted', async function ( |
| 45 | + assert |
| 46 | + ) { |
| 47 | + assert.expect(3); |
| 48 | + |
| 49 | + router.map(function (match) { |
| 50 | + match('/').to('application', function (match) { |
| 51 | + match('/').to('index'); |
| 52 | + match('/about').to('about'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + routes = { |
| 57 | + index: createHandler('index', { |
| 58 | + beforeModel(_params: Dict<unknown>, _transition: Transition) { |
| 59 | + assert.step('index beforeModel'); |
| 60 | + |
| 61 | + return router.transitionTo('/about'); |
| 62 | + }, |
| 63 | + }), |
| 64 | + |
| 65 | + about: createHandler('about', { |
| 66 | + setup() { |
| 67 | + assert.step('about setup'); |
| 68 | + }, |
| 69 | + }), |
| 70 | + }; |
| 71 | + |
| 72 | + await router.handleURL('/'); |
| 73 | + |
| 74 | + assert.equal(url, '/about', 'ended on /about'); |
| 75 | + |
| 76 | + assert.verifySteps(['index beforeModel', 'about setup']); |
| 77 | + }); |
| 78 | + |
| 79 | + QUnit.test( |
| 80 | + 'returning a promise that resolves to a transition (which resolves) does not reject', |
| 81 | + async function (assert) { |
| 82 | + assert.expect(1); |
| 83 | + |
| 84 | + router.map(function (match) { |
| 85 | + match('/').to('application', function (match) { |
| 86 | + match('/').to('index'); |
| 87 | + match('/about').to('about'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + routes = { |
| 92 | + index: createHandler('index', { |
| 93 | + async beforeModel(_params: Dict<unknown>, _transition: Transition) { |
| 94 | + await sleep(5); |
| 95 | + |
| 96 | + return router.transitionTo('/about'); |
| 97 | + }, |
| 98 | + }), |
| 99 | + |
| 100 | + about: createHandler('about', { |
| 101 | + setup: function () { |
| 102 | + assert.ok(true, 'setup was entered'); |
| 103 | + }, |
| 104 | + }), |
| 105 | + }; |
| 106 | + |
| 107 | + return router.handleURL('/'); |
| 108 | + } |
| 109 | + ); |
| 110 | +}); |
0 commit comments