From a4eaa84f5aad509d1f875cb7717d0d8c67dc2502 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Wed, 9 Sep 2020 12:22:57 -0400 Subject: [PATCH] Failing test for `TransitionAborted` via `router.transitionTo` within async hook. --- tests/index.ts | 1 + tests/native-async-test.ts | 110 +++++++++++++++++++++++++++++++++++++ tests/test_helpers.ts | 2 +- 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tests/native-async-test.ts diff --git a/tests/index.ts b/tests/index.ts index 2a032d8e..731f704b 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -7,3 +7,4 @@ import './transition_intent_test'; import './transition_state_test'; import './unrecognized-url-error_test'; import './utils_test'; +import './native-async-test'; diff --git a/tests/native-async-test.ts b/tests/native-async-test.ts new file mode 100644 index 00000000..d0d69dc7 --- /dev/null +++ b/tests/native-async-test.ts @@ -0,0 +1,110 @@ +import Router, { Route, Transition } from 'router'; +import { Dict } from 'router/core'; +import { createHandler, TestRouter } from './test_helpers'; + +function sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +QUnit.module('native async', function (hooks) { + let router: Router; + let url: string | undefined; + let routes: Dict; + + class LocalRouter extends TestRouter { + routeDidChange() {} + routeWillChange() {} + didTransition() {} + willTransition() {} + + getRoute(name: string) { + if (routes[name] === undefined) { + routes[name] = createHandler('empty'); + } + + return routes[name]; + } + + getSerializer(_name: string) { + return undefined; + } + + replaceURL(name: string) { + this.updateURL(name); + } + updateURL(newUrl: string) { + url = newUrl; + } + } + + hooks.beforeEach(() => { + router = new LocalRouter(); + }); + + QUnit.test('returning a transition does not reject with TransitionAborted', async function ( + assert + ) { + assert.expect(3); + + router.map(function (match) { + match('/').to('application', function (match) { + match('/').to('index'); + match('/about').to('about'); + }); + }); + + routes = { + index: createHandler('index', { + beforeModel(_params: Dict, _transition: Transition) { + assert.step('index beforeModel'); + + return router.transitionTo('/about'); + }, + }), + + about: createHandler('about', { + setup() { + assert.step('about setup'); + }, + }), + }; + + await router.handleURL('/'); + + assert.equal(url, '/about', 'ended on /about'); + + assert.verifySteps(['index beforeModel', 'about setup']); + }); + + QUnit.test( + 'returning a promise that resolves to a transition (which resolves) does not reject', + async function (assert) { + assert.expect(1); + + router.map(function (match) { + match('/').to('application', function (match) { + match('/').to('index'); + match('/about').to('about'); + }); + }); + + routes = { + index: createHandler('index', { + async beforeModel(_params: Dict, _transition: Transition) { + await sleep(5); + + return router.transitionTo('/about'); + }, + }), + + about: createHandler('about', { + setup: function () { + assert.ok(true, 'setup was entered'); + }, + }), + }; + + return router.handleURL('/'); + } + ); +}); diff --git a/tests/test_helpers.ts b/tests/test_helpers.ts index 29922b87..7d9376a7 100644 --- a/tests/test_helpers.ts +++ b/tests/test_helpers.ts @@ -57,7 +57,7 @@ function transitionTo( path: string | { queryParams: Dict }, ...context: any[] ) { - let result = router.transitionTo.apply(router, [path, ...context]); + let result = router.transitionTo(path, ...context); flushBackburner(); return result; }