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