Skip to content

Commit

Permalink
Merge pull request #27 from StanfordSocialNeuroscienceLab/extra-choices
Browse files Browse the repository at this point in the history
Support reusable choices array
  • Loading branch information
hesyifei committed Aug 6, 2020
2 parents f0500cd + a855d5c commit b93f5ac
Show file tree
Hide file tree
Showing 21 changed files with 786 additions and 215 deletions.
6 changes: 0 additions & 6 deletions config/names.json

This file was deleted.

52 changes: 34 additions & 18 deletions config/survey.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,22 @@
"question": "The current value on the slider bar is your previous response about how you currently feel. Please use the slider bar to indicate how you WOULD LIKE to feel.",
"slider": ["extremely negative", "extremely positive"],
"defaultValueFromQuestionId": "Feel_Current",
"next": "Anxiety"
},
"Anxiety": {
"id": "Anxiety",
"type": "ChoicesWithSingleAnswer",
"question": "Which of the following best categorizes the PRIMARY anxiety you are experiencing?",
"choices": "WELLBEING_CATEGORY",
"randomizeChoicesOrder": true,
"randomizeExceptForChoiceIds": ["Not experiencing stress", "Other"],
"next": "Stressor"
},
"Stressor": {
"id": "Stressor",
"type": "ChoicesWithSingleAnswer",
"question": "Which of the following best categorizes the PRIMARY stress you are experiencing?",
"choices": [
"Academic",
"Social/relationship",
"Mental health",
"Physical health",
"Financial",
"Not experiencing stress",
"Other"
],
"choices": "WELLBEING_CATEGORY",
"randomizeChoicesOrder": true,
"randomizeExceptForChoiceIds": ["Not experiencing stress", "Other"],
"next": "StressDurationNum"
Expand Down Expand Up @@ -121,15 +122,7 @@
"type": "MultipleText",
"question": "How would you best describe your relationship to the people you had an interaction with?",
"placeholder": "Select a category...",
"choices": [
"Friend",
"Co-worker",
"Parent",
"Sibling / other relative",
"Significant other",
"Stranger",
"Other"
],
"choices": "RELATIONSHIPS",
"forceChoice": true,
"max": 3,
"maxMinus": "SocInteraction_Names",
Expand All @@ -147,5 +140,28 @@
"next": null
}
}
},
"extraData": {
"reusableChoices": {
"WELLBEING_CATEGORY": [
"Academic",
"Social/relationship",
"Mental health",
"Physical health",
"Financial",
"Not experiencing it",
"Other"
],
"NAMES": ["REDACTED:", "a list", "of names", "here"],
"RELATIONSHIPS": [
"Friend",
"Co-worker",
"Parent",
"Sibling / other relative",
"Significant other",
"Stranger",
"Other"
]
}
}
}
14 changes: 12 additions & 2 deletions src/__tests__/helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as studyFileAsyncStorage from "../helpers/asyncStorage/studyFile";
import { StudyInfo } from "../helpers/types";
import { StudyInfo, ExtraData } from "../helpers/types";
import { FunctionSpyInstance } from "./jestHelper";

export const simplePipeInExtraMetaData = (id: string) => id;

