Skip to content

Commit 9edce03

Browse files
committed
test(structures): add tests for Emoji and Entitlement
1 parent 3550b49 commit 9edce03

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { DiscordSnowflake } from '@sapphire/snowflake';
2+
import type { APIEmoji, APIUser } from 'discord-api-types/v10';
3+
import { describe, expect, test } from 'vitest';
4+
import { Emoji } from '../src/emojis/Emoji.js';
5+
import { kPatch } from '../src/utils/symbols.js';
6+
7+
describe('Emoji structure', () => {
8+
const user: APIUser = {
9+
id: '1',
10+
username: 'username',
11+
discriminator: '0000',
12+
global_name: 'djs.structures.user.global_name',
13+
avatar: 'djs.structures.user.avatar',
14+
};
15+
16+
const data: APIEmoji = {
17+
id: '1',
18+
name: 'name',
19+
roles: ['1', '2', '3'],
20+
user,
21+
require_colons: true,
22+
managed: true,
23+
animated: true,
24+
available: true,
25+
};
26+
27+
const instance = new Emoji(data);
28+
29+
test('Emoji has all properties', () => {
30+
expect(instance.id).toBe(data.id);
31+
expect(instance.name).toBe(data.name);
32+
expect(instance.requireColons).toBe(data.require_colons);
33+
expect(instance.managed).toBe(data.managed);
34+
expect(instance.animated).toBe(data.animated);
35+
expect(instance.available).toBe(data.available);
36+
37+
expect(instance.createdTimestamp).toBe(DiscordSnowflake.timestampFrom(instance.id!));
38+
expect(instance.createdAt).toEqual(new Date(instance.createdTimestamp!));
39+
});
40+
41+
test('toJSON() is accurate', () => {
42+
expect(instance.toJSON()).toStrictEqual(data);
43+
});
44+
45+
test('Patching the Emoji works in place', () => {
46+
const patched = instance[kPatch]({
47+
available: false,
48+
});
49+
50+
expect(patched.toJSON()).not.toEqual(data);
51+
expect(patched).toBe(instance);
52+
});
53+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { DiscordSnowflake } from '@sapphire/snowflake';
2+
import { type APIEntitlement, EntitlementType } from 'discord-api-types/v10';
3+
import { describe, expect, test } from 'vitest';
4+
import { Entitlement } from '../src/entitlements/Entitlement.js';
5+
import { kPatch } from '../src/utils/symbols.js';
6+
7+
describe('Entitlement structure', () => {
8+
const data: APIEntitlement = {
9+
id: '1',
10+
sku_id: '1',
11+
application_id: '1',
12+
user_id: '1',
13+
type: EntitlementType.Purchase,
14+
deleted: false,
15+
starts_at: '2020-10-10T13:50:17.209000+00:00',
16+
ends_at: '2020-10-10T15:50:17.209000+00:00',
17+
consumed: false,
18+
// note guild_id is missing (to test kPatch)
19+
};
20+
21+
const instance = new Entitlement(data);
22+
23+
test('Entitlement has all properties', () => {
24+
expect(instance.id).toBe(data.id);
25+
expect(instance.skuId).toBe(data.sku_id);
26+
expect(instance.applicationId).toBe(data.application_id);
27+
expect(instance.userId).toBe(data.user_id);
28+
expect(instance.type).toBe(data.type);
29+
expect(instance.guildId).toBe(data.guild_id);
30+
expect(instance.consumed).toBe(data.consumed);
31+
32+
expect(instance.createdTimestamp).toBe(DiscordSnowflake.timestampFrom(instance.id!));
33+
expect(instance.createdAt).toEqual(new Date(instance.createdTimestamp!));
34+
});
35+
36+
test('toJSON() is accurate', () => {
37+
expect(instance.toJSON()).toStrictEqual(data);
38+
});
39+
40+
test('Patching the Entitlement works in place', () => {
41+
const guildId = '111111';
42+
const consumed = true;
43+
44+
const patched = instance[kPatch]({
45+
guild_id: guildId,
46+
consumed,
47+
});
48+
49+
expect(patched.guildId).toBe(instance.guildId);
50+
expect(patched.guildId).toBe(instance.guildId);
51+
52+
expect(patched.guildId).toEqual(guildId);
53+
expect(patched.consumed).toEqual(consumed);
54+
55+
expect(patched.toJSON()).not.toEqual(data);
56+
expect(patched).toBe(instance);
57+
});
58+
});

0 commit comments

Comments
 (0)