|
| 1 | +import type { FrameLocationTarget, FrameLocationWindow } from '../frame-location' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | +import { watchFrameLocation } from '../frame-location' |
| 4 | + |
| 5 | +type Listener = () => void |
| 6 | + |
| 7 | +interface FakeFrame { |
| 8 | + iframe: FrameLocationTarget |
| 9 | + /** Emit a same-document navigation the way a router would. */ |
| 10 | + pushState: (href: string) => void |
| 11 | + replaceState: (href: string) => void |
| 12 | + /** |
| 13 | + * Navigate without going through the frame's current `pushState` — a router |
| 14 | + * that captured the method before the watch attached, which no wrapper sees. |
| 15 | + */ |
| 16 | + pushStateBypassingWrapper: (href: string) => void |
| 17 | + /** Emit the Navigation API's post-commit notification. */ |
| 18 | + emitCurrentEntryChange: () => void |
| 19 | + /** Emit a browser-driven same-document navigation. */ |
| 20 | + emit: (type: 'popstate' | 'hashchange', href: string) => void |
| 21 | + /** Swap in a new document (new window, new `history`) and fire `load`. */ |
| 22 | + load: (href: string) => void |
| 23 | + /** Whether the frame's `history` methods are the ones it started with. */ |
| 24 | + isHistoryPristine: () => boolean |
| 25 | + /** Whether every listener the watcher attached has been removed. */ |
| 26 | + isDetached: () => boolean |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * A frame standing in for a same-origin iframe. `navigation: true` gives it the |
| 31 | + * Navigation API on top of `history`, the way a browser that ships it does. |
| 32 | + */ |
| 33 | +function fakeFrame(initialHref: string, options: { navigation?: boolean, crossOrigin?: boolean } = {}): FakeFrame { |
| 34 | + const loadListeners = new Set<Listener>() |
| 35 | + let win: FrameLocationWindow |
| 36 | + let href = initialHref |
| 37 | + let winListeners: Map<string, Set<Listener>> |
| 38 | + let navListeners: Set<Listener> |
| 39 | + let pristinePush: (...args: any[]) => void |
| 40 | + let pristineReplace: (...args: any[]) => void |
| 41 | + |
| 42 | + function createWindow(): void { |
| 43 | + const listeners = new Map<string, Set<Listener>>([['popstate', new Set()], ['hashchange', new Set()]]) |
| 44 | + const nav = new Set<Listener>() |
| 45 | + const history = { |
| 46 | + pushState: (...args: any[]) => { |
| 47 | + href = String(args[2]) |
| 48 | + }, |
| 49 | + replaceState: (...args: any[]) => { |
| 50 | + href = String(args[2]) |
| 51 | + }, |
| 52 | + } |
| 53 | + pristinePush = history.pushState |
| 54 | + pristineReplace = history.replaceState |
| 55 | + winListeners = listeners |
| 56 | + navListeners = nav |
| 57 | + win = { |
| 58 | + get location() { |
| 59 | + if (options.crossOrigin) |
| 60 | + throw new DOMException('cross-origin', 'SecurityError') |
| 61 | + return { |
| 62 | + get href() { |
| 63 | + return href |
| 64 | + }, |
| 65 | + } |
| 66 | + }, |
| 67 | + history, |
| 68 | + navigation: options.navigation |
| 69 | + ? { |
| 70 | + addEventListener: (_type, listener) => void nav.add(listener), |
| 71 | + removeEventListener: (_type, listener) => void nav.delete(listener), |
| 72 | + } |
| 73 | + : undefined, |
| 74 | + addEventListener: (type, listener) => void listeners.get(type)!.add(listener), |
| 75 | + removeEventListener: (type, listener) => void listeners.get(type)!.delete(listener), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + createWindow() |
| 80 | + |
| 81 | + const iframe: FrameLocationTarget = { |
| 82 | + get contentWindow() { |
| 83 | + return win |
| 84 | + }, |
| 85 | + addEventListener: (_type, listener) => void loadListeners.add(listener), |
| 86 | + removeEventListener: (_type, listener) => void loadListeners.delete(listener), |
| 87 | + } |
| 88 | + |
| 89 | + function emitCurrentEntryChange(): void { |
| 90 | + for (const listener of [...navListeners]) listener() |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + iframe, |
| 95 | + emitCurrentEntryChange, |
| 96 | + pushState: (next) => { |
| 97 | + win.history!.pushState({}, '', next) |
| 98 | + }, |
| 99 | + replaceState: (next) => { |
| 100 | + win.history!.replaceState({}, '', next) |
| 101 | + }, |
| 102 | + pushStateBypassingWrapper: (next) => { |
| 103 | + pristinePush({}, '', next) |
| 104 | + emitCurrentEntryChange() |
| 105 | + }, |
| 106 | + emit: (type, next) => { |
| 107 | + href = next |
| 108 | + for (const listener of [...winListeners.get(type)!]) listener() |
| 109 | + }, |
| 110 | + load: (next) => { |
| 111 | + createWindow() |
| 112 | + href = next |
| 113 | + for (const listener of [...loadListeners]) listener() |
| 114 | + }, |
| 115 | + isHistoryPristine: () => |
| 116 | + win.history!.pushState === pristinePush && win.history!.replaceState === pristineReplace, |
| 117 | + isDetached: () => |
| 118 | + loadListeners.size === 0 |
| 119 | + && navListeners.size === 0 |
| 120 | + && [...winListeners.values()].every(set => set.size === 0), |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +describe('watchFrameLocation', () => { |
| 125 | + it('reports pushState and replaceState by wrapping them, and restores them on dispose', () => { |
| 126 | + const frame = fakeFrame('http://localhost/app/') |
| 127 | + const onChange = vi.fn() |
| 128 | + const dispose = watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 129 | + |
| 130 | + expect(onChange).not.toHaveBeenCalled() |
| 131 | + expect(frame.isHistoryPristine()).toBe(false) |
| 132 | + |
| 133 | + frame.pushState('http://localhost/app/routes') |
| 134 | + frame.replaceState('http://localhost/app/routes?tab=2') |
| 135 | + expect(onChange.mock.calls.map(c => c[0])).toEqual([ |
| 136 | + 'http://localhost/app/routes', |
| 137 | + 'http://localhost/app/routes?tab=2', |
| 138 | + ]) |
| 139 | + |
| 140 | + dispose() |
| 141 | + expect(frame.isHistoryPristine()).toBe(true) |
| 142 | + expect(frame.isDetached()).toBe(true) |
| 143 | + |
| 144 | + frame.pushState('http://localhost/app/after-dispose') |
| 145 | + expect(onChange).toHaveBeenCalledTimes(2) |
| 146 | + }) |
| 147 | + |
| 148 | + it('the wrapper still performs the navigation it wraps', () => { |
| 149 | + const frame = fakeFrame('http://localhost/app/') |
| 150 | + watchFrameLocation({ iframe: frame.iframe, onChange: () => {}, initial: 'http://localhost/app/' }) |
| 151 | + frame.pushState('http://localhost/app/moved') |
| 152 | + expect(frame.iframe.contentWindow!.location.href).toBe('http://localhost/app/moved') |
| 153 | + }) |
| 154 | + |
| 155 | + it('reports pushState where the Navigation API exists too, without depending on it', () => { |
| 156 | + // Whether `pushState` fires a Navigation API event is not something to bet a |
| 157 | + // stale address bar on, so the wrapper stays in place either way. |
| 158 | + const frame = fakeFrame('http://localhost/app/', { navigation: true }) |
| 159 | + const onChange = vi.fn() |
| 160 | + watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 161 | + |
| 162 | + frame.pushState('http://localhost/app/routes') |
| 163 | + expect(onChange).toHaveBeenCalledTimes(1) |
| 164 | + expect(onChange).toHaveBeenLastCalledWith('http://localhost/app/routes') |
| 165 | + }) |
| 166 | + |
| 167 | + it('catches a navigation the wrapper cannot see, via currententrychange', () => { |
| 168 | + const frame = fakeFrame('http://localhost/app/', { navigation: true }) |
| 169 | + const onChange = vi.fn() |
| 170 | + watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 171 | + |
| 172 | + frame.pushStateBypassingWrapper('http://localhost/app/router-owned') |
| 173 | + expect(onChange).toHaveBeenCalledExactlyOnceWith('http://localhost/app/router-owned') |
| 174 | + }) |
| 175 | + |
| 176 | + it('reports a navigation heard from two sources once', () => { |
| 177 | + const frame = fakeFrame('http://localhost/app/', { navigation: true }) |
| 178 | + const onChange = vi.fn() |
| 179 | + watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 180 | + |
| 181 | + // A browser where `pushState` does fire the Navigation API event: the |
| 182 | + // wrapper and the listener both report, and the href dedupe absorbs it. |
| 183 | + frame.pushState('http://localhost/app/routes') |
| 184 | + frame.emitCurrentEntryChange() |
| 185 | + expect(onChange).toHaveBeenCalledExactlyOnceWith('http://localhost/app/routes') |
| 186 | + }) |
| 187 | + |
| 188 | + it('reports back/forward and hash routing', () => { |
| 189 | + const frame = fakeFrame('http://localhost/app/') |
| 190 | + const onChange = vi.fn() |
| 191 | + watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 192 | + |
| 193 | + frame.emit('popstate', 'http://localhost/app/back') |
| 194 | + frame.emit('hashchange', 'http://localhost/app/back#section') |
| 195 | + expect(onChange.mock.calls.map(c => c[0])).toEqual([ |
| 196 | + 'http://localhost/app/back', |
| 197 | + 'http://localhost/app/back#section', |
| 198 | + ]) |
| 199 | + }) |
| 200 | + |
| 201 | + it('re-subscribes after a document load, whose window and history are new', () => { |
| 202 | + const frame = fakeFrame('http://localhost/app/') |
| 203 | + const onChange = vi.fn() |
| 204 | + watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 205 | + |
| 206 | + frame.load('http://localhost/app/reloaded') |
| 207 | + expect(onChange).toHaveBeenLastCalledWith('http://localhost/app/reloaded') |
| 208 | + |
| 209 | + // The pre-load subscription died with the old window; only a fresh one on |
| 210 | + // the new `history` object keeps soft navigations reported. |
| 211 | + frame.pushState('http://localhost/app/reloaded/deep') |
| 212 | + expect(onChange).toHaveBeenLastCalledWith('http://localhost/app/reloaded/deep') |
| 213 | + }) |
| 214 | + |
| 215 | + it('reports an already-navigated frame on attach, but never the blank placeholder', () => { |
| 216 | + const booting = fakeFrame('about:blank') |
| 217 | + const onBoot = vi.fn() |
| 218 | + watchFrameLocation({ iframe: booting.iframe, onChange: onBoot, initial: 'http://localhost/app/' }) |
| 219 | + expect(onBoot).not.toHaveBeenCalled() |
| 220 | + |
| 221 | + // A frame that soft-navigated while no view was watching it (a shared frame |
| 222 | + // between dock switches) is corrected as soon as the next watch attaches. |
| 223 | + const live = fakeFrame('http://localhost/app/elsewhere') |
| 224 | + const onAttach = vi.fn() |
| 225 | + watchFrameLocation({ iframe: live.iframe, onChange: onAttach, initial: 'http://localhost/app/' }) |
| 226 | + expect(onAttach).toHaveBeenCalledExactlyOnceWith('http://localhost/app/elsewhere') |
| 227 | + }) |
| 228 | + |
| 229 | + it('reports each distinct href once', () => { |
| 230 | + const frame = fakeFrame('http://localhost/app/') |
| 231 | + const onChange = vi.fn() |
| 232 | + watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 233 | + |
| 234 | + frame.emit('popstate', 'http://localhost/app/same') |
| 235 | + frame.emit('popstate', 'http://localhost/app/same') |
| 236 | + expect(onChange).toHaveBeenCalledTimes(1) |
| 237 | + }) |
| 238 | + |
| 239 | + it('observes nothing on a cross-origin frame, and does not throw', () => { |
| 240 | + const frame = fakeFrame('http://elsewhere.test/app/', { crossOrigin: true }) |
| 241 | + const onChange = vi.fn() |
| 242 | + const dispose = watchFrameLocation({ iframe: frame.iframe, onChange, initial: 'http://localhost/app/' }) |
| 243 | + |
| 244 | + expect(onChange).not.toHaveBeenCalled() |
| 245 | + expect(() => frame.load('http://elsewhere.test/app/other')).not.toThrow() |
| 246 | + expect(onChange).not.toHaveBeenCalled() |
| 247 | + expect(() => dispose()).not.toThrow() |
| 248 | + }) |
| 249 | + |
| 250 | + it('tolerates a frame with no contentWindow', () => { |
| 251 | + const onChange = vi.fn() |
| 252 | + const iframe: FrameLocationTarget = { |
| 253 | + contentWindow: null, |
| 254 | + addEventListener: () => {}, |
| 255 | + removeEventListener: () => {}, |
| 256 | + } |
| 257 | + expect(() => watchFrameLocation({ iframe, onChange })()).not.toThrow() |
| 258 | + expect(onChange).not.toHaveBeenCalled() |
| 259 | + }) |
| 260 | +}) |
0 commit comments