/**
* Notice that it is possible to `mockStudyInfo` in an outer `beforeEach`,
* Notice that it is possible to e.g. `mockStudyInfo` in an outer `beforeEach`,
* and then `mockStudyInfo` again in an inner `beforeEach`.
* The `mockStudyInfo` in the inner `beforeEach` will override the outer one.
*
Expand All @@ -23,3 +23,13 @@ export function mockCurrentStudyInfo(
return mockStudyInfo;
});
}

export function mockCurrentExtraData(
mockExtraData: ExtraData,
): FunctionSpyInstance<typeof studyFileAsyncStorage.getCurrentExtraDataAsync> {
return jest
.spyOn(studyFileAsyncStorage, "getCurrentExtraDataAsync")
.mockImplementation(async () => {
return mockExtraData;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("ChoicesQuestionSchema", () => {
}).toThrowErrorMatchingSnapshot();
});

test("should be an array of strings", () => {
test("can be an array of strings", () => {
expect(() => {
ChoicesQuestionSchema.parse({
...question,
Expand All @@ -84,6 +84,29 @@ describe("ChoicesQuestionSchema", () => {
}).toThrowErrorMatchingSnapshot();
});

test("can be a string", () => {
expect(() => {
ChoicesQuestionSchema.parse({
...question,
choices: "helloworld",
});
}).not.toThrowError();

expect(() => {
ChoicesQuestionSchema.parse({
...question,
choices: "NAMES",
});
}).not.toThrowError();

expect(() => {
ChoicesQuestionSchema.parse({
...question,
choices: 5,
});
}).toThrowErrorMatchingSnapshot();
});

test("choices string cannot be empty", () => {
expect(() => {
ChoicesQuestionSchema.parse({
Expand Down Expand Up @@ -163,6 +186,18 @@ describe("ChoicesQuestionSchema", () => {
}).not.toThrowError();
});

test(`will not be tested if choices is string`, () => {
expect(() => {
ChoicesQuestionSchema.parse({
...question,
choices: "NAMES",
specialCasesStartId: {
一尊还酹江月: "dream",
},
});
}).not.toThrowError();
});

test(`can include only choices keys`, () => {
expect(() => {
ChoicesQuestionSchema.parse({
Expand Down Expand Up @@ -337,6 +372,16 @@ describe("ChoicesQuestionSchema", () => {
}).not.toThrowError();
});

test(`will not be tested if choices is string`, () => {
expect(() => {
ChoicesQuestionSchema.parse({
...question,
choices: "NAMES",
randomizeExceptForChoiceIds: ["花间一壶酒", "独酌无相亲"],
});
}).not.toThrowError();
});

test("can be choices keys", () => {
expect(() => {
ChoicesQuestionSchema.parse({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ describe("MultipleTextQuestionSchema", () => {
}).toThrowErrorMatchingSnapshot();
});

// TODO: WILL ONLY ACCEPT URL / ARRAY IN THE FUTURE
test("TODO: should not be string other than `NAMES`", () => {
test("can be string", () => {
expect(() => {
MultipleTextQuestionSchema.parse({
...question,
Expand All @@ -353,14 +352,7 @@ describe("MultipleTextQuestionSchema", () => {
...question,
choices: "helloworld",
});
}).toThrowErrorMatchingSnapshot("string");

expect(() => {
MultipleTextQuestionSchema.parse({
...question,
choices: "https://example.com/choices.json",
});
}).toThrowErrorMatchingSnapshot("url");
}).not.toThrowError();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ChoicesQuestionSchema choices can be a string 1`] = `
"1 validation issue(s)
Issue #0: invalid_union at choices
Invalid input
"
`;

exports[`ChoicesQuestionSchema choices can be an array of strings 1`] = `
"1 validation issue(s)
Issue #0: invalid_union at choices
Invalid input
"
`;

exports[`ChoicesQuestionSchema choices choices should not be duplicated 1`] = `
"1 validation issue(s)
Expand Down Expand Up @@ -32,20 +48,6 @@ exports[`ChoicesQuestionSchema choices choices string cannot be empty 2`] = `
"
`;

exports[`ChoicesQuestionSchema choices should be an array of strings 1`] = `
"3 validation issue(s)
Issue #0: invalid_type at choices.0
Expected string, received number
Issue #1: invalid_type at choices.1
Expected string, received number
Issue #2: invalid_type at choices.2
Expected string, received number
"
`;

exports[`ChoicesQuestionSchema choices should not be empty 1`] = `
"1 validation issue(s)
Expand All @@ -57,8 +59,8 @@ exports[`ChoicesQuestionSchema choices should not be empty 1`] = `
exports[`ChoicesQuestionSchema choices should not be undefined 1`] = `
"1 validation issue(s)
Issue #0: invalid_type at choices
Required
Issue #0: invalid_union at choices
Invalid input
"
`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,82 +1,66 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`MultipleTextQuestionSchema choices TODO: should not be string other than \`NAMES\`: string 1`] = `
"1 validation issue(s)
Issue #0: invalid_literal_value at choices
Input must be \\"NAMES\\"
"
`;

exports[`MultipleTextQuestionSchema choices TODO: should not be string other than \`NAMES\`: url 1`] = `
"1 validation issue(s)
Issue #0: invalid_literal_value at choices
Input must be \\"NAMES\\"
"
`;

exports[`MultipleTextQuestionSchema choices choices should not be duplicated 1`] = `
"1 validation issue(s)
Issue #0: invalid_union at choices
Invalid input
Issue #0: custom_error at choices
There should not be duplicate elements in the choices list.
"
`;

exports[`MultipleTextQuestionSchema choices choices should not be duplicated 2`] = `
"1 validation issue(s)
Issue #0: invalid_union at choices
Invalid input
Issue #0: custom_error at choices
There should not be duplicate elements in the choices list.
"
`;

exports[`MultipleTextQuestionSchema choices choices string cannot be empty 1`] = `
"1 validation issue(s)
Issue #0: invalid_union at choices
Invalid input
Issue #0: too_small at choices.0
Should be at least 1 characters
"
`;

exports[`MultipleTextQuestionSchema choices choices string cannot be empty 2`] = `
"1 validation issue(s)
Issue #0: invalid_union at choices
Invalid input
Issue #0: too_small at choices.0
Should be at least 1 characters
"
`;

exports[`MultipleTextQuestionSchema choices should not be anything besides array and string: number 1`] = `
"1 validation issue(s)
Issue #0: invalid_literal_value at choices
Input must be \\"NAMES\\"
Issue #0: invalid_union at choices
Invalid input
"
`;

exports[`MultipleTextQuestionSchema choices should not be anything besides array and string: object 1`] = `
"1 validation issue(s)
Issue #0: invalid_literal_value at choices
Input must be \\"NAMES\\"
Issue #0: invalid_union at choices
Invalid input
"
`;

exports[`MultipleTextQuestionSchema choices should not be empty array 1`] = `
"1 validation issue(s)
Issue #0: invalid_union at choices
Invalid input
Issue #0: nonempty_array_is_empty at choices
List must contain at least one item
"
`;

exports[`MultipleTextQuestionSchema choices should not be null 1`] = `
"1 validation issue(s)
Issue #0: invalid_literal_value at choices
Input must be \\"NAMES\\"
Issue #0: invalid_union at choices
Invalid input
"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ exports[`QuestionSchema mismatch type 1`] = `
Issue #0: unrecognized_keys at
Unrecognized key(s) in object: 'defaultValueFromQuestionId', 'slider'
Issue #1: invalid_type at choices
Required
Issue #1: invalid_union at choices
Invalid input
"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ exports[`QuestionsListSchema should not accept non-existent next: single 1`] = `
exports[`QuestionsListSchema should show individual question error: multiple 1`] = `
"5 validation issue(s)
Issue #0: invalid_type at choices
Required
Issue #0: invalid_union at choices
Invalid input
Issue #1: invalid_type at slider
Required
Expand Down
Loading

0 comments on commit b93f5ac

Please sign in to comment.