Files
discord.js/packages/structures/__tests__/invite.test.ts
Qjuh 3cff4d7412 feat: @discordjs/structures (#10900)
* chore: init /structures

* feat: base structure

* feat: initial structures design attempt

* refactor(Structure): use unknown to store in kData

* feat(Structure): add Invite

refactor(Structure): patch to _patch

* refactor: symbol names and override location

* fix: don't possibly return 0 if discord borks

Co-authored-by: Synbulat Biishev <signin@syjalo.dev>

* refactor: use getter value instead of api

Co-authored-by: Synbulat Biishev <signin@syjalo.dev>

* refactor: cache createdTimestamp value

Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com>

* docs: better docs for what's done so far

* feat: add Mixin

* refactor(User): remove bitfield getters and add displayName

* feat(structures): add Connection

* feat(structures): add Channel base

* refactor(Mixin): trace prototype chain, allow construction

* fix(structures): fix mixin behavior

* fix(structures): data optimization call behavior from perf testing

* feat: channel mixins

* chore: update deps

* feat: channels and mixins

* chore: more typeguard tests

* fix: tests and some other issues

* feat: add ChannelWebhookMixin

* fix: more tests

* chore: tests and docs

* chore: docs

* fix: remove unneccessary omitted

* chore: apply code suggestions

* refactor: change how extended invite works

* fix: type imports

* Apply suggestions from code review

Co-authored-by: Almeida <github@almeidx.dev>

* fix: tests

* chore: add jsdoc

* refactor: apply code suggestions

* fix: don't instantiate sub-structures

* fix: don't do null default twice

* chore: use formatters, add _cache

* chore: lockfile

* chore: move MixinTypes to declaratiion file

* fix: tests

* fix: don't include source d.ts files for docs

* feat: bitfields

* feat: more bitfields

* refactor: remove DirectoryChannel structure

* chore: apply suggestions from code review

* chore: remove unused import

* refactor: use symbol for mixin toJSON, remove _ prefix

* chore: apply suggestions from code review

* refactor: remove bitfield casts

* refactor: remove special case for threadchannel types

* fix: apply code review suggestions

* refactor: bitfields always store bigint

* fix: tests

* chore: apply suggestions from code review

* fix: lint

* refactor: conditional structuredClone

* Apply suggestions from code review

Co-authored-by: ckohen <chaikohen@gmail.com>

* fix: code review errors

* fix: lint

* chore: bump dtypes

* Update packages/structures/cliff.toml

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* docs: link to VideoQualityMode

* chore: typo in comment

* chore: small nits in docs links

* chore: small nits

* docs: forgot one

* chore: update template

* chore: typos and things

* chore: apply suggestions from code review

* fix: tests and typeguards

* chore: don't clone appliedTags

* refactor: use a symbol for patch method

* fix: add missing readonly

* chore: remove todo comment

* refactor: use symbol for clone

* fix: add constraint to DataType

* chore: apply suggestions

* fix: dtypes bump

* chore: fix comment

* chore: add todo comment

* chore: mark bitfield as todo
chore: mark bit field as todo and edit readme

---------

Co-authored-by: ckohen <chaikohen@gmail.com>
Co-authored-by: Synbulat Biishev <signin@syjalo.dev>
Co-authored-by: Almeida <github@almeidx.dev>
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2025-07-12 18:24:30 +00:00

91 lines
3.5 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/index.js';
import { kPatch } from '../src/utils/symbols.js';
describe('Invite', () => {
const dataNoCode: Omit<APIInvite, 'code'> = {
type: InviteType.Guild,
channel: null,
approximate_member_count: 15,
approximate_presence_count: 35,
target_type: InviteTargetType.EmbeddedApplication,
};
const data: APIInvite = {
...dataNoCode,
code: '123',
};
const dataExtended: APIExtendedInvite = {
...data,
created_at: '2020-10-10T13:50:17.209Z',
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(instance.createdAt?.toISOString()).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.209Z'));
expect(instance.expiresAt).toStrictEqual(new Date('2020-10-10T13:50:29.209Z'));
expect(instance.url).toBe('https://discord.gg/123');
expect(instance.toJSON()).toEqual({ ...dataExtended, expires_at: '2020-10-10T13:50:29.209Z' });
});
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.209Z' });
expect(instance.toJSON()).toEqual({ ...dataExtended, expires_at: '2020-10-10T13:50:29.209Z' });
});
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);
});
});