Skip to content

Commit

Permalink
chore: make requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Feb 10, 2022
1 parent a136af1 commit 584f980
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 43 deletions.
1 change: 0 additions & 1 deletion packages/builders/__tests__/components/selectMenu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ describe('Button Components', () => {
};

expect(
// @ts-expect-error
new SelectMenuComponent(selectMenuDataWithoutOptions)
.addOptions(new SelectMenuOption(selectMenuOptionData))
.toJSON(),
Expand Down
57 changes: 20 additions & 37 deletions packages/builders/__tests__/messages/embed.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Embed } from '../../src';
import type { APIEmbed } from 'discord-api-types/v9';

const emptyEmbed: APIEmbed = {};

const alpha = 'abcdefghijklmnopqrstuvwxyz';

Expand Down Expand Up @@ -29,21 +26,21 @@ describe('Embed', () => {
describe('Embed title', () => {
test('GIVEN an embed with a pre-defined title THEN return valid toJSON data', () => {
const embed = new Embed({ title: 'foo' });
expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, title: 'foo' });
expect(embed.toJSON()).toStrictEqual({ title: 'foo' });
});

test('GIVEN an embed using Embed#setTitle THEN return valid toJSON data', () => {
const embed = new Embed();
embed.setTitle('foo');

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, title: 'foo' });
expect(embed.toJSON()).toStrictEqual({ title: 'foo' });
});

test('GIVEN an embed with a pre-defined title THEN unset title THEN return valid toJSON data', () => {
const embed = new Embed({ title: 'foo' });
embed.setTitle(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, title: undefined });
expect(embed.toJSON()).toStrictEqual({ title: undefined });
});

test('GIVEN an embed with an invalid title THEN throws error', () => {
Expand All @@ -55,22 +52,22 @@ describe('Embed', () => {

describe('Embed description', () => {
test('GIVEN an embed with a pre-defined description THEN return valid toJSON data', () => {
const embed = new Embed({ ...emptyEmbed, description: 'foo' });
expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, description: 'foo' });
const embed = new Embed({ description: 'foo' });
expect(embed.toJSON()).toStrictEqual({ description: 'foo' });
});

test('GIVEN an embed using Embed#setDescription THEN return valid toJSON data', () => {
const embed = new Embed();
embed.setDescription('foo');

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, description: 'foo' });
expect(embed.toJSON()).toStrictEqual({ description: 'foo' });
});

test('GIVEN an embed with a pre-defined description THEN unset description THEN return valid toJSON data', () => {
const embed = new Embed({ description: 'foo' });
embed.setDescription(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, description: undefined });
expect(embed.toJSON()).toStrictEqual({ description: undefined });
});

test('GIVEN an embed with an invalid description THEN throws error', () => {
Expand All @@ -84,7 +81,6 @@ describe('Embed', () => {
test('GIVEN an embed with a pre-defined url THEN returns valid toJSON data', () => {
const embed = new Embed({ url: 'https://discord.js.org/' });
expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
url: 'https://discord.js.org/',
});
});
Expand All @@ -94,7 +90,6 @@ describe('Embed', () => {
embed.setURL('https://discord.js.org/');

expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
url: 'https://discord.js.org/',
});
});
Expand All @@ -103,7 +98,7 @@ describe('Embed', () => {
const embed = new Embed({ url: 'https://discord.js.org' });
embed.setURL(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, url: undefined });
expect(embed.toJSON()).toStrictEqual({ url: undefined });
});

