|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { popoutOpener, returnFromPopout } from '../shell-prefs'; |
| 3 | + |
| 4 | +/** A stand-in for a window, with the fields the helpers read. */ |
| 5 | +function fakeWindow(name: string, opener: unknown, referrer = '') { |
| 6 | + const win = { name, opener, document: { referrer } } as unknown as Window & { |
| 7 | + top: Window; |
| 8 | + }; |
| 9 | + Object.defineProperty(win, 'top', { value: win }); |
| 10 | + return win; |
| 11 | +} |
| 12 | + |
| 13 | +describe('the pop-out window', () => { |
| 14 | + it('knows the app page only in the window the popup opened', () => { |
| 15 | + const opener = { postMessage: vi.fn(), location: { origin: 'http://localhost:4200' } }; |
| 16 | + expect(popoutOpener(fakeWindow('pangular-inspector-k3x9', opener))).toBe(opener); |
| 17 | + expect(popoutOpener(fakeWindow('other', opener))).toBeNull(); |
| 18 | + expect(popoutOpener(fakeWindow('pangular-inspector-k3x9', null))).toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('asks the popup to take the panel back, on the app page origin', () => { |
| 22 | + const opener = { postMessage: vi.fn(), location: { origin: 'http://localhost:4200' } }; |
| 23 | + expect(returnFromPopout(fakeWindow('pangular-inspector-k3x9', opener))).toBe(true); |
| 24 | + expect(opener.postMessage).toHaveBeenCalledWith( |
| 25 | + { type: 'pangular:popup-return' }, |
| 26 | + 'http://localhost:4200', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('falls back to the referrer when the app page is on another origin', () => { |
| 31 | + const opener = { postMessage: vi.fn() }; |
| 32 | + Object.defineProperty(opener, 'location', { |
| 33 | + get: () => { |
| 34 | + throw new Error('cross origin'); |
| 35 | + }, |
| 36 | + }); |
| 37 | + const win = fakeWindow('pangular-inspector-k3x9', opener, 'http://app.test/trips'); |
| 38 | + expect(returnFromPopout(win)).toBe(true); |
| 39 | + expect(opener.postMessage).toHaveBeenCalledWith( |
| 40 | + { type: 'pangular:popup-return' }, |
| 41 | + 'http://app.test', |
| 42 | + ); |
| 43 | + expect(returnFromPopout(fakeWindow('pangular-inspector-k3x9', opener))).toBe(false); |
| 44 | + }); |
| 45 | +}); |
0 commit comments