forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.test.ts
More file actions
415 lines (390 loc) · 16.5 KB
/
checkbox.test.ts
File metadata and controls
415 lines (390 loc) · 16.5 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import { ComponentType, type APICheckboxComponent } from 'discord-api-types/v10';
import { describe, test, expect } from 'vitest';
import { LabelBuilder } from '../../src';
import { CheckboxBuilder } from '../../src/components/checkbox/Checkbox';
import { CheckboxGroupBuilder } from '../../src/components/checkbox/CheckboxGroup';
import { CheckboxGroupOptionBuilder } from '../../src/components/checkbox/CheckboxGroupOption';
import { RadioGroupBuilder } from '../../src/components/checkbox/RadioGroup';
import { RadioGroupOptionBuilder } from '../../src/components/checkbox/RadioGroupOption';
const longStr = ':3'.repeat(5_000);
const fiveCheckboxOptions = [
new CheckboxGroupOptionBuilder().setLabel('Option 1').setValue('option_1'),
new CheckboxGroupOptionBuilder().setLabel('Option 2').setValue('option_2'),
new CheckboxGroupOptionBuilder().setLabel('Option 3').setValue('option_3'),
new CheckboxGroupOptionBuilder().setLabel('Option 4').setValue('option_4'),
new CheckboxGroupOptionBuilder().setLabel('Option 5').setValue('option_5'),
];
const elevenCheckboxOptions = [
new CheckboxGroupOptionBuilder().setLabel('Option 1').setValue('option_1'),
new CheckboxGroupOptionBuilder().setLabel('Option 2').setValue('option_2'),
new CheckboxGroupOptionBuilder().setLabel('Option 3').setValue('option_3'),
new CheckboxGroupOptionBuilder().setLabel('Option 4').setValue('option_4'),
new CheckboxGroupOptionBuilder().setLabel('Option 5').setValue('option_5'),
new CheckboxGroupOptionBuilder().setLabel('Option 6').setValue('option_6'),
new CheckboxGroupOptionBuilder().setLabel('Option 7').setValue('option_7'),
new CheckboxGroupOptionBuilder().setLabel('Option 8').setValue('option_8'),
new CheckboxGroupOptionBuilder().setLabel('Option 9').setValue('option_9'),
new CheckboxGroupOptionBuilder().setLabel('Option 10').setValue('option_10'),
new CheckboxGroupOptionBuilder().setLabel('Option 11').setValue('option_11'),
];
const fiveRadioOptions = [
new RadioGroupOptionBuilder().setLabel('Option 1').setValue('option_1'),
new RadioGroupOptionBuilder().setLabel('Option 2').setValue('option_2'),
new RadioGroupOptionBuilder().setLabel('Option 3').setValue('option_3'),
new RadioGroupOptionBuilder().setLabel('Option 4').setValue('option_4'),
new RadioGroupOptionBuilder().setLabel('Option 5').setValue('option_5'),
];
const elevenRadioOptions = [
new RadioGroupOptionBuilder().setLabel('Option 1').setValue('option_1'),
new RadioGroupOptionBuilder().setLabel('Option 2').setValue('option_2'),
new RadioGroupOptionBuilder().setLabel('Option 3').setValue('option_3'),
new RadioGroupOptionBuilder().setLabel('Option 4').setValue('option_4'),
new RadioGroupOptionBuilder().setLabel('Option 5').setValue('option_5'),
new RadioGroupOptionBuilder().setLabel('Option 6').setValue('option_6'),
new RadioGroupOptionBuilder().setLabel('Option 7').setValue('option_7'),
new RadioGroupOptionBuilder().setLabel('Option 8').setValue('option_8'),
new RadioGroupOptionBuilder().setLabel('Option 9').setValue('option_9'),
new RadioGroupOptionBuilder().setLabel('Option 10').setValue('option_10'),
new RadioGroupOptionBuilder().setLabel('Option 11').setValue('option_11'),
];
describe('Checkbox Components', () => {
describe('CheckboxBuilder', () => {
test('Valid builder does not throw.', () => {
expect(() => new CheckboxBuilder().setCustomId('checkbox').toJSON()).not.toThrowError();
expect(() => new CheckboxBuilder().setCustomId('checkbox').setDefault(true).toJSON()).not.toThrowError();
});
test('Invalid builder does throw.', () => {
expect(() => new CheckboxBuilder().toJSON()).toThrowError();
expect(() => new CheckboxBuilder().setDefault(true).toJSON()).toThrowError();
expect(() => new CheckboxBuilder().setCustomId(longStr).toJSON()).toThrowError();
});
test('API data equals toJSON().', () => {
const checkboxData = {
type: ComponentType.Checkbox,
custom_id: 'checkbox',
default: true,
} satisfies APICheckboxComponent;
expect(new CheckboxBuilder(checkboxData).toJSON()).toEqual(checkboxData);
expect(new CheckboxBuilder().setCustomId('checkbox').setDefault(true).toJSON()).toEqual(checkboxData);
});
});
describe('CheckboxGroupBuilder', () => {
test('Valid builder does not throw.', () => {
expect(() =>
new CheckboxGroupBuilder({
custom_id: 'checkbox_group',
options: fiveCheckboxOptions.map((option) => option.toJSON()),
}).toJSON(),
).not.toThrowError();
expect(() =>
new CheckboxGroupBuilder().setCustomId('checkbox_group').setOptions(fiveCheckboxOptions).toJSON(),
).not.toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions([
new CheckboxGroupOptionBuilder().setLabel('Option 1').setValue('option_1'),
new CheckboxGroupOptionBuilder().setLabel('Option 2').setValue('option_2'),
])
.toJSON(),
).not.toThrowError();
expect(() =>
new CheckboxGroupBuilder().setCustomId('checkbox_group').addOptions(fiveCheckboxOptions).toJSON(),
).not.toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setMinValues(1)
.setMaxValues(2)
.setOptions([
new CheckboxGroupOptionBuilder().setLabel('Option 1').setValue('option_1'),
new CheckboxGroupOptionBuilder().setLabel('Option 2').setValue('option_2'),
])
.toJSON(),
).not.toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(fiveCheckboxOptions)
.spliceOptions(2, 1, ...elevenCheckboxOptions.slice(7, 9))
.spliceOptions(0, 1, { label: 'New Option', value: 'new_option' }),
).not.toThrowError();
});
test('Invalid builder does throw.', () => {
expect(() => new CheckboxGroupBuilder().toJSON()).toThrowError();
expect(() => new CheckboxGroupBuilder().addOptions([]).toJSON()).toThrowError();
expect(() => new CheckboxGroupBuilder().setMinValues(2).setMaxValues(1).toJSON()).toThrowError();
expect(() =>
new CheckboxGroupBuilder().setMinValues(2).setMaxValues(1).addOptions(fiveCheckboxOptions).toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setMinValues(2)
.setMaxValues(1)
.addOptions(fiveCheckboxOptions)
.toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder().setCustomId('checkbox_group').setMinValues(5).setMaxValues(11).toJSON(),
).toThrowError();
expect(() => new CheckboxGroupBuilder().setCustomId('checkbox_group').setOptions([]).toJSON()).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(fiveCheckboxOptions)
.setMinValues(6)
.toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(fiveCheckboxOptions)
.setMaxValues(6)
.toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder().setCustomId('checkbox_group').setOptions(elevenCheckboxOptions).toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(elevenCheckboxOptions)
.setMaxValues(12)
.toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder().setCustomId(longStr).setOptions(fiveCheckboxOptions).toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(fiveCheckboxOptions)
.setMinValues(0)
.setRequired(true)
.toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setMaxValues(4)
.setOptions([
new CheckboxGroupOptionBuilder().setLabel('Option 1').setValue('option_1').setDefault(true),
new CheckboxGroupOptionBuilder().setLabel('Option 2').setValue('option_2').setDefault(true),
new CheckboxGroupOptionBuilder().setLabel('Option 3').setValue('option_3').setDefault(true),
new CheckboxGroupOptionBuilder().setLabel('Option 4').setValue('option_4').setDefault(true),
new CheckboxGroupOptionBuilder().setLabel('Option 5').setValue('option_5').setDefault(true),
])
.toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(fiveCheckboxOptions)
.addOptions(fiveCheckboxOptions)
.toJSON(),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(elevenCheckboxOptions.slice(0, 5))
.addOptions(elevenCheckboxOptions.slice(5, 11))
.toJSON(),
).toThrowError();
expect(
() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(fiveCheckboxOptions)
.spliceOptions(2, 1, new CheckboxGroupOptionBuilder().setLabel('Option 6')), // no value
).toThrowError();
expect(() =>
new CheckboxGroupBuilder()
.setCustomId('checkbox_group')
.setOptions(fiveCheckboxOptions)
.spliceOptions(2, 1, { value: 'hi', label: longStr }),
).toThrowError();
expect(() =>
new CheckboxGroupBuilder().setCustomId('checkbox_group').addOptions({ value: 'hi', label: longStr }),
).toThrowError();
});
});
describe('CheckboxGroupOptionBuilder', () => {
test('Valid builder does not throw.', () => {
expect(() =>
new CheckboxGroupOptionBuilder().setLabel('Option 1').setValue('option_1').toJSON(),
).not.toThrowError();
expect(() =>
new CheckboxGroupOptionBuilder()
.setLabel('Option 2')
.setValue('option_2')
.setDescription('This is option 2')
.toJSON(),
).not.toThrowError();
expect(() =>
new CheckboxGroupOptionBuilder().setLabel('Option 3').setValue('option_3').setDefault(true).toJSON(),
).not.toThrowError();
});
test('Invalid builder does throw.', () => {
expect(() => new CheckboxGroupOptionBuilder().toJSON()).toThrowError();
expect(() => new CheckboxGroupOptionBuilder().setValue('option_1').toJSON()).toThrowError();
expect(() => new CheckboxGroupOptionBuilder().setLabel('Option 1').toJSON()).toThrowError();
expect(() => new CheckboxGroupOptionBuilder().setLabel(longStr).setValue('option_1').toJSON()).toThrowError();
expect(() => new CheckboxGroupOptionBuilder().setLabel('Option 1').setValue(longStr).toJSON()).toThrowError();
});
test('toJSON returns correct data.', () => {
const option = new CheckboxGroupOptionBuilder()
.setLabel('Option 1')
.setValue('option_1')
.setDescription('This is option 1')
.setDefault(true);
expect(option.toJSON()).toEqual({
label: 'Option 1',
value: 'option_1',
description: 'This is option 1',
default: true,
});
});
});
describe('RadioGroupBuilder', () => {
test('Valid builder does not throw.', () => {
expect(() =>
new RadioGroupBuilder().setCustomId('radio_group').addOptions(fiveRadioOptions).toJSON(),
).not.toThrowError();
expect(() =>
new RadioGroupBuilder()
.setCustomId('radio_group')
.setOptions([
new RadioGroupOptionBuilder().setLabel('Option 1').setValue('option_1'),
new RadioGroupOptionBuilder().setLabel('Option 2').setValue('option_2'),
])
.toJSON(),
).not.toThrowError();
expect(() =>
new RadioGroupBuilder().setCustomId('radio_group').addOptions(fiveRadioOptions).setRequired(false),
).not.toThrowError();
expect(() =>
new RadioGroupBuilder().setCustomId('radio_group').addOptions(fiveRadioOptions).setRequired(true),
).not.toThrowError();
expect(() => new RadioGroupBuilder().setCustomId('radio_group').setOptions(fiveRadioOptions)).not.toThrowError();
expect(() =>
new RadioGroupBuilder()
.setCustomId('radio_group')
.setOptions(fiveRadioOptions)
.spliceOptions(2, 1, elevenRadioOptions.slice(7, 9)),
).not.toThrowError();
expect(() =>
new RadioGroupBuilder()
.setCustomId('radio_group')
.addOptions(elevenRadioOptions.slice(0, 5))
.spliceOptions(0, 1, { label: 'New Option', value: 'new_option' }),
).not.toThrowError();
expect(() =>
new RadioGroupBuilder({
custom_id: 'radio_group',
options: fiveRadioOptions.map((option) => option.toJSON()),
}).toJSON(),
).not.toThrowError();
expect(() =>
new RadioGroupBuilder()
.setCustomId('radio_group')
.addOptions(fiveRadioOptions.map((option) => option.toJSON()))
.toJSON(),
).not.toThrowError();
});
test('Invalid builder does throw.', () => {
expect(() => new RadioGroupBuilder().toJSON()).toThrowError();
expect(() => new RadioGroupBuilder().addOptions([]).toJSON()).toThrowError();
// needs at least 2 options
expect(() => new RadioGroupBuilder().addOptions([fiveRadioOptions[0]]).toJSON()).toThrowError();
expect(() =>
new RadioGroupBuilder().setCustomId('radio_group').setOptions([fiveRadioOptions[0]]).toJSON(),
).toThrowError();
expect(() => new RadioGroupBuilder().setCustomId('radio_group').setOptions([]).toJSON()).toThrowError();
expect(() =>
new RadioGroupBuilder().setCustomId('radio_group').setOptions(elevenRadioOptions).toJSON(),
).toThrowError();
expect(() => new RadioGroupBuilder().setCustomId(longStr).setOptions(fiveRadioOptions).toJSON()).toThrowError();
expect(() =>
new RadioGroupBuilder()
.setCustomId('radio_group')
.setOptions([
new RadioGroupOptionBuilder().setLabel('Option 1').setValue('option_1').setDefault(true),
new RadioGroupOptionBuilder().setLabel('Option 2').setValue('option_2').setDefault(true),
])
.toJSON(),
).toThrowError();
expect(() =>
new RadioGroupBuilder()
.setCustomId('radio_group')
.addOptions(fiveRadioOptions)
.addOptions(fiveRadioOptions)
.toJSON(),
).toThrowError();
expect(() =>
new RadioGroupBuilder()
.setCustomId('radio_group')
.setOptions(fiveRadioOptions)
.spliceOptions(2, 1, { value: 'hi', label: longStr }),
).toThrowError();
expect(() =>
new RadioGroupBuilder().setCustomId('radio_group').addOptions({ value: 'hi', label: longStr }),
).toThrowError();
});
});
describe('RadioGroupOptionBuilder', () => {
test('Valid builder does not throw.', () => {
expect(() => new RadioGroupOptionBuilder().setLabel('Option 1').setValue('option_1').toJSON()).not.toThrowError();
expect(() =>
new RadioGroupOptionBuilder()
.setLabel('Option 2')
.setValue('option_2')
.setDescription('This is option 2')
.toJSON(),
).not.toThrowError();
expect(() =>
new RadioGroupOptionBuilder().setLabel('Option 3').setValue('option_3').setDefault(true).toJSON(),
).not.toThrowError();
});
test('Invalid builder does throw.', () => {
expect(() => new RadioGroupOptionBuilder().toJSON()).toThrowError();
expect(() => new RadioGroupOptionBuilder().setValue('option_1').toJSON()).toThrowError();
expect(() => new RadioGroupOptionBuilder().setLabel('Option 1').toJSON()).toThrowError();
expect(() => new RadioGroupOptionBuilder().setLabel(longStr).setValue('option_1').toJSON()).toThrowError();
expect(() => new RadioGroupOptionBuilder().setLabel('Option 1').setValue(longStr).toJSON()).toThrowError();
});
test('toJSON returns correct data.', () => {
const option = new RadioGroupOptionBuilder()
.setLabel('Option 1')
.setValue('option_1')
.setDescription('This is option 1')
.setDefault(true);
expect(option.toJSON()).toEqual({
label: 'Option 1',
value: 'option_1',
description: 'This is option 1',
default: true,
});
});
});
});
describe('LabelBuilder with Checkbox Components', () => {
test('LabelBuilder can set Checkbox component.', () => {
const checkbox = new CheckboxBuilder().setCustomId('checkbox').setDefault(true);
const label = new LabelBuilder().setLabel('Checkbox Label').setCheckboxComponent(checkbox);
expect(() => label.toJSON()).not.toThrowError();
expect(label.toJSON().component).toEqual(checkbox.toJSON());
});
test('LabelBuilder can set CheckboxGroup component.', () => {
const checkboxGroup = new CheckboxGroupBuilder().setCustomId('checkbox_group').setOptions(fiveCheckboxOptions);
const label = new LabelBuilder().setLabel('Checkbox Group Label').setCheckboxGroupComponent(checkboxGroup);
expect(() => label.toJSON()).not.toThrowError();
expect(label.toJSON().component).toEqual(checkboxGroup.toJSON());
});
test('LabelBuilder can set RadioGroup component.', () => {
const radioGroup = new RadioGroupBuilder().setCustomId('radio_group').setOptions(fiveRadioOptions);
const label = new LabelBuilder().setLabel('Radio Group Label').setRadioGroupComponent(radioGroup);
expect(() => label.toJSON()).not.toThrowError();
expect(label.toJSON().component).toEqual(radioGroup.toJSON());
});
});