|
| 1 | +import * as React from 'react'; |
| 2 | +import { TransitionHookFn } from '@uirouter/core'; |
| 3 | +import { makeTestRouter } from '../../__tests__/util'; |
| 4 | +import { UIView } from '../../components'; |
| 5 | +import { ReactStateDeclaration } from '../../interface'; |
| 6 | +import { useCanExit } from '../useCanExit'; |
| 7 | + |
| 8 | +const state1 = { name: 'state1', url: '/state1', component: UIView }; |
| 9 | + |
| 10 | +function TestComponent({ callback }: { callback: TransitionHookFn }) { |
| 11 | + useCanExit(callback); |
| 12 | + return <div />; |
| 13 | +} |
| 14 | + |
| 15 | +describe('useCanExit', () => { |
| 16 | + let { router, routerGo, mountInRouter } = makeTestRouter([]); |
| 17 | + beforeEach(() => ({ router, routerGo, mountInRouter } = makeTestRouter([state1]))); |
| 18 | + |
| 19 | + async function registerAndGo(state: ReactStateDeclaration) { |
| 20 | + router.stateRegistry.register(state); |
| 21 | + await routerGo(state); |
| 22 | + } |
| 23 | + |
| 24 | + it('can block a transition that exits the state it was used in', async () => { |
| 25 | + const callback = jest.fn(() => false); |
| 26 | + await registerAndGo({ name: 'state2', component: () => <TestComponent callback={callback} /> }); |
| 27 | + mountInRouter(<UIView />); |
| 28 | + expect(routerGo('state1')).rejects.toMatchObject({ |
| 29 | + message: 'The transition has been aborted', |
| 30 | + }); |
| 31 | + expect(callback).toHaveBeenCalled(); |
| 32 | + expect(router.globals.current.name).toBe('state2'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('can block a transition using a Promise that resolves to false', async () => { |
| 36 | + const callback = jest.fn(() => Promise.resolve(false)); |
| 37 | + await registerAndGo({ name: 'state2', component: () => <TestComponent callback={callback} /> }); |
| 38 | + mountInRouter(<UIView />); |
| 39 | + expect(routerGo('state1')).rejects.toMatchObject({ |
| 40 | + message: 'The transition has been aborted', |
| 41 | + }); |
| 42 | + expect(callback).toHaveBeenCalled(); |
| 43 | + expect(router.globals.current.name).toBe('state2'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('can allow a transition using a Promise that resolves to true', async () => { |
| 47 | + const callback = jest.fn(() => Promise.resolve(true)); |
| 48 | + await registerAndGo({ name: 'state2', component: () => <TestComponent callback={callback} /> }); |
| 49 | + mountInRouter(<UIView />); |
| 50 | + await routerGo('state1'); |
| 51 | + expect(router.globals.current.name).toBe('state1'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('can allow a transition using a Promise that resolves to undefined', async () => { |
| 55 | + const callback = jest.fn(() => Promise.resolve(undefined)); |
| 56 | + await registerAndGo({ name: 'state2', component: () => <TestComponent callback={callback} /> }); |
| 57 | + mountInRouter(<UIView />); |
| 58 | + await routerGo('state1'); |
| 59 | + expect(router.globals.current.name).toBe('state1'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('can allow a transition that exits the state it was used in', async () => { |
| 63 | + const callback = jest.fn(() => true); |
| 64 | + await registerAndGo({ name: 'state2', component: () => <TestComponent callback={callback} /> }); |
| 65 | + mountInRouter(<UIView />); |
| 66 | + await routerGo('state1'); |
| 67 | + expect(callback).toHaveBeenCalled(); |
| 68 | + expect(router.globals.current.name).toBe('state1'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('can block a transition that goes to the parent state', async () => { |
| 72 | + const callback = jest.fn(() => false); |
| 73 | + await registerAndGo({ name: 'state1.child', component: () => <TestComponent callback={callback} /> }); |
| 74 | + mountInRouter(<UIView />); |
| 75 | + expect(routerGo('state1')).rejects.toMatchObject({ |
| 76 | + message: 'The transition has been aborted', |
| 77 | + }); |
| 78 | + expect(callback).toHaveBeenCalled(); |
| 79 | + expect(router.globals.current.name).toBe('state1.child'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('does not block a transition which retains (does not exit) the state the hook was used in', async () => { |
| 83 | + const callback = jest.fn(() => false); |
| 84 | + const state2Component = () => ( |
| 85 | + <> |
| 86 | + <TestComponent callback={callback} /> |
| 87 | + <UIView /> |
| 88 | + </> |
| 89 | + ); |
| 90 | + |
| 91 | + // hook used in state2 |
| 92 | + router.stateRegistry.register({ name: 'state2', component: state2Component } as ReactStateDeclaration); |
| 93 | + router.stateRegistry.register({ name: 'state2.child', component: () => <div /> } as ReactStateDeclaration); |
| 94 | + |
| 95 | + await routerGo('state2.child'); |
| 96 | + mountInRouter(<UIView />); |
| 97 | + // exit state2.child but retain state2 |
| 98 | + await routerGo('state2'); |
| 99 | + |
| 100 | + expect(callback).not.toHaveBeenCalled(); |
| 101 | + expect(router.globals.current.name).toBe('state2'); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('implementation detail', () => { |
| 105 | + it('registers an onBefore transition hook', async () => { |
| 106 | + const callback = () => true; |
| 107 | + await registerAndGo({ name: 'state2', component: () => <TestComponent callback={callback} /> }); |
| 108 | + const spy = jest.spyOn(router.transitionService, 'onBefore'); |
| 109 | + mountInRouter(<UIView />); |
| 110 | + expect(spy).toHaveBeenCalledTimes(1); |
| 111 | + expect(spy).toHaveBeenCalledWith({ exiting: 'state2' }, expect.any(Function), undefined); |
| 112 | + }); |
| 113 | + |
| 114 | + it('deregisters the onBefore transition hook when unmounted', async () => { |
| 115 | + const deregisterSpy = jest.fn(); |
| 116 | + await registerAndGo({ name: 'state2', component: () => <TestComponent callback={() => true} /> }); |
| 117 | + jest.spyOn(router.transitionService, 'onBefore').mockImplementation(() => deregisterSpy); |
| 118 | + mountInRouter(<UIView />); |
| 119 | + expect(deregisterSpy).toHaveBeenCalledTimes(0); |
| 120 | + |
| 121 | + await routerGo('state1'); |
| 122 | + expect(router.globals.current.name).toBe('state1'); |
| 123 | + expect(deregisterSpy).toHaveBeenCalledTimes(1); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments