UNPKG

altair-graphql-core

Version:

Several of the core logic for altair graphql client

72 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const events_1 = require("./events"); const parent_worker_1 = require("./parent-worker"); (0, vitest_1.describe)('PluginParentWorker', () => { let worker; (0, vitest_1.beforeEach)(() => { Object.defineProperty(HTMLIFrameElement.prototype, 'sandbox', { configurable: true, value: { add: vitest_1.vi.fn() }, }); }); (0, vitest_1.afterEach)(() => { worker?.destroy(); worker = undefined; vitest_1.vi.restoreAllMocks(); delete HTMLIFrameElement.prototype.sandbox; }); (0, vitest_1.it)('initializes script sandboxes with postMessage instead of URL parameters', async () => { worker = new parent_worker_1.PluginParentWorker({ id: 'plugin-1', type: 'scripts', sandboxUrl: 'https://sandbox.example/index.html', scriptUrls: ['https://plugin.example/index.js'], styleUrls: ['https://plugin.example/index.css'], }); const iframe = worker.getIframe(); const sandboxWindow = iframe.contentWindow; (0, vitest_1.expect)(sandboxWindow).not.toBeNull(); if (!sandboxWindow) { throw new Error('Expected iframe content window'); } const postMessage = vitest_1.vi.spyOn(sandboxWindow, 'postMessage'); (0, vitest_1.expect)(iframe.src).toBe('https://sandbox.example/index.html?sandbox_type=plugin'); (0, vitest_1.expect)(iframe.src).not.toContain('scriptUrls'); (0, vitest_1.expect)(iframe.src).not.toContain('styleUrls'); window.dispatchEvent(new MessageEvent('message', { origin: 'null', source: window, data: { type: events_1.PLUGIN_SANDBOX_BOOTSTRAP_READY }, })); (0, vitest_1.expect)(postMessage).not.toHaveBeenCalled(); window.dispatchEvent(new MessageEvent('message', { origin: 'null', source: sandboxWindow, data: { type: events_1.PLUGIN_SANDBOX_BOOTSTRAP_READY }, })); (0, vitest_1.expect)(postMessage).toHaveBeenCalledWith({ type: events_1.PLUGIN_SANDBOX_INITIALIZE, payload: { params: { sc: window.location.origin, id: 'plugin-1', instanceType: 'main', }, scriptUrls: ['https://plugin.example/index.js'], styleUrls: ['https://plugin.example/index.css'], }, }, '*'); window.dispatchEvent(new MessageEvent('message', { origin: 'null', source: sandboxWindow, data: { type: events_1.PLUGIN_SANDBOX_INITIALIZED, frameId: 'plugin-1' }, })); worker.send('plugin-engine::ready'); await vitest_1.vi.waitFor(() => { (0, vitest_1.expect)(postMessage).toHaveBeenCalledWith({ type: 'plugin-engine::ready', payload: undefined }, '*'); }); }); }); //# sourceMappingURL=parent-worker.spec.js.map