Skip to content

Commit

Permalink
refactor(builders-methods): make methods consistent (#7395)
Browse files Browse the repository at this point in the history
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
  • Loading branch information
BenjammingKirby and vladfrangu committed Feb 12, 2022
1 parent 861f0e2 commit f495364
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 35 deletions.
6 changes: 3 additions & 3 deletions packages/builders/__tests__/components/actionRow.test.ts
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/__tests__/components/selectMenu.test.ts
Expand Up @@ -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', () => {
Expand Down
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Expand Up @@ -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),
Expand Down Expand Up @@ -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();
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/builders/src/components/ActionRow.ts
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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> {
Expand All @@ -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.');
}
Expand Down Expand Up @@ -51,26 +53,26 @@ 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.');
}

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;
}
Expand Down

0 comments on commit f495364

Please sign in to comment.