-
Notifications
You must be signed in to change notification settings - Fork 1
/
useQuestions.test.tsx
130 lines (121 loc) · 3.41 KB
/
useQuestions.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
jest.mock("axios");
import { renderHook } from "@testing-library/react-hooks";
import axios from "axios";
import { observable, when } from "mobx";
import React, { FC } from "react";
import { mocked } from "ts-jest/utils";
import config from "../config";
import { Question } from "../store/question";
import { QuizStore } from "../store/quiz";
import { QuizContext } from "../store/QuizProvider";
import { useQuestions } from "./useQuestions";
const mockedAxios = mocked(axios, true);
const useQuestionsMock = {
data: {
results: [
{
category: "Entertainment: Video Games",
type: "multiple",
difficulty: "easy",
question: "Which of these is NOT a game under the Worms series?",
correct_answer: "Major Malfunction",
incorrect_answers: ["Crazy Golf", "Clan Wars", "Ultimate Mayhem"],
},
{
category: "Entertainment: Video Games",
type: "multiple",
difficulty: "easy",
question: "What ingredients are required to make a cake in Minecraft?",
correct_answer: "Milk, Sugar, Egg, and Wheat",
incorrect_answers: [
"Milk, Bread, Egg, and Wheat",
"Milk, Sugar Cane, Egg, and Wheat",
"Milk, Sugar, and Wheat",
],
},
],
},
};
const hookWrapper = (mockStore: any): FC => ({ children }) => {
return (
<QuizContext.Provider value={mockStore as QuizStore}>
{children}
</QuizContext.Provider>
);
};
beforeEach(() => {
mockedAxios.get.mockResolvedValue(useQuestionsMock);
});
test("sets loading to true", async () => {
const mockStore = observable({
loading: false,
});
renderHook(() => useQuestions(), { wrapper: hookWrapper(mockStore) });
// TODO: rather self-evident! But sets up the pattern.
when(
() => mockStore.loading,
() => expect(mockStore.loading).toBe(true)
);
});
test("sets error if categoryId 0", async () => {
const mockStore = observable({
error: undefined,
});
renderHook(() => useQuestions(), { wrapper: hookWrapper(mockStore) });
when(
() => !!mockStore.error,
() =>
expect(mockStore.error).toMatchInlineSnapshot(
`[Error: No category provided.]`
)
);
});
test("sets error if quantity < 1", async () => {
const mockStore = observable({
categoryId: 1,
error: undefined,
quantity: 0,
});
renderHook(() => useQuestions(), { wrapper: hookWrapper(mockStore) });
when(
() => !!mockStore.error,
() =>
expect(mockStore.error).toMatchInlineSnapshot(
`[Error: Have to answer at least one question.]`
)
);
});
test("sets error if quantity greater than maxQuestions", async () => {
const mockStore = observable({
categoryId: 1,
error: undefined,
quantity: config.maxQuestions + 1,
});
renderHook(() => useQuestions(), {
wrapper: hookWrapper(mockStore),
});
when(
() => !!mockStore.error,
() =>
expect(mockStore.error).toMatchInlineSnapshot(
"[Error: Sorry, you can only have 10 in a quiz.]"
)
);
});
test("sets an array of questions", async () => {
const mockStore = observable({
categoryId: 1,
quantity: config.maxQuestions + 1,
questions: observable<Question>([]),
});
renderHook(() => useQuestions(), {
wrapper: hookWrapper(mockStore),
});
when(
() => !!mockStore.questions.length,
() =>
expect(mockStore.questions).toHaveLength(
useQuestionsMock.data.results.length
)
);
});