Skip to content

Commit

Permalink
refactor: split button
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Sep 23, 2024
1 parent fad1b86 commit e784dd6
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 266 deletions.
18 changes: 6 additions & 12 deletions packages/builders/__tests__/components/actionRow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ActionRowBuilder,
ButtonBuilder,
createComponentBuilder,
CustomIdButtonBuilder,
StringSelectMenuBuilder,
StringSelectMenuOptionBuilder,
} from '../../src/index.js';
Expand Down Expand Up @@ -49,13 +50,6 @@ const rowWithSelectMenuData: APIActionRowComponent<APIMessageActionRowComponent>

describe('Action Row Components', () => {
describe('Assertion Tests', () => {
test('GIVEN valid components THEN do not throw', () => {
expect(() =>
new ActionRowBuilder().addButtonComponents(new ButtonBuilder(), new ButtonBuilder()),
).not.toThrowError();
expect(() => new ActionRowBuilder().addButtonComponents([new ButtonBuilder()])).not.toThrowError();
});

test('GIVEN valid JSON input THEN valid JSON output is given', () => {
const actionRowData: APIActionRowComponent<APIMessageActionRowComponent> = {
type: ComponentType.ActionRow,
Expand Down Expand Up @@ -120,7 +114,7 @@ describe('Action Row Components', () => {
});

test('GIVEN valid builder options THEN valid JSON output is given 2', () => {
const button = new ButtonBuilder().setLabel('test').setStyle(ButtonStyle.Primary).setCustomId('123');
const button = new CustomIdButtonBuilder().setLabel('test').setStyle(ButtonStyle.Primary).setCustomId('123');
const selectMenu = new StringSelectMenuBuilder()
.setCustomId('1234')
.setMaxValues(2)
Expand All @@ -134,9 +128,9 @@ describe('Action Row Components', () => {
new StringSelectMenuOptionBuilder().setLabel('two').setValue('two'),
]);

expect(new ActionRowBuilder().addButtonComponents(button).toJSON()).toEqual(rowWithButtonData);
expect(new ActionRowBuilder().addCustomIdButtonComponents(button).toJSON()).toEqual(rowWithButtonData);
expect(new ActionRowBuilder().addStringSelectMenuComponent(selectMenu).toJSON()).toEqual(rowWithSelectMenuData);
expect(new ActionRowBuilder().addButtonComponents([button]).toJSON()).toEqual(rowWithButtonData);
expect(new ActionRowBuilder().addCustomIdButtonComponents([button]).toJSON()).toEqual(rowWithButtonData);
});

test('GIVEN 2 select menus THEN it throws', () => {
Expand All @@ -156,7 +150,7 @@ describe('Action Row Components', () => {
});

test('GIVEN a button and a select menu THEN it throws', () => {
const button = new ButtonBuilder().setLabel('test').setStyle(ButtonStyle.Primary).setCustomId('123');
const button = new CustomIdButtonBuilder().setLabel('test').setStyle(ButtonStyle.Primary).setCustomId('123');
const selectMenu = new StringSelectMenuBuilder()
.setCustomId('1234')
.setOptions(
Expand All @@ -165,7 +159,7 @@ describe('Action Row Components', () => {
);

expect(() =>
new ActionRowBuilder().addStringSelectMenuComponent(selectMenu).addButtonComponents(button).toJSON(),
new ActionRowBuilder().addStringSelectMenuComponent(selectMenu).addCustomIdButtonComponents(button).toJSON(),
).toThrowError();
});
});
Expand Down
105 changes: 16 additions & 89 deletions packages/builders/__tests__/components/button.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
} from 'discord-api-types/v10';
import { describe, test, expect } from 'vitest';
import { ButtonBuilder } from '../../src/components/button/Button.js';

const buttonComponent = () => new ButtonBuilder();
import { CustomIdButtonBuilder, SKUIdButtonBuilder, URLButtonBuilder } from '../../src/index.js';

const longStr =
'looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong';
Expand All @@ -16,11 +15,11 @@ describe('Button Components', () => {
describe('Assertion Tests', () => {
test('GIVEN valid fields THEN builder does not throw', () => {
expect(() =>
buttonComponent().setCustomId('custom').setStyle(ButtonStyle.Primary).setLabel('test'),
new CustomIdButtonBuilder().setCustomId('custom').setStyle(ButtonStyle.Primary).setLabel('test'),
).not.toThrowError();

expect(() => {
const button = buttonComponent()
const button = new CustomIdButtonBuilder()
.setCustomId('custom')
.setLabel('test')
.setStyle(ButtonStyle.Primary)
Expand All @@ -31,111 +30,41 @@ describe('Button Components', () => {
}).not.toThrowError();

expect(() => {
const button = buttonComponent().setSKUId('123456789012345678').setStyle(ButtonStyle.Premium);
const button = new SKUIdButtonBuilder().setSKUId('123456789012345678');
button.toJSON();
}).not.toThrowError();

expect(() => buttonComponent().setURL('https://google.com')).not.toThrowError();
expect(() => new URLButtonBuilder().setURL('https://google.com')).not.toThrowError();
});

test('GIVEN invalid fields THEN build does throw', () => {
expect(() => {
buttonComponent().setCustomId(longStr).toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent()
.setCustomId('custom')
.setStyle(ButtonStyle.Primary)
.setDisabled(true)
.setLabel('test')
.setURL('https://google.com')
.setEmoji({ name: 'test' });

button.toJSON();
new CustomIdButtonBuilder().setCustomId(longStr).toJSON();
}).toThrowError();

expect(() => {
// @ts-expect-error: Invalid emoji
const button = buttonComponent().setEmoji('test');
const button = new CustomIdButtonBuilder().setEmoji('test');
button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent().setStyle(ButtonStyle.Primary);
const button = new CustomIdButtonBuilder().setStyle(ButtonStyle.Primary);
button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent().setStyle(ButtonStyle.Primary).setCustomId('test');
button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent().setStyle(ButtonStyle.Link);
button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent().setStyle(ButtonStyle.Primary).setLabel('test').setURL('https://google.com');
button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent().setStyle(ButtonStyle.Link).setLabel('test');
button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent().setStyle(ButtonStyle.Primary).setSKUId('123456789012345678');
button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent()
.setStyle(ButtonStyle.Secondary)
.setLabel('button')
.setSKUId('123456789012345678');

button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent()
.setStyle(ButtonStyle.Success)
.setEmoji({ name: '😇' })
.setSKUId('123456789012345678');

button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent()
.setStyle(ButtonStyle.Danger)
.setCustomId('test')
.setSKUId('123456789012345678');

button.toJSON();
}).toThrowError();

expect(() => {
const button = buttonComponent()
.setStyle(ButtonStyle.Link)
.setURL('https://google.com')
.setSKUId('123456789012345678');

const button = new CustomIdButtonBuilder().setStyle(ButtonStyle.Primary).setCustomId('test');
button.toJSON();
}).toThrowError();

// @ts-expect-error: Invalid style
expect(() => buttonComponent().setCustomId('hi').setStyle(24).toJSON()).toThrowError();
expect(() => buttonComponent().setCustomId('hi').setLabel(longStr).toJSON()).toThrowError();
expect(() => new CustomIdButtonBuilder().setCustomId('hi').setStyle(24).toJSON()).toThrowError();
expect(() => new CustomIdButtonBuilder().setCustomId('hi').setLabel(longStr).toJSON()).toThrowError();
// @ts-expect-error: Invalid parameter for disabled
expect(() => buttonComponent().setCustomId('hi').setDisabled(0).toJSON()).toThrowError();
expect(() => new CustomIdButtonBuilder().setCustomId('hi').setDisabled(0).toJSON()).toThrowError();
// @ts-expect-error: Invalid emoji
expect(() => buttonComponent().setCustomId('hi').setEmoji('foo').toJSON()).toThrowError();

expect(() => buttonComponent().setCustomId('hi').setURL('foobar').toJSON()).toThrowError();
expect(() => new CustomIdButtonBuilder().setCustomId('hi').setEmoji('foo').toJSON()).toThrowError();
});

test('GiVEN valid input THEN valid JSON outputs are given', () => {
Expand All @@ -147,10 +76,10 @@ describe('Button Components', () => {
disabled: true,
};

expect(new ButtonBuilder(interactionData).toJSON()).toEqual(interactionData);
expect(new CustomIdButtonBuilder(interactionData).toJSON()).toEqual(interactionData);

expect(
buttonComponent()
new CustomIdButtonBuilder()
.setCustomId(interactionData.custom_id)
.setLabel(interactionData.label!)
.setStyle(interactionData.style)
Expand All @@ -166,9 +95,7 @@ describe('Button Components', () => {
url: 'https://google.com',
};

expect(new ButtonBuilder(linkData).toJSON()).toEqual(linkData);

expect(buttonComponent().setLabel(linkData.label!).setDisabled(true).setURL(linkData.url));
expect(new URLButtonBuilder(linkData).toJSON()).toEqual(linkData);
});
});
});
6 changes: 3 additions & 3 deletions packages/builders/__tests__/components/components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
import { describe, test, expect } from 'vitest';
import {
ActionRowBuilder,
ButtonBuilder,
createComponentBuilder,
CustomIdButtonBuilder,
StringSelectMenuBuilder,
TextInputBuilder,
} from '../../src/index.js';

describe('createComponentBuilder', () => {
test.each([ButtonBuilder, StringSelectMenuBuilder, TextInputBuilder])(
test.each([StringSelectMenuBuilder, TextInputBuilder])(
'passing an instance of %j should return itself',
(Builder) => {
const builder = new Builder();
Expand All @@ -42,7 +42,7 @@ describe('createComponentBuilder', () => {
type: ComponentType.Button,
};

expect(createComponentBuilder(button)).toBeInstanceOf(ButtonBuilder);
expect(createComponentBuilder(button)).toBeInstanceOf(CustomIdButtonBuilder);
});

test('GIVEN a select menu component THEN returns a StringSelectMenuBuilder', () => {
Expand Down
Loading

0 comments on commit e784dd6

Please sign in to comment.