refactor(builders-methods): make methods consistent (#7395)

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
Ben
2022-02-12 11:03:13 -07:00
committed by GitHub
parent 861f0e2134
commit f4953647ff
7 changed files with 39 additions and 35 deletions

View File

@@ -5,7 +5,7 @@ describe('Action Row Components', () => {
describe('Assertion Tests', () => {
test('GIVEN valid components THEN do not throw', () => {
expect(() => new ActionRow().addComponents(new ButtonComponent())).not.toThrowError();
expect(() => new ActionRow().setComponents([new ButtonComponent()])).not.toThrowError();
expect(() => new ActionRow().setComponents(new ButtonComponent())).not.toThrowError();
});
test('GIVEN valid JSON input THEN valid JSON output is given', () => {
@@ -84,10 +84,10 @@ describe('Action Row Components', () => {
.setCustomId('1234')
.setMaxValues(10)
.setMinValues(12)
.setOptions([
.setOptions(
new SelectMenuOption().setLabel('one').setValue('one'),
new SelectMenuOption().setLabel('two').setValue('two'),
]);
);
expect(new ActionRow().addComponents(button).toJSON()).toEqual(rowWithButtonData);
expect(new ActionRow().addComponents(selectMenu).toJSON()).toEqual(rowWithSelectMenuData);

View File

@@ -23,7 +23,7 @@ describe('Button Components', () => {
.setEmoji({ name: 'test' })
.setDescription('description');
expect(() => selectMenu().addOptions(option)).not.toThrowError();
expect(() => selectMenu().setOptions([option])).not.toThrowError();
expect(() => selectMenu().setOptions(option)).not.toThrowError();
});
test('GIVEN invalid inputs THEN Select Menu does throw', () => {

View File

@@ -87,9 +87,7 @@ describe('Application Command toJSON() results', () => {
min_value: 1,
});
expect(
getIntegerOption().setAutocomplete(true).setChoices([]).toJSON(),
).toEqual<APIApplicationCommandIntegerOption>({
expect(getIntegerOption().setAutocomplete(true).setChoices().toJSON()).toEqual<APIApplicationCommandIntegerOption>({
name: 'owo',
description: 'Testing 123',
type: ApplicationCommandOptionType.Integer,
@@ -101,7 +99,9 @@ describe('Application Command toJSON() results', () => {
choices: [],
});
expect(getIntegerOption().addChoice('uwu', 1).toJSON()).toEqual<APIApplicationCommandIntegerOption>({
expect(
getIntegerOption().addChoice({ name: 'uwu', value: 1 }).toJSON(),
).toEqual<APIApplicationCommandIntegerOption>({
name: 'owo',
description: 'Testing 123',
type: ApplicationCommandOptionType.Integer,
@@ -131,7 +131,7 @@ describe('Application Command toJSON() results', () => {
min_value: 1,
});
expect(getNumberOption().setAutocomplete(true).setChoices([]).toJSON()).toEqual<APIApplicationCommandNumberOption>({
expect(getNumberOption().setAutocomplete(true).setChoices().toJSON()).toEqual<APIApplicationCommandNumberOption>({
name: 'owo',
description: 'Testing 123',
type: ApplicationCommandOptionType.Number,
@@ -143,7 +143,7 @@ describe('Application Command toJSON() results', () => {
choices: [],
});
expect(getNumberOption().addChoice('uwu', 1).toJSON()).toEqual<APIApplicationCommandNumberOption>({
expect(getNumberOption().addChoice({ name: 'uwu', value: 1 }).toJSON()).toEqual<APIApplicationCommandNumberOption>({
name: 'owo',
description: 'Testing 123',
type: ApplicationCommandOptionType.Number,
@@ -171,7 +171,7 @@ describe('Application Command toJSON() results', () => {
required: true,
});
expect(getStringOption().setAutocomplete(true).setChoices([]).toJSON()).toEqual<APIApplicationCommandStringOption>({
expect(getStringOption().setAutocomplete(true).setChoices().toJSON()).toEqual<APIApplicationCommandStringOption>({
name: 'owo',
description: 'Testing 123',
type: ApplicationCommandOptionType.String,
@@ -181,7 +181,9 @@ describe('Application Command toJSON() results', () => {
choices: [],
});
expect(getStringOption().addChoice('uwu', '1').toJSON()).toEqual<APIApplicationCommandStringOption>({
expect(
getStringOption().addChoice({ name: 'uwu', value: '1' }).toJSON(),
).toEqual<APIApplicationCommandStringOption>({
name: 'owo',
description: 'Testing 123',
type: ApplicationCommandOptionType.String,

View File

@@ -138,23 +138,23 @@ describe('Slash Commands', () => {
integer
.setName('iscool')
.setDescription('Are we cool or what?')
.addChoices([['Very cool', 1_000]]),
.addChoices({ name: 'Very cool', value: 1_000 }),
)
.addNumberOption((number) =>
number
.setName('iscool')
.setDescription('Are we cool or what?')
.addChoices([['Very cool', 1.5]]),
.addChoices({ name: 'Very cool', value: 1.5 }),
)
.addStringOption((string) =>
string
.setName('iscool')
.setDescription('Are we cool or what?')
.addChoices([
['Fancy Pants', 'fp_1'],
['Fancy Shoes', 'fs_1'],
['The Whole shebang', 'all'],
]),
.addChoices(
{ name: 'Fancy Pants', value: 'fp_1' },
{ name: 'Fancy Shoes', value: 'fs_1' },
{ name: 'The Whole shebang', value: 'all' },
),
)
.addIntegerOption((integer) =>
integer.setName('iscool').setDescription('Are we cool or what?').setAutocomplete(true),
@@ -331,24 +331,24 @@ describe('Slash Commands', () => {
expect(() => getBuilder().setName('foo').setDescription('foo').setDefaultPermission(false)).not.toThrowError();
});
test('GIVEN an option that is autocompletable and has choices, THEN setting choices to an empty array should not throw an error', () => {
test('GIVEN an option that is autocompletable and has choices, THEN passing nothing to setChoices should not throw an error', () => {
expect(() =>
getBuilder().addStringOption(getStringOption().setAutocomplete(true).setChoices([])),
getBuilder().addStringOption(getStringOption().setAutocomplete(true).setChoices()),
).not.toThrowError();
});
test('GIVEN an option that is autocompletable, THEN setting choices should throw an error', () => {
expect(() =>
getBuilder().addStringOption(
getStringOption()
.setAutocomplete(true)
.setChoices([['owo', 'uwu']]),
getStringOption().setAutocomplete(true).setChoices({ name: 'owo', value: 'uwu' }),
),
).toThrowError();
});
test('GIVEN an option, THEN setting choices should not throw an error', () => {
expect(() => getBuilder().addStringOption(getStringOption().setChoices([['owo', 'uwu']]))).not.toThrowError();
expect(() =>
getBuilder().addStringOption(getStringOption().setChoices({ name: 'owo', value: 'uwu' })),
).not.toThrowError();
});
});

View File

@@ -34,7 +34,7 @@ export class ActionRow<T extends ActionRowComponent = ActionRowComponent> implem
* Sets the components in this action row
* @param components The components to set this row to
*/
public setComponents(components: T[]) {
public setComponents(...components: T[]) {
Reflect.set(this, 'components', [...components]);
return this;
}

View File

@@ -84,7 +84,7 @@ export class UnsafeSelectMenuComponent implements Component {
* Sets the options on this select menu
* @param options The options to set on this select menu
*/
public setOptions(options: SelectMenuOption[]) {
public setOptions(...options: SelectMenuOption[]) {
Reflect.set(this, 'options', [...options]);
return this;
}

View File

@@ -4,7 +4,9 @@ import { validateMaxChoicesLength } from '../Assertions';
const stringPredicate = z.string().min(1).max(100);
const numberPredicate = z.number().gt(-Infinity).lt(Infinity);
const choicesPredicate = z.tuple([stringPredicate, z.union([stringPredicate, numberPredicate])]).array();
const choicesPredicate = z
.object({ name: stringPredicate, value: z.union([stringPredicate, numberPredicate]) })
.array();
const booleanPredicate = z.boolean();
export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends string | number> {
@@ -17,10 +19,10 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends s
/**
* Adds a choice for this option
*
* @param name The name of the choice
* @param value The value of the choice
* @param choice The choice to add
*/
public addChoice(name: string, value: T): this {
public addChoice(choice: APIApplicationCommandOptionChoice<T>): this {
const { name, value } = choice;
if (this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
@@ -51,18 +53,18 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends s
*
* @param choices The choices to add
*/
public addChoices(choices: [name: string, value: T][]): this {
public addChoices(...choices: APIApplicationCommandOptionChoice<T>[]): this {
if (this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
choicesPredicate.parse(choices);
for (const [label, value] of choices) this.addChoice(label, value);
for (const entry of choices) this.addChoice(entry);
return this;
}
public setChoices(choices: [name: string, value: T][]): this {
public setChoices<Input extends APIApplicationCommandOptionChoice<T>[]>(...choices: Input): this {
if (choices.length > 0 && this.autocomplete) {
throw new RangeError('Autocomplete and choices are mutually exclusive to each other.');
}
@@ -70,7 +72,7 @@ export class ApplicationCommandOptionWithChoicesAndAutocompleteMixin<T extends s
choicesPredicate.parse(choices);
Reflect.set(this, 'choices', []);
for (const [label, value] of choices) this.addChoice(label, value);
for (const entry of choices) this.addChoice(entry);
return this;
}