UNPKG

@trivia-api/models

Version:

Models for The Trivia API.

149 lines (148 loc) 6.14 kB
import { assertIsImageChoiceQuestion, isImageChoiceQuestion } from "../index"; import { VALID_IMAGE_CHOICE_QUESTION } from "../../../fixtures/image_choice"; import { cloneDeep } from "lodash"; const NO_CATEGORY = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); delete NO_CATEGORY.category; const INVALID_CATEGORY = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); INVALID_CATEGORY.category = "invalid_category"; const NO_CORRECT_ANSWER = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); delete NO_CORRECT_ANSWER.correctAnswer; const INVALID_CORRECT_ANSWER = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); INVALID_CORRECT_ANSWER.correctAnswer = "invalid_correct_answer"; const EMPTY_CORRECT_ANSWER = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); EMPTY_CORRECT_ANSWER.correctAnswer = []; const INVALID_IMAGE_OPTION = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); INVALID_IMAGE_OPTION.correctAnswer = [{ thing: "invalid_image_option" }]; const NO_INCORRECT_ANSWER = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); delete NO_INCORRECT_ANSWER.incorrectAnswers; const EMPTY_INCORRECT_ANSWERs = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); EMPTY_INCORRECT_ANSWERs.incorrectAnswers = []; const INVALID_IMAGE_OPTIONS = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); INVALID_IMAGE_OPTIONS.incorrectAnswers = [["invalid_image_option"]]; const NO_QUESTION = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); delete NO_QUESTION.question; const QUESTION_NOT_OBJECT = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); QUESTION_NOT_OBJECT.question = "question"; const QUESTION_NO_TEXT = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); QUESTION_NO_TEXT.question = { image: "image" }; const QUESTION_TEXT_WRONG_TYPE = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); QUESTION_TEXT_WRONG_TYPE.question = { text: 123 }; const NO_TAGS = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); delete NO_TAGS.tags; const TAGS_NOT_ARRAY = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); TAGS_NOT_ARRAY.tags = "tags"; const TAG_NOT_STRING = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); TAG_NOT_STRING.tags = [123]; const NO_TYPE = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); delete NO_TYPE.type; const TYPE_NOT_IMAGE_CHOICE = cloneDeep(VALID_IMAGE_CHOICE_QUESTION); TYPE_NOT_IMAGE_CHOICE.type = "text_choice"; const IMAGE_CHOICE_TEST_CASES = [ { case: "the question has no category", question: NO_CATEGORY, expectedError: "Image choice question does not have category", }, { case: "the category is invalid", question: INVALID_CATEGORY, expectedError: "Image choice question category is not valid", }, { case: "the question has no correct answer", question: NO_CORRECT_ANSWER, expectedError: "Image choice question does not have correctAnswer", }, { case: "the correct answer is not an array", question: INVALID_CORRECT_ANSWER, expectedError: "Image choice question correctAnswer is not an array", }, { case: "the correct answer is empty", question: EMPTY_CORRECT_ANSWER, expectedError: "Image choice question correctAnswer is empty", }, { case: "the correct answer contains an invalid image option", question: INVALID_IMAGE_OPTION, expectedError: "Image option does not have url", }, { case: "the question has no incorrect answers", question: NO_INCORRECT_ANSWER, expectedError: "Image choice question does not have incorrectAnswers", }, { case: "the incorrect answers are empty", question: EMPTY_INCORRECT_ANSWERs, expectedError: "Image choice question incorrectAnswers is empty", }, { case: "the incorrect answers contain an invalid image option", question: INVALID_IMAGE_OPTIONS, expectedError: "Image option array contains invalid image option", }, { case: "the question has no question", question: NO_QUESTION, expectedError: "Image choice question does not have question", }, { case: "the question is not an object", question: QUESTION_NOT_OBJECT, expectedError: "Image choice question question is not an object", }, { case: "the question object has no text", question: QUESTION_NO_TEXT, expectedError: "Image choice question question does not have text", }, { case: "the question object has text of the wrong type", question: QUESTION_TEXT_WRONG_TYPE, expectedError: "Image choice question question text is not a string", }, { case: "the question has no tags", question: NO_TAGS, expectedError: "Image choice question does not have tags", }, { case: "the tags are not an array", question: TAGS_NOT_ARRAY, expectedError: "Image choice question tags is not an array", }, { case: "the tags contain a non-string", question: TAG_NOT_STRING, expectedError: "Image choice question tags contains non-string", }, { case: "the question has no type", question: NO_TYPE, expectedError: "Image choice question does not have type", }, { case: "the type is not image_choice", question: TYPE_NOT_IMAGE_CHOICE, expectedError: "Image choice question type is not image_choice", }, ]; describe("assertIsImageChoiceQuestion", () => { it("doesn't throw for a valid image choice question", () => { expect(() => assertIsImageChoiceQuestion(VALID_IMAGE_CHOICE_QUESTION)).not.toThrow(); }); it.each(IMAGE_CHOICE_TEST_CASES)("throws the correct error message when $case", ({ question, expectedError }) => { jest.spyOn(console, "error").mockImplementationOnce(() => { }); expect(() => assertIsImageChoiceQuestion(question)).toThrow(expectedError); }); }); describe("isImageChoiceQuestion", () => { it("returns true for a valid image choice question", () => { expect(isImageChoiceQuestion(VALID_IMAGE_CHOICE_QUESTION)).toBe(true); }); it.each(IMAGE_CHOICE_TEST_CASES)("returns false when $case", ({ question }) => { expect(isImageChoiceQuestion(question)).toBe(false); }); });