UNPKG

@thoughtspot/visual-embed-sdk

Version:
71 lines 3.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const _processTriggerInstance = tslib_1.__importStar(require("./processTrigger")); const types_1 = require("../types"); const test_utils_1 = require("../test/test-utils"); describe('Unit test for processTrigger', () => { const iFrame = { contentWindow: { postMessage: jest.fn(), }, }; test('when hostevent is reload, reload function should be called with iFrame', async () => { jest.useFakeTimers(); const iFrameElement = document.createElement('iframe'); const html = '<body>Foo</body>'; iFrameElement.src = `data:text/html;charset=utf-8,${encodeURI(html)}`; const divFrame = document.createElement('div'); divFrame.appendChild(iFrameElement); const messageType = types_1.HostEvent.Reload; const thoughtSpotHost = 'http://localhost:3000'; const spyReload = jest.spyOn(_processTriggerInstance, 'reload'); const data = {}; _processTriggerInstance.processTrigger(iFrameElement, messageType, thoughtSpotHost, data); jest.advanceTimersByTime(200); expect(spyReload).toBeCalledWith(iFrameElement); }); test('when hostevent is search, postMessage should be called', async () => { const messageType = types_1.HostEvent.Search; const thoughtSpotHost = 'http://localhost:3000'; const data = {}; (0, test_utils_1.mockMessageChannel)(); const triggerPromise = _processTriggerInstance.processTrigger(iFrame, messageType, thoughtSpotHost, data); expect(iFrame.contentWindow.postMessage).toBeCalled(); const res = { data: { test: '123', }, }; test_utils_1.messageChannelMock.port1.onmessage(res); expect(test_utils_1.messageChannelMock.port1.close).toBeCalled(); expect(triggerPromise).resolves.toEqual(res.data); }); test('Reject promise if error returned', async () => { const messageType = types_1.HostEvent.Search; const thoughtSpotHost = 'http://localhost:3000'; const data = {}; (0, test_utils_1.mockMessageChannel)(); const triggerPromise = _processTriggerInstance.processTrigger(iFrame, messageType, thoughtSpotHost, data); expect(iFrame.contentWindow.postMessage).toBeCalled(); const res = { data: { error: 'error', }, }; test_utils_1.messageChannelMock.port1.onmessage(res); expect(test_utils_1.messageChannelMock.port1.close).toBeCalled(); expect(triggerPromise).rejects.toEqual(res.data.error); }); test('should close channel.port1 when timeout exceeds TRIGGER_TIMEOUT', async () => { const messageType = types_1.HostEvent.Search; const thoughtSpotHost = 'http://localhost:3000'; const data = {}; (0, test_utils_1.mockMessageChannel)(); const triggerPromise = _processTriggerInstance.processTrigger(iFrame, messageType, thoughtSpotHost, data); jest.advanceTimersByTime(_processTriggerInstance.TRIGGER_TIMEOUT); expect(test_utils_1.messageChannelMock.port1.close).toBeCalled(); await expect(triggerPromise).resolves.toBeInstanceOf(Error); }); }); //# sourceMappingURL=processTrigger.spec.js.map