|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { DEVFRAME_EVENTS } from '../events' |
| 3 | +import { provideDevframeTheme, watchDevframeTheme } from './theme' |
| 4 | + |
| 5 | +const channel = DEVFRAME_EVENTS.postMessage.theme |
| 6 | + |
| 7 | +function fixture() { |
| 8 | + const parent = new EventTarget() as Window |
| 9 | + const panel = new EventTarget() as Window |
| 10 | + const origin = 'https://panel.example' |
| 11 | + const parentOrigin = 'https://hub.example' |
| 12 | + const queue: (() => void)[] = [] |
| 13 | + const send = (target: EventTarget, data: unknown, source: EventTarget, origin: string) => { |
| 14 | + target.dispatchEvent(Object.assign(new Event('message'), { data, source, origin })) |
| 15 | + } |
| 16 | + Object.assign(parent, { |
| 17 | + postMessage: (data: unknown) => queue.push(() => send(parent, data, panel, origin)), |
| 18 | + }) |
| 19 | + Object.assign(panel, { |
| 20 | + parent, |
| 21 | + postMessage: (data: unknown, targetOrigin: string) => queue.push(() => { |
| 22 | + if (targetOrigin === '*' || targetOrigin === origin) |
| 23 | + send(panel, data, parent, parentOrigin) |
| 24 | + }), |
| 25 | + }) |
| 26 | + // Model both browsing contexts and queued postMessage delivery without a DOM. |
| 27 | + const iframe = Object.assign(new EventTarget(), { |
| 28 | + contentWindow: panel, |
| 29 | + ownerDocument: { defaultView: parent } as Document, |
| 30 | + }) as HTMLIFrameElement |
| 31 | + vi.stubGlobal('window', panel) |
| 32 | + return { |
| 33 | + iframe, |
| 34 | + panel, |
| 35 | + parent, |
| 36 | + send, |
| 37 | + flush() { |
| 38 | + while (queue.length) |
| 39 | + queue.shift()!() |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +afterEach(() => vi.unstubAllGlobals()) |
| 45 | + |
| 46 | +describe('panel theme protocol', () => { |
| 47 | + it.each(['panel', 'provider'])('connects cross-origin when %s boots first', (first) => { |
| 48 | + const f = fixture() |
| 49 | + const changed = vi.fn() |
| 50 | + const startPanel = () => watchDevframeTheme(changed) |
| 51 | + const startProvider = () => provideDevframeTheme(f.iframe, { primaryColor: '#6b84fd' }) |
| 52 | + if (first === 'panel') { |
| 53 | + startPanel() |
| 54 | + f.flush() |
| 55 | + startProvider() |
| 56 | + } |
| 57 | + else { |
| 58 | + startProvider() |
| 59 | + f.flush() |
| 60 | + startPanel() |
| 61 | + } |
| 62 | + f.flush() |
| 63 | + expect(changed).toHaveBeenLastCalledWith({ primaryColor: '#6b84fd' }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('updates and clears the accent without changing other theme state', () => { |
| 67 | + const f = fixture() |
| 68 | + const provider = provideDevframeTheme(f.iframe, { primaryColor: '#6b84fd' }) |
| 69 | + const changed = vi.fn() |
| 70 | + watchDevframeTheme(changed) |
| 71 | + f.flush() |
| 72 | + provider.update({ primaryColor: 'rebeccapurple' }) |
| 73 | + f.flush() |
| 74 | + expect(changed).toHaveBeenLastCalledWith({ primaryColor: 'rebeccapurple' }) |
| 75 | + provider.update({}) |
| 76 | + f.flush() |
| 77 | + expect(changed).toHaveBeenLastCalledWith({ primaryColor: undefined }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('offers no theme data until the iframe opts in', () => { |
| 81 | + const f = fixture() |
| 82 | + const received: unknown[] = [] |
| 83 | + f.panel.addEventListener('message', event => received.push((event as MessageEvent).data)) |
| 84 | + const provider = provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) |
| 85 | + provider.update({ primaryColor: 'red' }) |
| 86 | + f.flush() |
| 87 | + expect(received).toEqual([{ channel, type: 'available' }]) |
| 88 | + }) |
| 89 | + |
| 90 | + it('requires a new document to opt in after navigation', () => { |
| 91 | + const f = fixture() |
| 92 | + const provider = provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) |
| 93 | + const changed = vi.fn() |
| 94 | + const stop = watchDevframeTheme(changed) |
| 95 | + f.flush() |
| 96 | + stop() |
| 97 | + f.iframe.dispatchEvent(new Event('load')) |
| 98 | + provider.update({ primaryColor: 'red' }) |
| 99 | + f.flush() |
| 100 | + changed.mockClear() |
| 101 | + watchDevframeTheme(changed) |
| 102 | + f.flush() |
| 103 | + expect(changed).toHaveBeenLastCalledWith({ primaryColor: 'red' }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('rejects theme updates from sibling windows and malformed messages', () => { |
| 107 | + const f = fixture() |
| 108 | + const changed = vi.fn() |
| 109 | + watchDevframeTheme(changed) |
| 110 | + f.send(f.panel, { channel, type: 'update', theme: { primaryColor: 'red' } }, new EventTarget(), 'https://hub.example') |
| 111 | + for (const data of [null, 'blue', { channel, type: 'update', theme: null }, { channel, type: 'update', theme: { primaryColor: 42 } }]) |
| 112 | + f.send(f.panel, data, f.parent, 'https://hub.example') |
| 113 | + expect(changed).not.toHaveBeenCalled() |
| 114 | + }) |
| 115 | + |
| 116 | + it('rejects requests from unrelated windows and opaque origins', () => { |
| 117 | + const f = fixture() |
| 118 | + const received: unknown[] = [] |
| 119 | + f.panel.addEventListener('message', event => received.push((event as MessageEvent).data)) |
| 120 | + provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) |
| 121 | + f.flush() |
| 122 | + received.length = 0 |
| 123 | + f.send(f.parent, { channel, type: 'request' }, new EventTarget(), 'https://panel.example') |
| 124 | + f.send(f.parent, { channel, type: 'request' }, f.panel, 'null') |
| 125 | + f.flush() |
| 126 | + expect(received).toEqual([]) |
| 127 | + }) |
| 128 | + |
| 129 | + it('cleans up both ends and reconnects a preserved iframe to a new provider', () => { |
| 130 | + const f = fixture() |
| 131 | + const changed = vi.fn() |
| 132 | + const stop = watchDevframeTheme(changed) |
| 133 | + const provider = provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) |
| 134 | + f.flush() |
| 135 | + provider.dispose() |
| 136 | + changed.mockClear() |
| 137 | + provider.update({ primaryColor: 'red' }) |
| 138 | + f.iframe.dispatchEvent(new Event('load')) |
| 139 | + f.flush() |
| 140 | + expect(changed).not.toHaveBeenCalled() |
| 141 | + const next = provideDevframeTheme(f.iframe, {}) |
| 142 | + f.flush() |
| 143 | + expect(changed).toHaveBeenLastCalledWith({ primaryColor: undefined }) |
| 144 | + stop() |
| 145 | + changed.mockClear() |
| 146 | + next.update({ primaryColor: 'red' }) |
| 147 | + f.flush() |
| 148 | + expect(changed).not.toHaveBeenCalled() |
| 149 | + }) |
| 150 | + |
| 151 | + it('leaves standalone SPAs and SSR untouched', () => { |
| 152 | + const changed = vi.fn() |
| 153 | + vi.stubGlobal('window', undefined) |
| 154 | + watchDevframeTheme(changed)() |
| 155 | + const standalone = { parent: undefined as unknown } |
| 156 | + standalone.parent = standalone |
| 157 | + vi.stubGlobal('window', standalone) |
| 158 | + watchDevframeTheme(changed)() |
| 159 | + expect(changed).not.toHaveBeenCalled() |
| 160 | + }) |
| 161 | +}) |
0 commit comments