|
| 1 | +/* eslint-disable ember/no-private-routing-service */ |
| 2 | +import { settled } from '@ember/test-helpers'; |
| 3 | + |
| 4 | +import type { Router } from '@ember/routing'; |
| 5 | +import type { TestContext } from 'ember-test-helpers'; |
| 6 | + |
| 7 | +type MapFunction = Parameters<typeof Router['map']>[0]; |
| 8 | + |
| 9 | +interface SetupRouterOptions { |
| 10 | + active?: string | string[]; |
| 11 | + map?: MapFunction; |
| 12 | + rootURL?: string; |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +const noop = () => {}; |
| 17 | + |
| 18 | +/** |
| 19 | + * A test helper to define a new router map in the context of a test. |
| 20 | + * |
| 21 | + * Useful for when an integration test may need to interact with the router service, |
| 22 | + * but since you're only rendering a component, routing isn't enabled (pre Ember 3.25). |
| 23 | + * |
| 24 | + * Also useful for testing custom link components. |
| 25 | + * |
| 26 | + * @example |
| 27 | + * |
| 28 | + * import { setupRouter } from '@crowdstrike/test-helpers'; |
| 29 | + * |
| 30 | + * module('tests that need a router', function(hooks) { |
| 31 | + * setupRouter(hooks, { |
| 32 | + * active: ['some-route-path.foo', 2], |
| 33 | + * map: function() { |
| 34 | + * this.route('some-route-path', function() { |
| 35 | + * this.route('hi'); |
| 36 | + * this.route('foo', { path: ':dynamic_segment' }); |
| 37 | + * }); |
| 38 | + * }, |
| 39 | + * }); |
| 40 | + * }) |
| 41 | + * |
| 42 | + * |
| 43 | + * @param {NestedHooks} hooks |
| 44 | + * @param {Object} configuration - router configuration, as it would be defined in router.js |
| 45 | + * @param {Array} [configuration.active] - route segments that make up the active route |
| 46 | + * @param {Function} configuration.map - the router map |
| 47 | + * @param {string} [configuration.rootURL] - the root URL of the application |
| 48 | + */ |
| 49 | +export function setupRouter( |
| 50 | + hooks: NestedHooks, |
| 51 | + { active, map = noop, rootURL = '/' }: SetupRouterOptions = {} |
| 52 | +) { |
| 53 | + let originalMaps: unknown[] = []; |
| 54 | + |
| 55 | + hooks.beforeEach(async function (this: TestContext) { |
| 56 | + const router = this.owner.resolveRegistration('router:main'); |
| 57 | + |
| 58 | + router.rootURL = rootURL; |
| 59 | + originalMaps = router.dslCallbacks; |
| 60 | + router.dslCallbacks = []; |
| 61 | + |
| 62 | + router.map(map); |
| 63 | + this.owner.lookup('router:main').setupRouter(); |
| 64 | + |
| 65 | + if (active) { |
| 66 | + const routerService = this.owner.lookup('service:router'); |
| 67 | + |
| 68 | + routerService.transitionTo(...ensureArray(active)); |
| 69 | + await settled(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + hooks.afterEach(function (this: TestContext) { |
| 74 | + const router = this.owner.resolveRegistration('router:main'); |
| 75 | + |
| 76 | + router.dslCallbacks = originalMaps; |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * For setting up the currently configured router in your app |
| 82 | + * |
| 83 | + */ |
| 84 | +export function setupAppRouter(hooks: NestedHooks) { |
| 85 | + hooks.beforeEach(function (this: TestContext) { |
| 86 | + this.owner.lookup('router:main').setupRouter(); |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +export function ensureArray<T>(maybeArray?: T | T[]): T[] { |
| 91 | + if (Array.isArray(maybeArray)) { |
| 92 | + return maybeArray; |
| 93 | + } |
| 94 | + |
| 95 | + if (!maybeArray) { |
| 96 | + return []; |
| 97 | + } |
| 98 | + |
| 99 | + return [maybeArray]; |
| 100 | +} |
0 commit comments