Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnswersExperienceFrame unit tests #855

Merged
merged 1 commit into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});