-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLearningPathDetails.test.ts
203 lines (185 loc) · 6.18 KB
/
LearningPathDetails.test.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { faker } from "@faker-js/faker/locale/en"
import { factories, urls } from "api/test-utils"
import type {
LearningPathResource,
PaginatedLearningPathRelationshipList,
} from "api"
import { manageListDialogs } from "./ManageListDialogs"
import ItemsListing from "./ItemsListing"
import { learningPathsView } from "../urls"
import {
screen,
renderTestApp,
setMockResponse,
user,
waitFor,
act,
expectLastProps,
} from "../../test-utils"
import { User } from "../../types/settings"
import { ControlledPromise } from "ol-util/test-utils"
import invariant from "tiny-invariant"
jest.mock("./ItemsListing", () => {
const actual = jest.requireActual("./ItemsListing")
return {
__esModule: true,
...actual,
default: jest.fn(actual.default),
}
})
const spyItemsListing = jest.mocked(ItemsListing)
/**
* Set up the mock API responses for lists page.
*/
const setup = ({
userSettings,
path,
}: {
userSettings?: Partial<User>
path: LearningPathResource
}) => {
invariant(path.learning_path, "Must pass a learning path")
const paginatedRelationships =
factories.learningResources.learningPathRelationships({
count: path.learning_path.item_count,
parent: path.id,
})
const detailsUrl = urls.learningPaths.details({ id: path.id })
const pathResourcesUrl = urls.learningPaths.resources({ parent_id: path.id })
setMockResponse.get(detailsUrl, path)
setMockResponse.get(pathResourcesUrl, paginatedRelationships)
const { history, queryClient } = renderTestApp({
user: userSettings,
url: learningPathsView(path.id),
})
return {
history,
paginatedRelationships,
queryClient,
pathResourcesUrl,
detailsUrl,
}
}
describe("LearningPathDetailsPage", () => {
it("Renders list title", async () => {
const path = factories.learningResources.learningPath()
setup({ path })
await screen.findByRole("heading", { name: path.title })
await waitFor(() => expect(document.title).toBe(path.title))
})
test.each([
{
userSettings: { is_learning_path_editor: false },
canEdit: false,
},
{
userSettings: { is_learning_path_editor: true },
canEdit: true,
},
])(
"Users can edit/reorder if and only if is_learning_path_editor=true",
async ({ userSettings, canEdit }) => {
const path = factories.learningResources.learningPath()
setup({ path, userSettings })
await screen.findByRole("heading", { name: path.title })
const editButton = screen.queryByRole("button", { name: "Edit" })
expect(!!editButton).toBe(canEdit)
const reorderButton = screen.queryByRole("button", { name: "Reorder" })
expect(!!reorderButton).toBe(canEdit)
},
)
test("Clicking reorder makes items reorderable, clicking Done makes them static", async () => {
const path = factories.learningResources.learningPath()
setup({ path, userSettings: { is_learning_path_editor: true } })
const reorderButton = await screen.findByRole("button", {
name: "Reorder",
})
expectLastProps(spyItemsListing, { sortable: false })
await user.click(reorderButton)
expectLastProps(spyItemsListing, { sortable: true })
expect(reorderButton).toHaveAccessibleName("Done ordering")
await user.click(reorderButton)
expectLastProps(spyItemsListing, { sortable: false })
})
it.each([
{
itemCount: 0,
canReorder: false,
},
{
itemCount: faker.datatype.number({ min: 1, max: 10 }),
canReorder: true,
},
])(
"Shows 'Reorder' button if and only not empty",
async ({ itemCount, canReorder }) => {
const path = factories.learningResources.learningPath(
{},
{
item_count: itemCount,
},
)
setup({ path, userSettings: { is_learning_path_editor: true } })
await screen.findByRole("heading", { name: path.title })
const reorderButton = screen.queryByRole("button", { name: "Reorder" })
expect(!!reorderButton).toBe(canReorder)
},
)
test("Edit buttons opens editing dialog", async () => {
const path = factories.learningResources.learningPath()
setup({ path, userSettings: { is_learning_path_editor: true } })
const editButton = await screen.findByRole("button", { name: "Edit" })
const editList = jest.spyOn(manageListDialogs, "upsert")
editList.mockImplementationOnce(jest.fn())
expect(editList).not.toHaveBeenCalled()
await user.click(editButton)
expect(editList).toHaveBeenCalledWith(path)
})
test("Passes appropriate props to ItemsListing", async () => {
const path = factories.learningResources.learningPath()
const { paginatedRelationships } = setup({ path })
expectLastProps(spyItemsListing, {
isLoading: true,
items: [],
emptyMessage: "There are no items in this list yet.",
})
await waitFor(() => {
expectLastProps(spyItemsListing, {
// sortable is tested elsewhere
isLoading: false,
items: paginatedRelationships.results,
emptyMessage: "There are no items in this list yet.",
})
})
})
test("Passes isRefetching=true to ItemsList while reloading data", async () => {
const path = factories.learningResources.learningPath()
const { paginatedRelationships, queryClient, pathResourcesUrl } = setup({
path,
})
await waitFor(() => expectLastProps(spyItemsListing, { isLoading: false }))
expectLastProps(spyItemsListing, { isRefetching: false })
spyItemsListing.mockClear()
const response =
new ControlledPromise<PaginatedLearningPathRelationshipList>()
setMockResponse.get(pathResourcesUrl, response)
spyItemsListing.mockClear()
// invalidate the cache entries and check that isRefetching is true
act(() => {
queryClient.invalidateQueries()
})
// Wait till everything has settled except our ControlledPromise
await waitFor(() => expect(queryClient.isFetching()).toBe(1))
await waitFor(() =>
expectLastProps(spyItemsListing, { isRefetching: true }),
)
spyItemsListing.mockClear()
await act(async () => {
response.resolve(paginatedRelationships)
await response
})
await waitFor(() =>
expectLastProps(spyItemsListing, { isRefetching: false }),
)
})
})