Skip to content

Commit 2670a8f

Browse files
committed
feat(builders): add tests
1 parent dd0d603 commit 2670a8f

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

packages/builders/__tests__/components/button.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ describe('Button Components', () => {
5050
button.toJSON();
5151
}).not.toThrowError();
5252

53+
expect(() => {
54+
// @ts-expect-error: discord-api-types.
55+
const button = buttonComponent().setSKUId('123456789012345678').setStyle(ButtonStyle.Premium);
56+
button.toJSON();
57+
}).not.toThrowError();
58+
5359
expect(() => buttonComponent().setURL('https://google.com')).not.toThrowError();
5460
});
5561

@@ -101,6 +107,47 @@ describe('Button Components', () => {
101107
button.toJSON();
102108
}).toThrowError();
103109

110+
expect(() => {
111+
const button = buttonComponent().setStyle(ButtonStyle.Primary).setSKUId('123456789012345678');
112+
button.toJSON();
113+
}).toThrowError();
114+
115+
expect(() => {
116+
const button = buttonComponent()
117+
.setStyle(ButtonStyle.Secondary)
118+
.setLabel('button')
119+
.setSKUId('123456789012345678');
120+
121+
button.toJSON();
122+
}).toThrowError();
123+
124+
expect(() => {
125+
const button = buttonComponent()
126+
.setStyle(ButtonStyle.Success)
127+
.setEmoji({ name: '😇' })
128+
.setSKUId('123456789012345678');
129+
130+
button.toJSON();
131+
}).toThrowError();
132+
133+
expect(() => {
134+
const button = buttonComponent()
135+
.setStyle(ButtonStyle.Danger)
136+
.setCustomId('test')
137+
.setSKUId('123456789012345678');
138+
139+
button.toJSON();
140+
}).toThrowError();
141+
142+
expect(() => {
143+
const button = buttonComponent()
144+
.setStyle(ButtonStyle.Link)
145+
.setURL('https://google.com')
146+
.setSKUId('123456789012345678');
147+
148+
button.toJSON();
149+
}).toThrowError();
150+
104151
// @ts-expect-error: Invalid style
105152
expect(() => buttonComponent().setStyle(24)).toThrowError();
106153
expect(() => buttonComponent().setLabel(longStr)).toThrowError();

packages/builders/src/components/Assertions.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,37 @@ export function validateRequiredButtonParameters(
8181
label?: string,
8282
emoji?: APIMessageComponentEmoji,
8383
customId?: string,
84+
skuId?: string,
8485
url?: string,
8586
) {
86-
if (url && customId) {
87-
throw new RangeError('URL and custom id are mutually exclusive');
88-
}
87+
// @ts-expect-error discord-api-types.
88+
if (style === ButtonStyle.Premium) {
89+
if (!skuId) {
90+
throw new RangeError('Premium buttons must have an SKU id.');
91+
}
8992

90-
if (!label && !emoji) {
91-
throw new RangeError('Buttons must have a label and/or an emoji');
92-
}
93+
if (customId || label || url || emoji) {
94+
throw new RangeError('Premium buttons cannot have a custom id, label, URL, or emoji.');
95+
}
96+
} else {
97+
if (skuId) {
98+
throw new RangeError('Non-premium buttons must not have an SKU id.');
99+
}
100+
101+
if (url && customId) {
102+
throw new RangeError('URL and custom id are mutually exclusive.');
103+
}
104+
105+
if (!label && !emoji) {
106+
throw new RangeError('Non-premium buttons must have a label and/or an emoji.');
107+
}
93108

94-
if (style === ButtonStyle.Link) {
95-
if (!url) {
96-
throw new RangeError('Link buttons must have a url');
109+
if (style === ButtonStyle.Link) {
110+
if (!url) {
111+
throw new RangeError('Link buttons must have a URL.');
112+
}
113+
} else if (url) {
114+
throw new RangeError('Non-premium and non-link buttons cannot have a URL.');
97115
}
98-
} else if (url) {
99-
throw new RangeError('Non-link buttons cannot have a url');
100116
}
101117
}

packages/builders/src/components/button/Button.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ export class ButtonBuilder extends ComponentBuilder<APIButtonComponent> {
140140
this.data.label,
141141
this.data.emoji,
142142
(this.data as APIButtonComponentWithCustomId).custom_id,
143+
// @ts-expect-error discord-api-types.
144+
(this.data as APIButtonComponentWithSKUId).sku_id,
143145
(this.data as APIButtonComponentWithURL).url,
144146
);
145147

0 commit comments

Comments
 (0)