Skip to content

Commit

Permalink
Add tests for SurveyJS locale
Browse files Browse the repository at this point in the history
  • Loading branch information
okaycj committed Oct 31, 2024
1 parent 9f24375 commit 50e87fe
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions packages/surveys/src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Model } from "survey-jquery";
import { Trial } from "./exitSurvey";
import {
consentSurveyFunction,
exitSurveyFunction,
Expand All @@ -10,9 +11,34 @@ jest.mock("@lookit/data", () => ({
updateResponse: jest.fn().mockReturnValue("Response"),
}));

/**
* Helper function to generate a trial object.
*
* @param values - Additonal paramters added to trial object
* @returns Trial object
*/
const getTrial = (values: Record<string, string | boolean> = {}) =>
({
locale: "en-US",
...values,
}) as unknown as Trial;

/**
* Helper function to generate surveys for testing.
*
* @param values - Values to add to survey object
* @returns Survey
*/
const getSurvey = (values: Record<string, string | object> = {}) =>
({
onComplete: { add: jest.fn() },
onTextMarkdown: { add: jest.fn() },
...values,
}) as unknown as Model;

test("Markdown to HTML through survey function", () => {
const addMock = jest.fn();
const survey = { onTextMarkdown: { add: addMock } } as unknown as Model;
const survey = getSurvey({ onTextMarkdown: { add: addMock } });
const textValue = "some text";
const options = { text: `**${textValue}**`, html: null };
const rtnSurvey = textMarkdownSurveyFunction(survey);
Expand All @@ -26,11 +52,9 @@ test("Markdown to HTML through survey function", () => {
});

test("Exit survey function", () => {
const survey = {
onComplete: { add: jest.fn() },
onTextMarkdown: { add: jest.fn() },
} as unknown as Model;
const rtnSurvey = exitSurveyFunction(survey);
const survey = getSurvey();
const trial = getTrial();
const rtnSurvey = exitSurveyFunction(trial)(survey);

expect(survey.onComplete.add).toHaveBeenCalledTimes(1);
expect(survey.onTextMarkdown.add).toHaveBeenCalledTimes(1);
Expand All @@ -39,13 +63,11 @@ test("Exit survey function", () => {

test("Anonymous function within exit survey function where withdrawal > 0", () => {
const addMock = jest.fn();
const survey = {
onComplete: { add: addMock },
onTextMarkdown: { add: jest.fn() },
} as unknown as Model;
const survey = getSurvey({ onComplete: { add: addMock } });
const sender = { setValue: jest.fn() };
const trial = getTrial();

exitSurveyFunction(survey);
exitSurveyFunction(trial)(survey);

const anonFn = addMock.mock.calls[0][0];

Expand All @@ -58,13 +80,11 @@ test("Anonymous function within exit survey function where withdrawal > 0", () =

test("Anonymous function within exit survey function where withdrawal is 0", () => {
const addMock = jest.fn();
const survey = {
onComplete: { add: addMock },
onTextMarkdown: { add: jest.fn() },
} as unknown as Model;
const survey = getSurvey({ onComplete: { add: addMock } });
const sender = { setValue: jest.fn() };
const trial = getTrial();

exitSurveyFunction(survey);
exitSurveyFunction(trial)(survey);

const anonFn = addMock.mock.calls[0][0];

Expand All @@ -77,10 +97,7 @@ test("Anonymous function within exit survey function where withdrawal is 0", ()
});

test("Consent survey function", () => {
const survey = {
onComplete: { add: jest.fn() },
onTextMarkdown: { add: jest.fn() },
} as unknown as Model;
const survey = getSurvey();
const survey_function = consentSurveyFunction();
const rtnSurvey = survey_function(survey);

Expand All @@ -90,10 +107,7 @@ test("Consent survey function", () => {
});

test("User function for consent survey function", () => {
const survey = {
onComplete: { add: jest.fn() },
onTextMarkdown: { add: jest.fn() },
} as unknown as Model;
const survey = getSurvey();
const userFn = jest.fn();
const survey_function = consentSurveyFunction(userFn);

Expand All @@ -104,10 +118,7 @@ test("User function for consent survey function", () => {

test("Anonymous function within consent survey function", () => {
const addMock = jest.fn();
const survey = {
onComplete: { add: addMock },
onTextMarkdown: { add: jest.fn() },
} as unknown as Model;
const survey = getSurvey({ onComplete: { add: addMock } });

consentSurveyFunction()(survey);

Expand All @@ -120,3 +131,17 @@ test("Anonymous function within consent survey function", () => {
expect(survey.onComplete.add).toHaveBeenCalledTimes(1);
expect(survey.onTextMarkdown.add).toHaveBeenCalledTimes(1);
});

test("Set SurveyJS locale parameter", () => {
const trial = getTrial();
const survey = getSurvey();
exitSurveyFunction(trial)(survey);
expect(survey.locale).toStrictEqual("en-US");
});

test("Set SurveyJS locale parameter to French", () => {
const trial = getTrial({ locale: "fr" });
const survey = getSurvey();
exitSurveyFunction(trial)(survey);
expect(survey.locale).toStrictEqual(trial.locale);
});

0 comments on commit 50e87fe

Please sign in to comment.