UNPKG

@trivia-api/models

Version:

Models for The Trivia API.

151 lines (150 loc) 6.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); const image_choice_1 = require("../../../fixtures/image_choice"); const lodash_1 = require("lodash"); const NO_CATEGORY = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); delete NO_CATEGORY.category; const INVALID_CATEGORY = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); INVALID_CATEGORY.category = "invalid_category"; const NO_CORRECT_ANSWER = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); delete NO_CORRECT_ANSWER.correctAnswer; const INVALID_CORRECT_ANSWER = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); INVALID_CORRECT_ANSWER.correctAnswer = "invalid_correct_answer"; const EMPTY_CORRECT_ANSWER = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); EMPTY_CORRECT_ANSWER.correctAnswer = []; const INVALID_IMAGE_OPTION = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); INVALID_IMAGE_OPTION.correctAnswer = [{ thing: "invalid_image_option" }]; const NO_INCORRECT_ANSWER = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); delete NO_INCORRECT_ANSWER.incorrectAnswers; const EMPTY_INCORRECT_ANSWERs = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); EMPTY_INCORRECT_ANSWERs.incorrectAnswers = []; const INVALID_IMAGE_OPTIONS = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); INVALID_IMAGE_OPTIONS.incorrectAnswers = [["invalid_image_option"]]; const NO_QUESTION = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); delete NO_QUESTION.question; const QUESTION_NOT_OBJECT = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); QUESTION_NOT_OBJECT.question = "question"; const QUESTION_NO_TEXT = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); QUESTION_NO_TEXT.question = { image: "image" }; const QUESTION_TEXT_WRONG_TYPE = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); QUESTION_TEXT_WRONG_TYPE.question = { text: 123 }; const NO_TAGS = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); delete NO_TAGS.tags; const TAGS_NOT_ARRAY = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); TAGS_NOT_ARRAY.tags = "tags"; const TAG_NOT_STRING = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); TAG_NOT_STRING.tags = [123]; const NO_TYPE = (0, lodash_1.cloneDeep)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION); delete NO_TYPE.type; const TYPE_NOT_IMAGE_CHOICE = (0, lodash_1.cloneDeep)(image_choice_1.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(() => (0, index_1.assertIsImageChoiceQuestion)(image_choice_1.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(() => (0, index_1.assertIsImageChoiceQuestion)(question)).toThrow(expectedError); }); }); describe("isImageChoiceQuestion", () => { it("returns true for a valid image choice question", () => { expect((0, index_1.isImageChoiceQuestion)(image_choice_1.VALID_IMAGE_CHOICE_QUESTION)).toBe(true); }); it.each(IMAGE_CHOICE_TEST_CASES)("returns false when $case", ({ question }) => { expect((0, index_1.isImageChoiceQuestion)(question)).toBe(false); }); });