Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cea2aj committed Jun 28, 2021
1 parent 0f39c75 commit 398de54
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions static/js/__mocks__/iframe-common.js
Original file line number Diff line number Diff line change
@@ -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;
39 changes: 39 additions & 0 deletions tests/static/js/answers-experience-frame.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 398de54

Please sign in to comment.