test('GIVEN an embed with an invalid URL THEN throws error', () => {
Expand All @@ -116,21 +111,21 @@ describe('Embed', () => {
describe('Embed Color', () => {
test('GIVEN an embed with a pre-defined color THEN returns valid toJSON data', () => {
const embed = new Embed({ color: 0xff0000 });
expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, color: 0xff0000 });
expect(embed.toJSON()).toStrictEqual({ color: 0xff0000 });
});

test('GIVEN an embed using Embed#setColor THEN returns valid toJSON data', () => {
const embed = new Embed();
embed.setColor(0xff0000);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, color: 0xff0000 });
expect(embed.toJSON()).toStrictEqual({ color: 0xff0000 });
});

test('GIVEN an embed with a pre-defined color THEN unset color THEN return valid toJSON data', () => {
const embed = new Embed({ color: 0xff0000 });
embed.setColor(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, color: undefined });
expect(embed.toJSON()).toStrictEqual({ color: undefined });
});

test('GIVEN an embed with an invalid color THEN throws error', () => {
Expand All @@ -146,43 +141,42 @@ describe('Embed', () => {

test('GIVEN an embed with a pre-defined timestamp THEN returns valid toJSON data', () => {
const embed = new Embed({ timestamp: now.toISOString() });
expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, timestamp: now.toISOString() });
expect(embed.toJSON()).toStrictEqual({ timestamp: now.toISOString() });
});

test('given an embed using Embed#setTimestamp (with Date) THEN returns valid toJSON data', () => {
const embed = new Embed();
embed.setTimestamp(now);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, timestamp: now.toISOString() });
expect(embed.toJSON()).toStrictEqual({ timestamp: now.toISOString() });
});

test('GIVEN an embed using Embed#setTimestamp (with int) THEN returns valid toJSON data', () => {
const embed = new Embed();
embed.setTimestamp(now.getTime());

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, timestamp: now.toISOString() });
expect(embed.toJSON()).toStrictEqual({ timestamp: now.toISOString() });
});

test('GIVEN an embed using Embed#setTimestamp (default) THEN returns valid toJSON data', () => {
const embed = new Embed();
embed.setTimestamp();

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, timestamp: embed.timestamp });
expect(embed.toJSON()).toStrictEqual({ timestamp: embed.timestamp });
});

test('GIVEN an embed with a pre-defined timestamp THEN unset timestamp THEN return valid toJSON data', () => {
const embed = new Embed({ timestamp: now.toISOString() });
embed.setTimestamp(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, timestamp: undefined });
expect(embed.toJSON()).toStrictEqual({ timestamp: undefined });
});
});

describe('Embed Thumbnail', () => {
test('GIVEN an embed with a pre-defined thumbnail THEN returns valid toJSON data', () => {
const embed = new Embed({ thumbnail: { url: 'https://discord.js.org/static/logo.svg' } });
expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
thumbnail: { url: 'https://discord.js.org/static/logo.svg' },
});
});
Expand All @@ -192,7 +186,6 @@ describe('Embed', () => {
embed.setThumbnail('https://discord.js.org/static/logo.svg');

expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
thumbnail: { url: 'https://discord.js.org/static/logo.svg' },
});
});
Expand All @@ -201,7 +194,7 @@ describe('Embed', () => {
const embed = new Embed({ thumbnail: { url: 'https://discord.js.org/static/logo.svg' } });
embed.setThumbnail(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, thumbnail: undefined });
expect(embed.toJSON()).toStrictEqual({ thumbnail: undefined });
});

test('GIVEN an embed with an invalid thumbnail THEN throws error', () => {
Expand All @@ -215,7 +208,6 @@ describe('Embed', () => {
test('GIVEN an embed with a pre-defined image THEN returns valid toJSON data', () => {
const embed = new Embed({ image: { url: 'https://discord.js.org/static/logo.svg' } });
expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
image: { url: 'https://discord.js.org/static/logo.svg' },
});
});
Expand All @@ -225,7 +217,6 @@ describe('Embed', () => {
embed.setImage('https://discord.js.org/static/logo.svg');

expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
image: { url: 'https://discord.js.org/static/logo.svg' },
});
});
Expand All @@ -234,7 +225,7 @@ describe('Embed', () => {
const embed = new Embed({ image: { url: 'https://discord.js/org/static/logo.svg' } });
embed.setImage(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, image: undefined });
expect(embed.toJSON()).toStrictEqual({ image: undefined });
});

test('GIVEN an embed with an invalid image THEN throws error', () => {
Expand All @@ -250,7 +241,6 @@ describe('Embed', () => {
author: { name: 'Wumpus', icon_url: 'https://discord.js.org/static/logo.svg', url: 'https://discord.js.org' },
});
expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
author: { name: 'Wumpus', icon_url: 'https://discord.js.org/static/logo.svg', url: 'https://discord.js.org' },
});
});
Expand All @@ -264,7 +254,6 @@ describe('Embed', () => {
});

expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
author: { name: 'Wumpus', icon_url: 'https://discord.js.org/static/logo.svg', url: 'https://discord.js.org' },
});
});
Expand All @@ -275,7 +264,7 @@ describe('Embed', () => {
});
embed.setAuthor(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, author: undefined });
expect(embed.toJSON()).toStrictEqual({ author: undefined });
});

test('GIVEN an embed with an invalid author name THEN throws error', () => {
Expand All @@ -291,7 +280,6 @@ describe('Embed', () => {
footer: { text: 'Wumpus', icon_url: 'https://discord.js.org/static/logo.svg' },
});
expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
footer: { text: 'Wumpus', icon_url: 'https://discord.js.org/static/logo.svg' },
});
});
Expand All @@ -301,7 +289,6 @@ describe('Embed', () => {
embed.setFooter({ text: 'Wumpus', iconURL: 'https://discord.js.org/static/logo.svg' });

expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
footer: { text: 'Wumpus', icon_url: 'https://discord.js.org/static/logo.svg' },
});
});
Expand All @@ -310,7 +297,7 @@ describe('Embed', () => {
const embed = new Embed({ footer: { text: 'Wumpus', icon_url: 'https://discord.js.org/static/logo.svg' } });
embed.setFooter(null);

expect(embed.toJSON()).toStrictEqual({ ...emptyEmbed, footer: undefined });
expect(embed.toJSON()).toStrictEqual({ footer: undefined });
});

test('GIVEN an embed with invalid footer text THEN throws error', () => {
Expand All @@ -326,7 +313,6 @@ describe('Embed', () => {
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
});
expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
});
});
Expand All @@ -336,7 +322,6 @@ describe('Embed', () => {
embed.addField({ name: 'foo', value: 'bar' });

expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
});
});
Expand All @@ -346,7 +331,6 @@ describe('Embed', () => {
embed.addFields({ name: 'foo', value: 'bar' });

expect(embed.toJSON()).toStrictEqual({
...emptyEmbed,
fields: [{ name: 'foo', value: 'bar', inline: undefined }],
});
});
Expand All @@ -356,7 +340,6 @@ describe('Embed', () => {
embed.addFields({ name: 'foo', value: 'bar' }, { name: 'foo', value: 'baz' });

expect(embed.spliceFields(0, 1).toJSON()).toStrictEqual({
...emptyEmbed,
fields: [{ name: 'foo', value: 'baz', inline: undefined }],
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/src/components/ActionRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ActionRow<T extends ActionRowComponent = ActionRowComponent> extend

public constructor({ components, ...data }: Partial<APIActionRowComponent> = {}) {
super({ type: ComponentType.ActionRow, ...data });
this.components = (components?.map(createComponent) ?? []) as T[];
this.components = (components?.map((c) => createComponent(c)) ?? []) as T[];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/src/components/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export abstract class Component<
/**
* The API data associated with this component
*/
protected data: DataType;
protected readonly data: DataType;

/**
* Converts this component to an API-compatible JSON object
Expand Down
2 changes: 1 addition & 1 deletion packages/builders/src/messages/embed/Assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const embedFieldsArrayPredicate = embedFieldPredicate.array();

export const fieldLengthPredicate = z.number().lte(25);

export function validateFieldLength(fields: APIEmbedField[] | undefined, amountAdding: number): void {
export function validateFieldLength(amountAdding: number, fields?: APIEmbedField[]): void {
fieldLengthPredicate.parse((fields?.length ?? 0) + amountAdding);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/builders/src/messages/embed/Embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import { EmbedAuthorOptions, EmbedFooterOptions, UnsafeEmbed } from './UnsafeEmb
export class Embed extends UnsafeEmbed {
public override addFields(...fields: APIEmbedField[]): this {
// Ensure adding these fields won't exceed the 25 field limit
validateFieldLength(this.fields, fields.length);
validateFieldLength(fields.length, this.fields);

// Data assertions
return super.addFields(...embedFieldsArrayPredicate.parse(fields));
}

public override spliceFields(index: number, deleteCount: number, ...fields: APIEmbedField[]): this {
// Ensure adding these fields won't exceed the 25 field limit
validateFieldLength(this.fields, fields.length - deleteCount);
validateFieldLength(fields.length - deleteCount, this.fields);

// Data assertions
return super.spliceFields(index, deleteCount, ...embedFieldsArrayPredicate.parse(fields));
Expand Down

0 comments on commit 584f980

Please sign in to comment.