Skip to content

Commit 8162ae8

Browse files
test: Add toJSON() to tests (#11321)
test: fix validation Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent ace834b commit 8162ae8

File tree

5 files changed

+114
-59
lines changed

5 files changed

+114
-59
lines changed

packages/builders/__tests__/components/button.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const longStr =
1919
describe('Button Components', () => {
2020
describe('Assertion Tests', () => {
2121
test('GIVEN valid fields THEN builder does not throw', () => {
22-
expect(() => new PrimaryButtonBuilder().setCustomId('custom').setLabel('test')).not.toThrowError();
22+
expect(() => new PrimaryButtonBuilder().setCustomId('custom').setLabel('test').toJSON()).not.toThrowError();
2323

2424
expect(() => {
2525
const button = new PrimaryButtonBuilder()

packages/builders/__tests__/components/selectMenu.test.ts

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,49 @@ function mapStringSelectMenuOptionBuildersToJson(selectMenu: StringSelectMenuBui
5151
describe('Select Menu Components', () => {
5252
describe('Assertion Tests', () => {
5353
test('GIVEN valid inputs THEN Select Menu does not throw', () => {
54-
expect(() => selectMenu().setCustomId('foo')).not.toThrowError();
55-
expect(() => selectMenu().setMaxValues(10)).not.toThrowError();
56-
expect(() => selectMenu().setMinValues(3)).not.toThrowError();
57-
expect(() => selectMenu().setDisabled(true)).not.toThrowError();
58-
expect(() => selectMenu().setDisabled()).not.toThrowError();
59-
expect(() => selectMenu().setPlaceholder('description')).not.toThrowError();
54+
expect(() =>
55+
selectMenu().setCustomId('foo').addOptions({ label: 'test', value: 'test' }).toJSON(),
56+
).not.toThrowError();
57+
expect(() =>
58+
selectMenuWithId().setMaxValues(10).addOptions({ label: 'test', value: 'test' }).toJSON(),
59+
).not.toThrowError();
60+
expect(() =>
61+
selectMenuWithId()
62+
.setMinValues(3)
63+
.addOptions(
64+
{ label: 'test1', value: 'test1' },
65+
{ label: 'test2', value: 'test2' },
66+
{ label: 'test3', value: 'test3' },
67+
)
68+
.toJSON(),
69+
).not.toThrowError();
70+
expect(() =>
71+
selectMenuWithId().setDisabled(true).addOptions({ label: 'test', value: 'test' }).toJSON(),
72+
).not.toThrowError();
73+
expect(() =>
74+
selectMenuWithId().setDisabled().addOptions({ label: 'test', value: 'test' }).toJSON(),
75+
).not.toThrowError();
76+
expect(() =>
77+
selectMenuWithId().setPlaceholder('description').addOptions({ label: 'test', value: 'test' }).toJSON(),
78+
).not.toThrowError();
6079
const option = selectMenuOption()
6180
.setLabel('test')
6281
.setValue('test')
6382
.setDefault(true)
6483
.setEmoji({ name: 'test' })
6584
.setDescription('description');
66-
expect(() => selectMenu().addOptions(option)).not.toThrowError();
67-
expect(() => selectMenu().setOptions(option)).not.toThrowError();
68-
expect(() => selectMenu().setOptions({ label: 'test', value: 'test' })).not.toThrowError();
69-
expect(() => selectMenu().addOptions([option])).not.toThrowError();
70-
expect(() => selectMenu().setOptions([option])).not.toThrowError();
71-
expect(() => selectMenu().setOptions([{ label: 'test', value: 'test' }])).not.toThrowError();
85+
expect(() => selectMenuWithId().addOptions(option).toJSON()).not.toThrowError();
86+
expect(() => selectMenuWithId().setOptions(option).toJSON()).not.toThrowError();
87+
expect(() => selectMenuWithId().setOptions({ label: 'test', value: 'test' }).toJSON()).not.toThrowError();
88+
expect(() => selectMenuWithId().addOptions([option]).toJSON()).not.toThrowError();
89+
expect(() => selectMenuWithId().setOptions([option]).toJSON()).not.toThrowError();
7290
expect(() =>
73-
selectMenu()
91+
selectMenuWithId()
92+
.setOptions([{ label: 'test', value: 'test' }])
93+
.toJSON(),
94+
).not.toThrowError();
95+
expect(() =>
96+
selectMenuWithId()
7497
.addOptions({
7598
label: 'test',
7699
value: 'test',
@@ -90,26 +113,37 @@ describe('Select Menu Components', () => {
90113
animated: true,
91114
},
92115
},
93-
]),
116+
])
117+
.toJSON(),
94118
).not.toThrowError();
95119

96120
const options = Array.from<APISelectMenuOption>({ length: 25 }).fill({ label: 'test', value: 'test' });
97121

98-
expect(() => selectMenu().addOptions(...options)).not.toThrowError();
99-
expect(() => selectMenu().setOptions(...options)).not.toThrowError();
100-
expect(() => selectMenu().addOptions(options)).not.toThrowError();
101-
expect(() => selectMenu().setOptions(options)).not.toThrowError();
122+
expect(() =>
123+
selectMenuWithId()
124+
.addOptions(...options)
125+
.toJSON(),
126+
).not.toThrowError();
127+
expect(() =>
128+
selectMenuWithId()
129+
.setOptions(...options)
130+
.toJSON(),
131+
).not.toThrowError();
132+
expect(() => selectMenuWithId().addOptions(options).toJSON()).not.toThrowError();
133+
expect(() => selectMenuWithId().setOptions(options).toJSON()).not.toThrowError();
102134

103135
expect(() =>
104-
selectMenu()
136+
selectMenuWithId()
105137
.addOptions({ label: 'test', value: 'test' })
106138

107-
.addOptions(...Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' })),
139+
.addOptions(...Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' }))
140+
.toJSON(),
108141
).not.toThrowError();
109142
expect(() =>
110-
selectMenu()
143+
selectMenuWithId()
111144
.addOptions([{ label: 'test', value: 'test' }])
112-
.addOptions(Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' })),
145+
.addOptions(Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' }))
146+
.toJSON(),
113147
).not.toThrowError();
114148
});
115149

@@ -212,13 +246,17 @@ describe('Select Menu Components', () => {
212246

213247
test('GIVEN valid option types THEN does not throw', () => {
214248
expect(() =>
215-
selectMenu().addOptions({
216-
label: 'test',
217-
value: 'test',
218-
}),
249+
selectMenuWithId()
250+
.addOptions({
251+
label: 'test',
252+
value: 'test',
253+
})
254+
.toJSON(),
219255
).not.toThrowError();
220256

221-
expect(() => selectMenu().addOptions(selectMenuOption().setLabel('test').setValue('test'))).not.toThrowError();
257+
expect(() =>
258+
selectMenuWithId().addOptions(selectMenuOption().setLabel('test').setValue('test')).toJSON(),
259+
).not.toThrowError();
222260
});
223261

224262
test('GIVEN valid JSON input THEN valid JSON history is correct', () => {

packages/builders/__tests__/components/v2/container.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ const containerWithSeparatorDataNoColor: APIContainerComponent = {
6464
describe('Container Components', () => {
6565
describe('Assertion Tests', () => {
6666
test('GIVEN valid components THEN do not throw', () => {
67-
expect(() => new ContainerBuilder().addSeparatorComponents(new SeparatorBuilder())).not.toThrowError();
68-
expect(() => new ContainerBuilder().spliceComponents(0, 0, new SeparatorBuilder())).not.toThrowError();
69-
expect(() => new ContainerBuilder().addSeparatorComponents([new SeparatorBuilder()])).not.toThrowError();
67+
expect(() => new ContainerBuilder().addSeparatorComponents(new SeparatorBuilder()).toJSON()).not.toThrowError();
68+
expect(() => new ContainerBuilder().spliceComponents(0, 0, new SeparatorBuilder()).toJSON()).not.toThrowError();
69+
expect(() => new ContainerBuilder().addSeparatorComponents([new SeparatorBuilder()]).toJSON()).not.toThrowError();
7070
expect(() =>
71-
new ContainerBuilder().spliceComponents(0, 0, [{ type: ComponentType.Separator }]),
71+
new ContainerBuilder().spliceComponents(0, 0, [{ type: ComponentType.Separator }]).toJSON(),
7272
).not.toThrowError();
7373
});
7474

packages/builders/__tests__/interactions/ChatInputCommands/ChatInputCommands.test.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ describe('ChatInput Commands', () => {
237237
});
238238

239239
test('GIVEN valid names THEN does not throw error', () => {
240-
expect(() => getBuilder().setName('hi_there').setDescription(':3')).not.toThrowError();
241-
expect(() => getBuilder().setName('o_comandă').setDescription(':3')).not.toThrowError();
242-
expect(() => getBuilder().setName('どうも').setDescription(':3')).not.toThrowError();
240+
expect(() => getBuilder().setName('hi_there').setDescription(':3').toJSON()).not.toThrowError();
241+
expect(() => getBuilder().setName('o_comandă').setDescription(':3').toJSON()).not.toThrowError();
242+
expect(() => getBuilder().setName('どうも').setDescription(':3').toJSON()).not.toThrowError();
243243
});
244244

245245
test('GIVEN invalid returns for builder THEN throw error', () => {
@@ -384,8 +384,12 @@ describe('ChatInput Commands', () => {
384384
};
385385

386386
test('GIVEN valid name localizations THEN does not throw error', () => {
387-
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
388-
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
387+
expect(() => getNamedBuilder().setNameLocalization(Locale.EnglishUS, 'foobar').toJSON()).not.toThrowError();
388+
expect(() =>
389+
getNamedBuilder()
390+
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })
391+
.toJSON(),
392+
).not.toThrowError();
389393
});
390394

391395
test('GIVEN invalid name localizations THEN does throw error', () => {
@@ -451,19 +455,19 @@ describe('ChatInput Commands', () => {
451455

452456
describe('permissions', () => {
453457
test('GIVEN valid permission string THEN does not throw error', () => {
454-
expect(() => getNamedBuilder().setDefaultMemberPermissions('1')).not.toThrowError();
458+
expect(() => getNamedBuilder().setDefaultMemberPermissions('1').toJSON()).not.toThrowError();
455459
});
456460

457461
test('GIVEN valid permission bitfield THEN does not throw error', () => {
458462
expect(() =>
459-
getNamedBuilder().setDefaultMemberPermissions(
460-
PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles,
461-
),
463+
getNamedBuilder()
464+
.setDefaultMemberPermissions(PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles)
465+
.toJSON(),
462466
).not.toThrowError();
463467
});
464468

465469
test('GIVEN null permissions THEN does not throw error', () => {
466-
expect(() => getNamedBuilder().clearDefaultMemberPermissions()).not.toThrowError();
470+
expect(() => getNamedBuilder().clearDefaultMemberPermissions().toJSON()).not.toThrowError();
467471
});
468472

469473
test('GIVEN invalid inputs THEN does throw error', () => {
@@ -476,7 +480,7 @@ describe('ChatInput Commands', () => {
476480
getNamedBuilder().addBooleanOptions(getBooleanOption()).setDefaultMemberPermissions('1').toJSON(),
477481
).not.toThrowError();
478482

479-
expect(() => getNamedBuilder().addChannelOptions(getChannelOption())).not.toThrowError();
483+
expect(() => getNamedBuilder().addChannelOptions(getChannelOption()).toJSON()).not.toThrowError();
480484
});
481485
});
482486

packages/builders/__tests__/interactions/ContextMenuCommands.test.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ describe('Context Menu Commands', () => {
2121
expect(() => getBuilder().setName('A COMMAND').toJSON()).not.toThrowError();
2222

2323
// Translation: a_command
24-
expect(() => getBuilder().setName('o_comandă')).not.toThrowError();
24+
expect(() => getBuilder().setName('o_comandă').toJSON()).not.toThrowError();
2525

2626
// Translation: thx (according to GTranslate)
27-
expect(() => getBuilder().setName('どうも')).not.toThrowError();
27+
expect(() => getBuilder().setName('どうも').toJSON()).not.toThrowError();
2828

2929
expect(() => getBuilder().setName('🎉').toJSON()).not.toThrowError();
3030
expect(() => getBuilder().setName('🫆').toJSON()).not.toThrowError();
@@ -41,8 +41,15 @@ describe('Context Menu Commands', () => {
4141
};
4242

4343
test('GIVEN valid name localizations THEN does not throw error', () => {
44-
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
45-
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
44+
expect(() =>
45+
getBuilder().setName('test').setNameLocalization(Locale.EnglishUS, 'foobar').toJSON(),
46+
).not.toThrowError();
47+
expect(() =>
48+
getBuilder()
49+
.setName('test')
50+
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })
51+
.toJSON(),
52+
).not.toThrowError();
4653
});
4754

4855
test('GIVEN invalid name localizations THEN does throw error', () => {
@@ -71,12 +78,15 @@ describe('Context Menu Commands', () => {
7178

7279
describe('permissions', () => {
7380
test('GIVEN valid permission string THEN does not throw error', () => {
74-
expect(() => getBuilder().setDefaultMemberPermissions('1')).not.toThrowError();
81+
expect(() => getBuilder().setName('test').setDefaultMemberPermissions('1').toJSON()).not.toThrowError();
7582
});
7683

7784
test('GIVEN valid permission bitfield THEN does not throw error', () => {
7885
expect(() =>
79-
getBuilder().setDefaultMemberPermissions(PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles),
86+
getBuilder()
87+
.setName('test')
88+
.setDefaultMemberPermissions(PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles)
89+
.toJSON(),
8090
).not.toThrowError();
8191
});
8292

@@ -90,11 +100,14 @@ describe('Context Menu Commands', () => {
90100
describe('contexts', () => {
91101
test('GIVEN a builder with valid contexts THEN does not throw an error', () => {
92102
expect(() =>
93-
getBuilder().setContexts([InteractionContextType.Guild, InteractionContextType.BotDM]),
103+
getBuilder()
104+
.setName('test')
105+
.setContexts([InteractionContextType.Guild, InteractionContextType.BotDM])
106+
.toJSON(),
94107
).not.toThrowError();
95108

96109
expect(() =>
97-
getBuilder().setContexts(InteractionContextType.Guild, InteractionContextType.BotDM),
110+
getBuilder().setName('test').setContexts(InteractionContextType.Guild, InteractionContextType.BotDM).toJSON(),
98111
).not.toThrowError();
99112
});
100113

@@ -110,17 +123,17 @@ describe('Context Menu Commands', () => {
110123
describe('integration types', () => {
111124
test('GIVEN a builder with valid integration types THEN does not throw an error', () => {
112125
expect(() =>
113-
getBuilder().setIntegrationTypes([
114-
ApplicationIntegrationType.GuildInstall,
115-
ApplicationIntegrationType.UserInstall,
116-
]),
126+
getBuilder()
127+
.setName('test')
128+
.setIntegrationTypes([ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall])
129+
.toJSON(),
117130
).not.toThrowError();
118131

119132
expect(() =>
120-
getBuilder().setIntegrationTypes(
121-
ApplicationIntegrationType.GuildInstall,
122-
ApplicationIntegrationType.UserInstall,
123-
),
133+
getBuilder()
134+
.setName('test')
135+
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall)
136+
.toJSON(),
124137
).not.toThrowError();
125138
});
126139

0 commit comments

Comments
 (0)