@thoughtspot/visual-embed-sdk
Version:
ThoughtSpot Embed SDK
242 lines • 10.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const logger_1 = require("../utils/logger");
const index_1 = require("../index");
const test_utils_1 = require("../test/test-utils");
const authInstance = tslib_1.__importStar(require("../auth"));
const thoughtSpotHost = 'tshost';
const defaultViewConfig = {
frameParams: {
width: 1280,
height: 720,
},
};
const PAYLOAD = 'Sample payload';
beforeAll(() => {
(0, index_1.init)({
thoughtSpotHost,
authType: index_1.AuthType.None,
});
jest.spyOn(window, 'alert');
jest.spyOn(authInstance, 'postLoginService').mockImplementation(() => Promise.resolve(undefined));
});
describe('test communication between host app and ThoughtSpot', () => {
beforeEach(() => {
document.body.innerHTML = (0, test_utils_1.getDocumentBody)();
});
test('should capture event from ThoughtSpot app', (done) => {
const searchEmbed = new index_1.SearchEmbed((0, test_utils_1.getRootEl)(), defaultViewConfig);
searchEmbed
.on(index_1.EmbedEvent.CustomAction, (data) => {
expect(data.data).toBe(PAYLOAD);
done();
})
.render();
(0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.CustomAction,
data: PAYLOAD,
});
});
});
// TODO: enable test once we are actually able to load stuff in the iframe
xtest('should trigger iframe load event', async () => {
const onLoadSpy = jest.fn();
const searchEmbed = new index_1.SearchEmbed((0, test_utils_1.getRootEl)(), {});
searchEmbed.on(index_1.EmbedEvent.Load, onLoadSpy).render();
await (0, test_utils_1.executeAfterWait)(() => {
expect(onLoadSpy).toHaveBeenCalled();
}, test_utils_1.EVENT_WAIT_TIME);
});
test('should trigger event to ThoughtSpot app', (done) => {
(0, test_utils_1.mockMessageChannel)();
const searchEmbed = new index_1.SearchEmbed((0, test_utils_1.getRootEl)(), {});
searchEmbed.render();
setTimeout(() => {
searchEmbed.trigger(index_1.HostEvent.Search, {
body: PAYLOAD,
});
}, test_utils_1.EVENT_WAIT_TIME);
(0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
iframe.contentWindow.addEventListener('message', (e) => {
expect(e.data.type).toBe(index_1.HostEvent.Search);
expect(e.data.data.body).toBe(PAYLOAD);
done();
});
});
});
test('should execute multiple event handlers if registered', async () => {
const handlerOne = jest.fn();
const handlerTwo = jest.fn();
const searchEmbed = new index_1.SearchEmbed((0, test_utils_1.getRootEl)(), defaultViewConfig);
searchEmbed
.on(index_1.EmbedEvent.CustomAction, handlerOne)
.on(index_1.EmbedEvent.CustomAction, handlerTwo)
.render();
await (0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.CustomAction,
data: PAYLOAD,
});
});
await (0, test_utils_1.executeAfterWait)(() => {
expect(handlerOne).toHaveBeenCalled();
expect(handlerTwo).toHaveBeenCalled();
}, test_utils_1.EVENT_WAIT_TIME);
});
test('should capture event from correct iframe', async () => {
const spyOne = jest.fn();
const embedOne = new index_1.SearchEmbed((0, test_utils_1.getRootEl)(), defaultViewConfig);
embedOne.on(index_1.EmbedEvent.CustomAction, spyOne).render();
const spyTwo = jest.fn();
const embedTwo = new index_1.PinboardEmbed((0, test_utils_1.getRootEl2)(), {
...defaultViewConfig,
pinboardId: 'eca215d4-0d2c-4a55-90e3-d81ef6848ae0',
});
const spyThree = jest.fn();
const embedThree = new index_1.LiveboardEmbed((0, test_utils_1.getRootEl2)(), {
...defaultViewConfig,
liveboardId: 'eca215d4-0d2c-4a55-90e3-d81ef6848ae0',
});
embedTwo.on(index_1.EmbedEvent.CustomAction, spyTwo).render();
embedThree.on(index_1.EmbedEvent.CustomAction, spyThree).render();
await (0, test_utils_1.executeAfterWait)(() => {
const iframeOne = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframeOne.contentWindow, {
type: index_1.EmbedEvent.CustomAction,
data: PAYLOAD,
});
});
await (0, test_utils_1.executeAfterWait)(() => {
expect(spyOne).toHaveBeenCalled();
expect(spyTwo).not.toHaveBeenCalled();
expect(spyThree).not.toHaveBeenCalled();
}, test_utils_1.EVENT_WAIT_TIME);
});
test('send getIframeCenter Event without eventPort', async () => {
const liveboardEmbed = new index_1.LiveboardEmbed((0, test_utils_1.getRootEl)(), {
...defaultViewConfig,
fullHeight: true,
pinboardId: 'eca215d4-0d2c-4a55-90e3-d81ef6848ae0',
});
liveboardEmbed.render();
const spy1 = jest.spyOn(logger_1.logger, 'log');
await (0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.EmbedIframeCenter,
data: PAYLOAD,
});
});
expect(spy1).toHaveBeenCalledWith('Event Port is not defined');
});
test('send getIframeCenter Event without eventPort - pinboard', async () => {
const pinboardEmbed = new index_1.PinboardEmbed((0, test_utils_1.getRootEl)(), {
...defaultViewConfig,
fullHeight: true,
pinboardId: 'eca215d4-0d2c-4a55-90e3-d81ef6848ae0',
});
pinboardEmbed.render();
const spy1 = jest.spyOn(logger_1.logger, 'log');
await (0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.EmbedIframeCenter,
data: PAYLOAD,
});
});
expect(spy1).toHaveBeenCalledWith('Event Port is not defined');
});
test('send getIframeCenter Event with eventPort - pinboard', async () => {
const pinboardEmbed = new index_1.PinboardEmbed((0, test_utils_1.getRootEl)(), {
...defaultViewConfig,
fullHeight: true,
pinboardId: 'eca215d4-0d2c-4a55-90e3-d81ef6848ae0',
});
pinboardEmbed.render();
const mockPort = {
postMessage: jest.fn(),
};
await (0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.EmbedIframeCenter,
data: PAYLOAD,
}, mockPort);
});
const heightObj = {
data: {
iframeCenter: 0,
iframeHeight: 0,
iframeScrolled: 0,
iframeVisibleViewPort: 0,
viewPortHeight: 768,
},
type: index_1.EmbedEvent.EmbedIframeCenter,
};
expect(mockPort.postMessage).toHaveBeenCalledWith(heightObj);
});
test('send getIframeCenter Event with eventPort', async () => {
const liveboardEmbed = new index_1.LiveboardEmbed((0, test_utils_1.getRootEl)(), {
...defaultViewConfig,
fullHeight: true,
pinboardId: 'eca215d4-0d2c-4a55-90e3-d81ef6848ae0',
});
liveboardEmbed.render();
const mockPort = {
postMessage: jest.fn(),
};
await (0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.EmbedIframeCenter,
data: PAYLOAD,
}, mockPort);
});
const heightObj = {
data: {
iframeCenter: 0,
iframeHeight: 0,
iframeScrolled: 0,
iframeVisibleViewPort: 0,
viewPortHeight: 768,
},
type: index_1.EmbedEvent.EmbedIframeCenter,
};
expect(mockPort.postMessage).toHaveBeenCalledWith(heightObj);
});
test('ALL event listener should fire for all events with the event type set correctly', async () => {
const embed = new index_1.AppEmbed((0, test_utils_1.getRootEl)(), defaultViewConfig);
const spy = jest.fn();
embed.on(index_1.EmbedEvent.ALL, spy);
embed.render();
await (0, test_utils_1.executeAfterWait)(() => {
const iframe = (0, test_utils_1.getIFrameEl)();
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.CustomAction,
data: PAYLOAD,
});
(0, test_utils_1.postMessageToParent)(iframe.contentWindow, {
type: index_1.EmbedEvent.DialogOpen,
});
});
await (0, test_utils_1.executeAfterWait)(() => {
expect(spy).toHaveBeenCalledTimes(3);
expect(spy.mock.calls[0][0]).toMatchObject({
type: index_1.EmbedEvent.Init,
});
expect(spy.mock.calls[1][0]).toMatchObject({
type: index_1.EmbedEvent.CustomAction,
data: PAYLOAD,
});
expect(spy.mock.calls[2][0]).toMatchObject({
type: index_1.EmbedEvent.DialogOpen,
});
}, test_utils_1.EVENT_WAIT_TIME);
});
});
//# sourceMappingURL=events.spec.js.map