Skip to content

Commit 5ea1d83

Browse files
Robert Jacksonstefanpenner
Robert Jackson
authored andcommitted
Failing test for TransitionAborted via router.transitionTo within async hook.
1 parent 912b2b0 commit 5ea1d83

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

tests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ import './transition_intent_test';
77
import './transition_state_test';
88
import './unrecognized-url-error_test';
99
import './utils_test';
10+
import './native-async-test';

tests/native-async-test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
});

tests/test_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function transitionTo(
5757
path: string | { queryParams: Dict<unknown> },
5858
...context: any[]
5959
) {
60-
let result = router.transitionTo.apply(router, [path, ...context]);
60+
let result = router.transitionTo(path, ...context);
6161
flushBackburner();
6262
return result;
6363
}

0 commit comments

Comments
 (0)