|
| 1 | +import historyRouter from '../history'; |
| 2 | + |
| 3 | +const wait = (ms = 0) => new Promise(res => setTimeout(res, ms)); |
| 4 | + |
| 5 | +describe('life cycle', () => { |
| 6 | + beforeEach(() => { |
| 7 | + window.history.pushState(null, '-- divider --', 'http://localhost/'); |
| 8 | + jest.restoreAllMocks(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('does not write the same url twice', async () => { |
| 12 | + const pushState = jest.spyOn(window.history, 'pushState'); |
| 13 | + const router = historyRouter({ |
| 14 | + writeDelay: 0, |
| 15 | + }); |
| 16 | + |
| 17 | + router.write({ some: 'state' }); |
| 18 | + await wait(0); |
| 19 | + |
| 20 | + router.write({ some: 'state' }); |
| 21 | + await wait(0); |
| 22 | + |
| 23 | + router.write({ some: 'state' }); |
| 24 | + await wait(0); |
| 25 | + |
| 26 | + expect(pushState).toHaveBeenCalledTimes(1); |
| 27 | + expect(pushState.mock.calls).toMatchInlineSnapshot(` |
| 28 | + Array [ |
| 29 | + Array [ |
| 30 | + Object { |
| 31 | + "some": "state", |
| 32 | + }, |
| 33 | + "", |
| 34 | + "http://localhost/?some=state", |
| 35 | + ], |
| 36 | + ] |
| 37 | + `); |
| 38 | + }); |
| 39 | + |
| 40 | + it('does not write if already externally updated to desired URL', async () => { |
| 41 | + const pushState = jest.spyOn(window.history, 'pushState'); |
| 42 | + const router = historyRouter({ |
| 43 | + writeDelay: 0, |
| 44 | + }); |
| 45 | + |
| 46 | + const fakeState = { identifier: 'fake state' }; |
| 47 | + |
| 48 | + router.write({ some: 'state one' }); |
| 49 | + |
| 50 | + // external update before timeout passes |
| 51 | + window.history.pushState( |
| 52 | + fakeState, |
| 53 | + '', |
| 54 | + 'http://localhost/?some=state%20two' |
| 55 | + ); |
| 56 | + |
| 57 | + // this write isn't needed anymore |
| 58 | + router.write({ some: 'state two' }); |
| 59 | + await wait(0); |
| 60 | + |
| 61 | + expect(pushState).toHaveBeenCalledTimes(1); |
| 62 | + expect(pushState.mock.calls).toMatchInlineSnapshot(` |
| 63 | + Array [ |
| 64 | + Array [ |
| 65 | + Object { |
| 66 | + "identifier": "fake state", |
| 67 | + }, |
| 68 | + "", |
| 69 | + "http://localhost/?some=state%20two", |
| 70 | + ], |
| 71 | + ] |
| 72 | + `); |
| 73 | + |
| 74 | + // proves that InstantSearch' write did not happen |
| 75 | + expect(history.state).toBe(fakeState); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not write the same url title twice', async () => { |
| 79 | + const title = jest.spyOn(window.document, 'title', 'set'); |
| 80 | + const pushState = jest.spyOn(window.history, 'pushState'); |
| 81 | + |
| 82 | + const router = historyRouter({ |
| 83 | + writeDelay: 0, |
| 84 | + windowTitle: state => `My Site - ${state.some}`, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(title).toHaveBeenCalledTimes(1); |
| 88 | + expect(window.document.title).toBe('My Site - undefined'); |
| 89 | + |
| 90 | + router.write({ some: 'state' }); |
| 91 | + await wait(0); |
| 92 | + |
| 93 | + router.write({ some: 'state' }); |
| 94 | + await wait(0); |
| 95 | + |
| 96 | + router.write({ some: 'state' }); |
| 97 | + await wait(0); |
| 98 | + |
| 99 | + expect(pushState).toHaveBeenCalledTimes(1); |
| 100 | + expect(pushState.mock.calls).toMatchInlineSnapshot(` |
| 101 | + Array [ |
| 102 | + Array [ |
| 103 | + Object { |
| 104 | + "some": "state", |
| 105 | + }, |
| 106 | + "My Site - state", |
| 107 | + "http://localhost/?some=state", |
| 108 | + ], |
| 109 | + ] |
| 110 | + `); |
| 111 | + |
| 112 | + expect(title).toHaveBeenCalledTimes(2); |
| 113 | + expect(window.document.title).toBe('My Site - state'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('writes after timeout is done', async () => { |
| 117 | + const pushState = jest.spyOn(window.history, 'pushState'); |
| 118 | + |
| 119 | + const router = historyRouter({ |
| 120 | + writeDelay: 0, |
| 121 | + }); |
| 122 | + |
| 123 | + router.write({ some: 'state' }); |
| 124 | + router.write({ some: 'second' }); |
| 125 | + router.write({ some: 'third' }); |
| 126 | + await wait(0); |
| 127 | + |
| 128 | + expect(pushState).toHaveBeenCalledTimes(1); |
| 129 | + expect(pushState.mock.calls).toMatchInlineSnapshot(` |
| 130 | + Array [ |
| 131 | + Array [ |
| 132 | + Object { |
| 133 | + "some": "third", |
| 134 | + }, |
| 135 | + "", |
| 136 | + "http://localhost/?some=third", |
| 137 | + ], |
| 138 | + ] |
| 139 | + `); |
| 140 | + }); |
| 141 | +}); |
0 commit comments