Skip to content

Commit

Permalink
feat(builders): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Jun 18, 2024
1 parent 552f312 commit de3f24a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
47 changes: 47 additions & 0 deletions packages/builders/__tests__/components/button.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ describe('Button Components', () => {
button.toJSON();
}).not.toThrowError();

expect(() => {
// @ts-expect-error: discord-api-types.
const button = buttonComponent().setSKUId('123456789012345678').setStyle(ButtonStyle.Premium);
button.toJSON();
}).not.toThrowError();

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

Expand Down Expand Up @@ -101,6 +107,47 @@ describe('Button Components', () => {
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');

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

// @ts-expect-error: Invalid style
expect(() => buttonComponent().setStyle(24)).toThrowError();
expect(() => buttonComponent().setLabel(longStr)).toThrowError();
Expand Down
38 changes: 27 additions & 11 deletions packages/builders/src/components/Assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,37 @@ export function validateRequiredButtonParameters(
label?: string,
emoji?: APIMessageComponentEmoji,
customId?: string,
skuId?: string,
url?: string,
) {
if (url && customId) {
throw new RangeError('URL and custom id are mutually exclusive');
}
// @ts-expect-error discord-api-types.
if (style === ButtonStyle.Premium) {
if (!skuId) {
throw new RangeError('Premium buttons must have an SKU id.');
}

if (!label && !emoji) {
throw new RangeError('Buttons must have a label and/or an emoji');
}
if (customId || label || url || emoji) {
throw new RangeError('Premium buttons cannot have a custom id, label, URL, or emoji.');
}
} else {
if (skuId) {
throw new RangeError('Non-premium buttons must not have an SKU id.');
}

if (url && customId) {
throw new RangeError('URL and custom id are mutually exclusive.');
}

if (!label && !emoji) {
throw new RangeError('Non-premium buttons must have a label and/or an emoji.');
}

if (style === ButtonStyle.Link) {
if (!url) {
throw new RangeError('Link buttons must have a url');
if (style === ButtonStyle.Link) {
if (!url) {
throw new RangeError('Link buttons must have a URL.');
}
} else if (url) {
throw new RangeError('Non-premium and non-link buttons cannot have a URL.');
}
} else if (url) {
throw new RangeError('Non-link buttons cannot have a url');
}
}
2 changes: 2 additions & 0 deletions packages/builders/src/components/button/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ export class ButtonBuilder extends ComponentBuilder<APIButtonComponent> {
(this.data as Exclude<APIButtonComponent, APIButtonComponentWithSKUId>).label,
(this.data as Exclude<APIButtonComponent, APIButtonComponentWithSKUId>).emoji,
(this.data as APIButtonComponentWithCustomId).custom_id,
// @ts-expect-error discord-api-types.
(this.data as APIButtonComponentWithSKUId).sku_id,
(this.data as APIButtonComponentWithURL).url,
);

Expand Down

0 comments on commit de3f24a

Please sign in to comment.