-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
CharInvCompleted.test.tsx
167 lines (152 loc) · 5.08 KB
/
CharInvCompleted.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Provider } from "react-redux";
import {
type ReactTestInstance,
type ReactTestRenderer,
act,
create,
} from "react-test-renderer";
import configureMockStore from "redux-mock-store";
import WordCard from "components/WordCard";
import CharInvCompleted, {
CharChange,
CharInvChangesGoalList,
CharInvCompletedId,
} from "goals/CharacterInventory/CharInvCompleted";
import {
type CharInvChanges,
type CharacterChange,
CharacterStatus,
type FindAndReplaceChange,
defaultCharInvChanges,
} from "goals/CharacterInventory/CharacterInventoryTypes";
import { defaultState } from "goals/Redux/GoalReduxTypes";
import { type StoreState } from "types";
import { newWord as mockWord } from "types/word";
jest.mock("backend", () => ({
areInFrontier: (ids: string[]) => Promise.resolve(ids),
getWord: () => Promise.resolve(mockWord()),
updateWord: () => jest.fn(),
}));
const mockCharChanges: CharacterChange[] = [
["a", CharacterStatus.Accepted, CharacterStatus.Rejected],
["b", CharacterStatus.Accepted, CharacterStatus.Undecided],
["c", CharacterStatus.Rejected, CharacterStatus.Accepted],
["d", CharacterStatus.Rejected, CharacterStatus.Undecided],
["e", CharacterStatus.Undecided, CharacterStatus.Accepted],
["f", CharacterStatus.Undecided, CharacterStatus.Rejected],
];
const mockWordKeys = ["oldA", "oldB", "oldC"];
const mockWordChanges: FindAndReplaceChange = {
find: "Q",
replace: "q",
words: {
[mockWordKeys[0]]: "newA",
[mockWordKeys[1]]: "newB",
[mockWordKeys[2]]: "newC",
},
};
const mockState = (changes?: CharInvChanges): Partial<StoreState> => ({
goalsState: {
...defaultState,
currentGoal: { ...defaultState.currentGoal, changes },
},
});
let renderer: ReactTestRenderer;
let root: ReactTestInstance;
beforeEach(() => {
jest.resetAllMocks();
});
describe("CharInvCompleted", () => {
const renderCharInvCompleted = async (
changes?: CharInvChanges
): Promise<void> => {
await act(async () => {
renderer = create(
<Provider store={configureMockStore()(mockState(changes))}>
<CharInvCompleted />
</Provider>
);
});
root = renderer.root;
};
it("renders all char inv changes", async () => {
await renderCharInvCompleted({
...defaultCharInvChanges,
charChanges: mockCharChanges,
});
expect(root.findAllByType(CharChange)).toHaveLength(mockCharChanges.length);
expect(root.findAllByType(WordCard)).toHaveLength(0);
expect(() =>
root.findByProps({ id: CharInvCompletedId.TypographyNoCharChanges })
).toThrow();
root.findByProps({ id: CharInvCompletedId.TypographyNoWordChanges });
expect(() =>
root.findByProps({ id: CharInvCompletedId.TypographyWordChanges })
).toThrow();
});
it("renders all words changed", async () => {
await renderCharInvCompleted({
...defaultCharInvChanges,
wordChanges: [mockWordChanges],
});
expect(root.findAllByType(CharChange)).toHaveLength(0);
expect(renderer.root.findAllByType(WordCard)).toHaveLength(
mockWordKeys.length
);
root.findByProps({ id: CharInvCompletedId.TypographyNoCharChanges });
expect(() =>
root.findByProps({ id: CharInvCompletedId.TypographyNoWordChanges })
).toThrow();
root.findByProps({ id: CharInvCompletedId.TypographyWordChanges });
});
});
describe("CharInvChangesGoalList", () => {
const renderCharInvChangesGoalList = async (
changes?: CharInvChanges
): Promise<void> => {
await act(async () => {
renderer = create(
CharInvChangesGoalList(changes ?? defaultCharInvChanges)
);
});
root = renderer.root;
};
it("renders up to 3 char changes", async () => {
const changes = (count: number): CharInvChanges => ({
...defaultCharInvChanges,
charChanges: mockCharChanges.slice(0, count),
});
await renderCharInvChangesGoalList(changes(0));
expect(root.findAllByType(CharChange)).toHaveLength(0);
await renderCharInvChangesGoalList(changes(1));
expect(root.findAllByType(CharChange)).toHaveLength(1);
await renderCharInvChangesGoalList(changes(3));
expect(root.findAllByType(CharChange)).toHaveLength(3);
});
it("doesn't render more than 3 char changes", async () => {
await renderCharInvChangesGoalList({
...defaultCharInvChanges,
charChanges: mockCharChanges,
});
expect(root.findAllByType(CharChange)).toHaveLength(2);
});
it("doesn't show word changes when there are none", async () => {
await renderCharInvChangesGoalList(defaultCharInvChanges);
expect(() =>
root.findByProps({ id: CharInvCompletedId.TypographyNoWordChanges })
).toThrow();
expect(() =>
root.findByProps({ id: CharInvCompletedId.TypographyWordChanges })
).toThrow();
});
it("shows word changes when there are some", async () => {
await renderCharInvChangesGoalList({
...defaultCharInvChanges,
wordChanges: [mockWordChanges],
});
expect(() =>
root.findByProps({ id: CharInvCompletedId.TypographyNoWordChanges })
).toThrow();
root.findByProps({ id: CharInvCompletedId.TypographyWordChanges });
});
});