diff --git a/static/js/__mocks__/iframe-common.js b/static/js/__mocks__/iframe-common.js new file mode 100644 index 000000000..e2e019fd1 --- /dev/null +++ b/static/js/__mocks__/iframe-common.js @@ -0,0 +1,6 @@ +// Note: This will need to be updated to 'createMockFromModule' if we upgrade to jest 26+ +const iframeCommon = jest.genMockFromModule('./iframe-common'); + +iframeCommon.sendToIframe = jest.fn(); + +module.exports = iframeCommon; \ No newline at end of file diff --git a/tests/static/js/answers-experience-frame.js b/tests/static/js/answers-experience-frame.js new file mode 100644 index 000000000..60caee222 --- /dev/null +++ b/tests/static/js/answers-experience-frame.js @@ -0,0 +1,39 @@ +import AnswersExperienceFrame from '../../../static/js/answers-experience-frame'; +import RuntimeConfig from '../../../static/js/runtime-config'; +import { sendToIframe } from '../../../static/js/iframe-common'; + +jest.mock('../../../static/js/iframe-common'); + +describe('AnswersExperienceFrame works propertly', () => { + let answersExperienceFrame; + + beforeEach(() => { + const runtimeConfig = new RuntimeConfig(); + answersExperienceFrame = new AnswersExperienceFrame(runtimeConfig); + jest.clearAllMocks(); // ensure mock.calls are cleared before each test + }); + + it('The init function sends an init message to the iframe', () => { + answersExperienceFrame.init({}); + const expectedMessage = { + initAnswersExperience: true, + runtimeConfig: {} + }; + expect(sendToIframe).toHaveBeenCalledWith(expectedMessage); + }); + + it('Runtime config passed to the init function is sent to the child iframe', () => { + answersExperienceFrame.init({linkTarget: '_blank'}); + const expectedMessage = { + initAnswersExperience: true, + runtimeConfig: {linkTarget: '_blank'} + }; + expect(sendToIframe).toHaveBeenCalledWith(expectedMessage); + }); + + it('An init message will only be sent once', () => { + answersExperienceFrame.init({}); + answersExperienceFrame.init({}); + expect(sendToIframe).toHaveBeenCalledTimes(1); + }); +}); \ No newline at end of file