forked from tilt-dev/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BulkApiButton.test.tsx
341 lines (295 loc) · 9.86 KB
/
BulkApiButton.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { render, screen, waitFor } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { AnalyticsAction, AnalyticsType } from "./analytics"
import {
cleanupMockAnalyticsCalls,
expectIncrs,
mockAnalyticsCalls,
nonAnalyticsCalls,
} from "./analytics_test_helpers"
import { ApiButtonToggleState, ApiButtonType } from "./ApiButton"
import {
getUIButtonDataFromCall,
mockUIButtonUpdates,
} from "./ApiButton.testhelpers"
import {
BulkApiButton,
canBulkButtonBeToggled,
canButtonBeToggled,
} from "./BulkApiButton"
import { BulkAction } from "./OverviewTableBulkActions"
import { flushPromises } from "./promise"
import { disableButton, oneUIButton } from "./testdata"
const NON_TOGGLE_BUTTON = oneUIButton({ componentID: "database" })
const DISABLE_BUTTON_DB = disableButton("database", true)
const DISABLE_BUTTON_FRONTEND = disableButton("frontend", true)
const ENABLE_BUTTON_BACKEND = disableButton("backend", false)
const TEST_UIBUTTONS = [
DISABLE_BUTTON_DB,
DISABLE_BUTTON_FRONTEND,
ENABLE_BUTTON_BACKEND,
]
describe("BulkApiButton", () => {
beforeEach(() => {
mockAnalyticsCalls()
mockUIButtonUpdates()
})
afterEach(() => {
cleanupMockAnalyticsCalls()
})
it("is disabled when there are no UIButtons", () => {
render(
<BulkApiButton
bulkAction={BulkAction.Disable}
buttonText="I cannot be clicked"
requiresConfirmation={false}
uiButtons={[]}
/>
)
const bulkButton = screen.getByLabelText("Trigger I cannot be clicked")
expect(bulkButton).toBeTruthy()
expect((bulkButton as HTMLButtonElement).disabled).toBe(true)
})
it("is disabled when there are no UIButtons that can be toggled to the target toggle state", () => {
render(
<BulkApiButton
bulkAction={BulkAction.Disable}
buttonText="I cannot be toggled that way"
requiresConfirmation={false}
targetToggleState={ApiButtonToggleState.On}
uiButtons={[DISABLE_BUTTON_DB]}
/>
)
const bulkButton = screen.getByLabelText(
"Trigger I cannot be toggled that way"
)
expect(bulkButton).toBeTruthy()
expect((bulkButton as HTMLButtonElement).disabled).toBe(true)
})
it("is enabled when there are UIButtons", () => {
render(
<BulkApiButton
bulkAction={BulkAction.Disable}
buttonText="Run lint"
requiresConfirmation={false}
uiButtons={[NON_TOGGLE_BUTTON]}
/>
)
const bulkButton = screen.getByLabelText("Trigger Run lint")
expect(bulkButton).toBeTruthy()
expect((bulkButton as HTMLButtonElement).disabled).toBe(false)
})
it("is enabled when there are UIButtons that can be toggled to the target toggle state", () => {
render(
<BulkApiButton
bulkAction={BulkAction.Disable}
buttonText="Enable resources"
requiresConfirmation={false}
targetToggleState={ApiButtonToggleState.On}
uiButtons={[DISABLE_BUTTON_DB, ENABLE_BUTTON_BACKEND]}
/>
)
const bulkButton = screen.getByLabelText("Trigger Enable resources")
expect(bulkButton).toBeTruthy()
expect((bulkButton as HTMLButtonElement).disabled).toBe(false)
})
describe("when it's clicked", () => {
let mockCallback: jest.Mock
beforeEach(async () => {
mockCallback = jest.fn()
render(
<BulkApiButton
bulkAction={BulkAction.Disable}
buttonText="Turn everything off"
onClickCallback={mockCallback}
requiresConfirmation={false}
targetToggleState={ApiButtonToggleState.Off}
uiButtons={TEST_UIBUTTONS}
/>
)
const bulkButton = screen.getByLabelText("Trigger Turn everything off")
userEvent.click(bulkButton)
// Wait for the async calls to complete
await waitFor(flushPromises)
})
it("triggers all buttons that can be toggled when it's clicked", () => {
const buttonUpdateCalls = nonAnalyticsCalls()
// Out of the three test buttons, only two of them can be toggled to the target toggle state
expect(buttonUpdateCalls.length).toBe(2)
const buttonUpdateNames = buttonUpdateCalls.map(
(call) => getUIButtonDataFromCall(call)?.metadata?.name
)
expect(buttonUpdateNames).toStrictEqual([
DISABLE_BUTTON_DB.metadata?.name,
DISABLE_BUTTON_FRONTEND.metadata?.name,
])
})
it("generates the correct analytics payload", () => {
expectIncrs({
name: "ui.web.bulkButton",
tags: {
action: AnalyticsAction.Click,
bulkAction: BulkAction.Disable,
bulkCount: "3",
toggleValue: ApiButtonToggleState.On,
component: ApiButtonType.Global,
type: AnalyticsType.Grid,
},
})
})
it("calls a specified onClick callback", () => {
expect(mockCallback).toHaveBeenCalledTimes(1)
})
})
describe("when it requires confirmation", () => {
beforeEach(async () => {
render(
<BulkApiButton
bulkAction={BulkAction.Disable}
buttonText="Click everything when I'm sure"
requiresConfirmation={true}
uiButtons={TEST_UIBUTTONS}
/>
)
const bulkButton = screen.getByLabelText(
"Trigger Click everything when I'm sure"
)
userEvent.click(bulkButton)
})
it("displays confirm and cancel buttons when clicked once", () => {
expect(
screen.getByLabelText("Confirm Click everything when I'm sure")
).toBeTruthy()
expect(
screen.getByLabelText("Cancel Click everything when I'm sure")
).toBeTruthy()
})
it("triggers all buttons when `Confirm` is clicked", async () => {
userEvent.click(
screen.getByLabelText("Confirm Click everything when I'm sure")
)
await waitFor(flushPromises)
const buttonUpdateCalls = nonAnalyticsCalls()
expect(buttonUpdateCalls.length).toBe(3)
const buttonUpdateNames = buttonUpdateCalls.map(
(call) => getUIButtonDataFromCall(call)?.metadata?.name
)
expect(buttonUpdateNames).toStrictEqual([
DISABLE_BUTTON_DB.metadata?.name,
DISABLE_BUTTON_FRONTEND.metadata?.name,
ENABLE_BUTTON_BACKEND.metadata?.name,
])
})
it("does NOT trigger any buttons when `Cancel` is clicked", async () => {
userEvent.click(
screen.getByLabelText("Cancel Click everything when I'm sure")
)
// There shouldn't be any async calls made when canceling, but wait in case
await waitFor(flushPromises)
expect(
screen.getByLabelText("Trigger Click everything when I'm sure")
).toBeTruthy()
})
it("generates the correct analytics payload for confirmation", async () => {
userEvent.click(
screen.getByLabelText("Confirm Click everything when I'm sure")
)
await waitFor(flushPromises)
expectIncrs(
{
name: "ui.web.bulkButton",
tags: {
action: AnalyticsAction.Click,
bulkAction: BulkAction.Disable,
bulkCount: "3",
component: ApiButtonType.Global,
type: AnalyticsType.Grid,
},
},
{
name: "ui.web.bulkButton",
tags: {
action: AnalyticsAction.Click,
bulkAction: BulkAction.Disable,
bulkCount: "3",
confirm: "true",
component: ApiButtonType.Global,
type: AnalyticsType.Grid,
},
}
)
})
it("generates the correct analytics payload for cancelation", () => {
userEvent.click(
screen.getByLabelText("Cancel Click everything when I'm sure")
)
expectIncrs(
{
name: "ui.web.bulkButton",
tags: {
action: AnalyticsAction.Click,
bulkAction: BulkAction.Disable,
bulkCount: "3",
component: ApiButtonType.Global,
type: AnalyticsType.Grid,
},
},
{
name: "ui.web.bulkButton",
tags: {
action: AnalyticsAction.Click,
bulkAction: BulkAction.Disable,
bulkCount: "3",
confirm: "false",
component: ApiButtonType.Global,
type: AnalyticsType.Grid,
},
}
)
})
})
describe("helpers", () => {
describe("canButtonBeToggled", () => {
it("returns true when there is no target toggle state", () => {
expect(canButtonBeToggled(DISABLE_BUTTON_FRONTEND)).toBe(true)
})
it("returns false when button is not a toggle button", () => {
expect(canButtonBeToggled(NON_TOGGLE_BUTTON)).toBe(false)
})
it("returns false when button is already in the target toggle state", () => {
expect(
canButtonBeToggled(DISABLE_BUTTON_FRONTEND, ApiButtonToggleState.On)
).toBe(false)
expect(
canButtonBeToggled(ENABLE_BUTTON_BACKEND, ApiButtonToggleState.Off)
).toBe(false)
})
it("returns true when button is not in the target toggle state", () => {
expect(
canButtonBeToggled(DISABLE_BUTTON_FRONTEND, ApiButtonToggleState.Off)
).toBe(true)
expect(
canButtonBeToggled(ENABLE_BUTTON_BACKEND, ApiButtonToggleState.On)
).toBe(true)
})
})
describe("canBulkButtonBeToggled", () => {
it("returns false if no buttons in the list of buttons can be toggled", () => {
expect(
canBulkButtonBeToggled(
[NON_TOGGLE_BUTTON, DISABLE_BUTTON_FRONTEND],
ApiButtonToggleState.On
)
).toBe(false)
})
it("returns true if at least one button in the list of buttons can be toggled", () => {
expect(
canBulkButtonBeToggled(
[NON_TOGGLE_BUTTON, DISABLE_BUTTON_FRONTEND, ENABLE_BUTTON_BACKEND],
ApiButtonToggleState.On
)
).toBe(true)
})
})
})
})