mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
test: Add toJSON() to tests (#11321)
test: fix validation Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -19,7 +19,7 @@ const longStr =
|
||||
describe('Button Components', () => {
|
||||
describe('Assertion Tests', () => {
|
||||
test('GIVEN valid fields THEN builder does not throw', () => {
|
||||
expect(() => new PrimaryButtonBuilder().setCustomId('custom').setLabel('test')).not.toThrowError();
|
||||
expect(() => new PrimaryButtonBuilder().setCustomId('custom').setLabel('test').toJSON()).not.toThrowError();
|
||||
|
||||
expect(() => {
|
||||
const button = new PrimaryButtonBuilder()
|
||||
|
||||
@@ -51,26 +51,49 @@ function mapStringSelectMenuOptionBuildersToJson(selectMenu: StringSelectMenuBui
|
||||
describe('Select Menu Components', () => {
|
||||
describe('Assertion Tests', () => {
|
||||
test('GIVEN valid inputs THEN Select Menu does not throw', () => {
|
||||
expect(() => selectMenu().setCustomId('foo')).not.toThrowError();
|
||||
expect(() => selectMenu().setMaxValues(10)).not.toThrowError();
|
||||
expect(() => selectMenu().setMinValues(3)).not.toThrowError();
|
||||
expect(() => selectMenu().setDisabled(true)).not.toThrowError();
|
||||
expect(() => selectMenu().setDisabled()).not.toThrowError();
|
||||
expect(() => selectMenu().setPlaceholder('description')).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenu().setCustomId('foo').addOptions({ label: 'test', value: 'test' }).toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId().setMaxValues(10).addOptions({ label: 'test', value: 'test' }).toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId()
|
||||
.setMinValues(3)
|
||||
.addOptions(
|
||||
{ label: 'test1', value: 'test1' },
|
||||
{ label: 'test2', value: 'test2' },
|
||||
{ label: 'test3', value: 'test3' },
|
||||
)
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId().setDisabled(true).addOptions({ label: 'test', value: 'test' }).toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId().setDisabled().addOptions({ label: 'test', value: 'test' }).toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId().setPlaceholder('description').addOptions({ label: 'test', value: 'test' }).toJSON(),
|
||||
).not.toThrowError();
|
||||
const option = selectMenuOption()
|
||||
.setLabel('test')
|
||||
.setValue('test')
|
||||
.setDefault(true)
|
||||
.setEmoji({ name: 'test' })
|
||||
.setDescription('description');
|
||||
expect(() => selectMenu().addOptions(option)).not.toThrowError();
|
||||
expect(() => selectMenu().setOptions(option)).not.toThrowError();
|
||||
expect(() => selectMenu().setOptions({ label: 'test', value: 'test' })).not.toThrowError();
|
||||
expect(() => selectMenu().addOptions([option])).not.toThrowError();
|
||||
expect(() => selectMenu().setOptions([option])).not.toThrowError();
|
||||
expect(() => selectMenu().setOptions([{ label: 'test', value: 'test' }])).not.toThrowError();
|
||||
expect(() => selectMenuWithId().addOptions(option).toJSON()).not.toThrowError();
|
||||
expect(() => selectMenuWithId().setOptions(option).toJSON()).not.toThrowError();
|
||||
expect(() => selectMenuWithId().setOptions({ label: 'test', value: 'test' }).toJSON()).not.toThrowError();
|
||||
expect(() => selectMenuWithId().addOptions([option]).toJSON()).not.toThrowError();
|
||||
expect(() => selectMenuWithId().setOptions([option]).toJSON()).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenu()
|
||||
selectMenuWithId()
|
||||
.setOptions([{ label: 'test', value: 'test' }])
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId()
|
||||
.addOptions({
|
||||
label: 'test',
|
||||
value: 'test',
|
||||
@@ -90,26 +113,37 @@ describe('Select Menu Components', () => {
|
||||
animated: true,
|
||||
},
|
||||
},
|
||||
]),
|
||||
])
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
|
||||
const options = Array.from<APISelectMenuOption>({ length: 25 }).fill({ label: 'test', value: 'test' });
|
||||
|
||||
expect(() => selectMenu().addOptions(...options)).not.toThrowError();
|
||||
expect(() => selectMenu().setOptions(...options)).not.toThrowError();
|
||||
expect(() => selectMenu().addOptions(options)).not.toThrowError();
|
||||
expect(() => selectMenu().setOptions(options)).not.toThrowError();
|
||||
|
||||
expect(() =>
|
||||
selectMenu()
|
||||
.addOptions({ label: 'test', value: 'test' })
|
||||
|
||||
.addOptions(...Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' })),
|
||||
selectMenuWithId()
|
||||
.addOptions(...options)
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenu()
|
||||
selectMenuWithId()
|
||||
.setOptions(...options)
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() => selectMenuWithId().addOptions(options).toJSON()).not.toThrowError();
|
||||
expect(() => selectMenuWithId().setOptions(options).toJSON()).not.toThrowError();
|
||||
|
||||
expect(() =>
|
||||
selectMenuWithId()
|
||||
.addOptions({ label: 'test', value: 'test' })
|
||||
|
||||
.addOptions(...Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' }))
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId()
|
||||
.addOptions([{ label: 'test', value: 'test' }])
|
||||
.addOptions(Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' })),
|
||||
.addOptions(Array.from<APISelectMenuOption>({ length: 24 }).fill({ label: 'test', value: 'test' }))
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
@@ -212,13 +246,17 @@ describe('Select Menu Components', () => {
|
||||
|
||||
test('GIVEN valid option types THEN does not throw', () => {
|
||||
expect(() =>
|
||||
selectMenu().addOptions({
|
||||
label: 'test',
|
||||
value: 'test',
|
||||
}),
|
||||
selectMenuWithId()
|
||||
.addOptions({
|
||||
label: 'test',
|
||||
value: 'test',
|
||||
})
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
|
||||
expect(() => selectMenu().addOptions(selectMenuOption().setLabel('test').setValue('test'))).not.toThrowError();
|
||||
expect(() =>
|
||||
selectMenuWithId().addOptions(selectMenuOption().setLabel('test').setValue('test')).toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN valid JSON input THEN valid JSON history is correct', () => {
|
||||
|
||||
@@ -64,11 +64,11 @@ const containerWithSeparatorDataNoColor: APIContainerComponent = {
|
||||
describe('Container Components', () => {
|
||||
describe('Assertion Tests', () => {
|
||||
test('GIVEN valid components THEN do not throw', () => {
|
||||
expect(() => new ContainerBuilder().addSeparatorComponents(new SeparatorBuilder())).not.toThrowError();
|
||||
expect(() => new ContainerBuilder().spliceComponents(0, 0, new SeparatorBuilder())).not.toThrowError();
|
||||
expect(() => new ContainerBuilder().addSeparatorComponents([new SeparatorBuilder()])).not.toThrowError();
|
||||
expect(() => new ContainerBuilder().addSeparatorComponents(new SeparatorBuilder()).toJSON()).not.toThrowError();
|
||||
expect(() => new ContainerBuilder().spliceComponents(0, 0, new SeparatorBuilder()).toJSON()).not.toThrowError();
|
||||
expect(() => new ContainerBuilder().addSeparatorComponents([new SeparatorBuilder()]).toJSON()).not.toThrowError();
|
||||
expect(() =>
|
||||
new ContainerBuilder().spliceComponents(0, 0, [{ type: ComponentType.Separator }]),
|
||||
new ContainerBuilder().spliceComponents(0, 0, [{ type: ComponentType.Separator }]).toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
|
||||
@@ -237,9 +237,9 @@ describe('ChatInput Commands', () => {
|
||||
});
|
||||
|
||||
test('GIVEN valid names THEN does not throw error', () => {
|
||||
expect(() => getBuilder().setName('hi_there').setDescription(':3')).not.toThrowError();
|
||||
expect(() => getBuilder().setName('o_comandă').setDescription(':3')).not.toThrowError();
|
||||
expect(() => getBuilder().setName('どうも').setDescription(':3')).not.toThrowError();
|
||||
expect(() => getBuilder().setName('hi_there').setDescription(':3').toJSON()).not.toThrowError();
|
||||
expect(() => getBuilder().setName('o_comandă').setDescription(':3').toJSON()).not.toThrowError();
|
||||
expect(() => getBuilder().setName('どうも').setDescription(':3').toJSON()).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN invalid returns for builder THEN throw error', () => {
|
||||
@@ -384,8 +384,12 @@ describe('ChatInput Commands', () => {
|
||||
};
|
||||
|
||||
test('GIVEN valid name localizations THEN does not throw error', () => {
|
||||
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
|
||||
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
|
||||
expect(() => getNamedBuilder().setNameLocalization(Locale.EnglishUS, 'foobar').toJSON()).not.toThrowError();
|
||||
expect(() =>
|
||||
getNamedBuilder()
|
||||
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN invalid name localizations THEN does throw error', () => {
|
||||
@@ -451,19 +455,19 @@ describe('ChatInput Commands', () => {
|
||||
|
||||
describe('permissions', () => {
|
||||
test('GIVEN valid permission string THEN does not throw error', () => {
|
||||
expect(() => getNamedBuilder().setDefaultMemberPermissions('1')).not.toThrowError();
|
||||
expect(() => getNamedBuilder().setDefaultMemberPermissions('1').toJSON()).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN valid permission bitfield THEN does not throw error', () => {
|
||||
expect(() =>
|
||||
getNamedBuilder().setDefaultMemberPermissions(
|
||||
PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles,
|
||||
),
|
||||
getNamedBuilder()
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles)
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN null permissions THEN does not throw error', () => {
|
||||
expect(() => getNamedBuilder().clearDefaultMemberPermissions()).not.toThrowError();
|
||||
expect(() => getNamedBuilder().clearDefaultMemberPermissions().toJSON()).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN invalid inputs THEN does throw error', () => {
|
||||
@@ -476,7 +480,7 @@ describe('ChatInput Commands', () => {
|
||||
getNamedBuilder().addBooleanOptions(getBooleanOption()).setDefaultMemberPermissions('1').toJSON(),
|
||||
).not.toThrowError();
|
||||
|
||||
expect(() => getNamedBuilder().addChannelOptions(getChannelOption())).not.toThrowError();
|
||||
expect(() => getNamedBuilder().addChannelOptions(getChannelOption()).toJSON()).not.toThrowError();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -21,10 +21,10 @@ describe('Context Menu Commands', () => {
|
||||
expect(() => getBuilder().setName('A COMMAND').toJSON()).not.toThrowError();
|
||||
|
||||
// Translation: a_command
|
||||
expect(() => getBuilder().setName('o_comandă')).not.toThrowError();
|
||||
expect(() => getBuilder().setName('o_comandă').toJSON()).not.toThrowError();
|
||||
|
||||
// Translation: thx (according to GTranslate)
|
||||
expect(() => getBuilder().setName('どうも')).not.toThrowError();
|
||||
expect(() => getBuilder().setName('どうも').toJSON()).not.toThrowError();
|
||||
|
||||
expect(() => getBuilder().setName('🎉').toJSON()).not.toThrowError();
|
||||
expect(() => getBuilder().setName('').toJSON()).not.toThrowError();
|
||||
@@ -41,8 +41,15 @@ describe('Context Menu Commands', () => {
|
||||
};
|
||||
|
||||
test('GIVEN valid name localizations THEN does not throw error', () => {
|
||||
expect(() => getBuilder().setNameLocalization(Locale.EnglishUS, 'foobar')).not.toThrowError();
|
||||
expect(() => getBuilder().setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })).not.toThrowError();
|
||||
expect(() =>
|
||||
getBuilder().setName('test').setNameLocalization(Locale.EnglishUS, 'foobar').toJSON(),
|
||||
).not.toThrowError();
|
||||
expect(() =>
|
||||
getBuilder()
|
||||
.setName('test')
|
||||
.setNameLocalizations({ [Locale.EnglishUS]: 'foobar' })
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN invalid name localizations THEN does throw error', () => {
|
||||
@@ -71,12 +78,15 @@ describe('Context Menu Commands', () => {
|
||||
|
||||
describe('permissions', () => {
|
||||
test('GIVEN valid permission string THEN does not throw error', () => {
|
||||
expect(() => getBuilder().setDefaultMemberPermissions('1')).not.toThrowError();
|
||||
expect(() => getBuilder().setName('test').setDefaultMemberPermissions('1').toJSON()).not.toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN valid permission bitfield THEN does not throw error', () => {
|
||||
expect(() =>
|
||||
getBuilder().setDefaultMemberPermissions(PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles),
|
||||
getBuilder()
|
||||
.setName('test')
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.AddReactions | PermissionFlagsBits.AttachFiles)
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
@@ -90,11 +100,14 @@ describe('Context Menu Commands', () => {
|
||||
describe('contexts', () => {
|
||||
test('GIVEN a builder with valid contexts THEN does not throw an error', () => {
|
||||
expect(() =>
|
||||
getBuilder().setContexts([InteractionContextType.Guild, InteractionContextType.BotDM]),
|
||||
getBuilder()
|
||||
.setName('test')
|
||||
.setContexts([InteractionContextType.Guild, InteractionContextType.BotDM])
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
|
||||
expect(() =>
|
||||
getBuilder().setContexts(InteractionContextType.Guild, InteractionContextType.BotDM),
|
||||
getBuilder().setName('test').setContexts(InteractionContextType.Guild, InteractionContextType.BotDM).toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
@@ -110,17 +123,17 @@ describe('Context Menu Commands', () => {
|
||||
describe('integration types', () => {
|
||||
test('GIVEN a builder with valid integration types THEN does not throw an error', () => {
|
||||
expect(() =>
|
||||
getBuilder().setIntegrationTypes([
|
||||
ApplicationIntegrationType.GuildInstall,
|
||||
ApplicationIntegrationType.UserInstall,
|
||||
]),
|
||||
getBuilder()
|
||||
.setName('test')
|
||||
.setIntegrationTypes([ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall])
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
|
||||
expect(() =>
|
||||
getBuilder().setIntegrationTypes(
|
||||
ApplicationIntegrationType.GuildInstall,
|
||||
ApplicationIntegrationType.UserInstall,
|
||||
),
|
||||
getBuilder()
|
||||
.setName('test')
|
||||
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall)
|
||||
.toJSON(),
|
||||
).not.toThrowError();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user