mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
* feat: message structures * fix: docs * chore: components and more * feat: embed and more * feat: more substructures and code review suggestions * chore: tests and date conversions * chore: jsdoc strings * fix: tests * fix: tests * feat: hexColor getters * chore: remove getters for nested data * chore: apply suggestions from code review * fix: burst_colors in toJSON * docs: rephrase SectionBuilder remark * chore: add LabelComponent * fix: add name and size to file component * chore: move resolved interaction data to interactions dir * fix: code review * chore: bump discord-api-types * chore: apply code review suggestions * fix: lockfile * chore: update remark * fix: missing export * chore: code review and tests * build: fix file * fix: typo * fix: missing toJSON * fix: remove redundant patch overrides * chore: missing component suffix * chore: better name * chore: add comment explaining timestamp conversion --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
94 lines
3.8 KiB
TypeScript
94 lines
3.8 KiB
TypeScript
import type { APIExtendedInvite, APIInvite } from 'discord-api-types/v10';
|
|
import { InviteTargetType, InviteType } from 'discord-api-types/v10';
|
|
import { describe, expect, test } from 'vitest';
|
|
import { Invite } from '../src/invites/Invite.js';
|
|
import { dateToDiscordISOTimestamp } from '../src/utils/optimization.js';
|
|
import { kPatch } from '../src/utils/symbols.js';
|
|
|
|
describe('Invite', () => {
|
|
// TODO: Check if omitting `expires_at` is appropriate
|
|
|
|
const dataNoCode: Omit<APIInvite, 'code' | 'expires_at'> = {
|
|
type: InviteType.Guild,
|
|
channel: null,
|
|
approximate_member_count: 15,
|
|
approximate_presence_count: 35,
|
|
target_type: InviteTargetType.EmbeddedApplication,
|
|
};
|
|
|
|
const data: Omit<APIInvite, 'expires_at'> = {
|
|
...dataNoCode,
|
|
code: '123',
|
|
};
|
|
|
|
const dataExtended: Omit<APIExtendedInvite, 'expires_at'> = {
|
|
...data,
|
|
created_at: '2020-10-10T13:50:17.209000+00:00',
|
|
max_age: 12,
|
|
max_uses: 34,
|
|
temporary: false,
|
|
uses: 5,
|
|
};
|
|
|
|
test('Invite has all properties', () => {
|
|
const instance = new Invite(data);
|
|
expect(instance.type).toBe(data.type);
|
|
expect(instance.code).toBe(data.code);
|
|
expect(instance.createdAt).toBe(null);
|
|
expect(instance.createdTimestamp).toBe(null);
|
|
expect(instance.maxAge).toBe(undefined);
|
|
expect(instance.maxUses).toBe(undefined);
|
|
expect(instance.approximateMemberCount).toBe(data.approximate_member_count);
|
|
expect(instance.approximatePresenceCount).toBe(data.approximate_presence_count);
|
|
expect(instance.targetType).toBe(data.target_type);
|
|
expect(instance.temporary).toBe(undefined);
|
|
expect(instance.uses).toBe(undefined);
|
|
expect(instance.expiresTimestamp).toBe(null);
|
|
expect(instance.expiresAt).toBe(null);
|
|
expect(instance.url).toBe('https://discord.gg/123');
|
|
expect(instance.toJSON()).toEqual(data);
|
|
expect(`${instance}`).toBe('https://discord.gg/123');
|
|
expect(instance.valueOf()).toBe(data.code);
|
|
});
|
|
|
|
test('extended Invite has all properties', () => {
|
|
const instance = new Invite(dataExtended);
|
|
expect(instance.type).toBe(data.type);
|
|
expect(instance.code).toBe(dataExtended.code);
|
|
expect(dateToDiscordISOTimestamp(instance.createdAt!)).toBe(dataExtended.created_at);
|
|
expect(instance.createdTimestamp).toBe(Date.parse(dataExtended.created_at));
|
|
expect(instance.maxAge).toBe(dataExtended.max_age);
|
|
expect(instance.maxUses).toBe(dataExtended.max_uses);
|
|
expect(instance.approximateMemberCount).toBe(dataExtended.approximate_member_count);
|
|
expect(instance.approximatePresenceCount).toBe(dataExtended.approximate_presence_count);
|
|
expect(instance.targetType).toBe(dataExtended.target_type);
|
|
expect(instance.temporary).toBe(dataExtended.temporary);
|
|
expect(instance.uses).toBe(dataExtended.uses);
|
|
expect(instance.expiresTimestamp).toStrictEqual(Date.parse('2020-10-10T13:50:29.209000+00:00'));
|
|
expect(instance.expiresAt).toStrictEqual(new Date('2020-10-10T13:50:29.209000+00:00'));
|
|
expect(instance.url).toBe('https://discord.gg/123');
|
|
expect(instance.toJSON()).toEqual({ ...dataExtended, expires_at: '2020-10-10T13:50:29.209000+00:00' });
|
|
});
|
|
|
|
test('Invite with omitted properties', () => {
|
|
const instance = new Invite(dataNoCode);
|
|
expect(instance.toJSON()).toEqual(dataNoCode);
|
|
expect(instance.url).toBe(null);
|
|
expect(instance.code).toBe(undefined);
|
|
expect(`${instance}`).toBe('');
|
|
expect(instance.valueOf()).toEqual(Object.prototype.valueOf.apply(instance));
|
|
});
|
|
|
|
test('Invite with expiration', () => {
|
|
const instance = new Invite({ ...dataExtended, expires_at: '2020-10-10T13:50:29.209000+00:00' });
|
|
expect(instance.toJSON()).toEqual({ ...dataExtended, expires_at: '2020-10-10T13:50:29.209000+00:00' });
|
|
});
|
|
|
|
test('Patching Invite works in place', () => {
|
|
const instance1 = new Invite(data);
|
|
const instance2 = instance1[kPatch]({ max_age: 34 });
|
|
expect(instance1.toJSON()).not.toEqual(data);
|
|
expect(instance2).toBe(instance1);
|
|
});
|
|
});
|