/[sudobot]/trunk/tests/framework/arguments/GuildMemberArgument.test.ts
ViewVC logotype

Contents of /trunk/tests/framework/arguments/GuildMemberArgument.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (show annotations)
Mon Jul 29 17:59:26 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2882 byte(s)
chore: add trunk
1 import { Casted } from "@framework/arguments/Argument";
2 import GuildMemberArgument from "@framework/arguments/GuildMemberArgument";
3 import * as entityUtils from "@framework/utils/entities";
4 import { Guild, GuildMember, Snowflake, User } from "discord.js";
5 import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6 import { createApplication } from "../../mocks/application.mock";
7 import { randomSnowflake } from "../../mocks/snowflakes";
8 import { initialize } from "./ArgumentTestUtils";
9
10 const fetchMember = vi
11 .spyOn(entityUtils, "fetchMember")
12 .mockImplementation(async (guild: Guild, memberId: Snowflake) => {
13 if (memberId === "11111111111111111") {
14 return null;
15 }
16
17 return {
18 [Symbol.hasInstance]: (obj: unknown) => obj === GuildMember,
19 id: memberId,
20 guild,
21 user: {
22 id: memberId
23 }
24 } as unknown as GuildMember;
25 });
26
27 describe("GuildMemberArgument", () => {
28 createApplication();
29
30 let userId: Snowflake, guildId: Snowflake;
31 let data: ReturnType<typeof initialize>;
32
33 beforeEach(() => {
34 userId = randomSnowflake();
35 guildId = randomSnowflake();
36 });
37
38 afterEach(() => {
39 fetchMember.mockClear();
40 });
41
42 it("should parse a guild member argument with mentions", async () => {
43 data = initialize({
44 content: `test <@${userId}>`,
45 userId,
46 guildId,
47 prefix: "!"
48 });
49
50 const result = (await GuildMemberArgument.performCast(
51 data.context,
52 data.content,
53 data.argv,
54 data.argv[1],
55 0,
56 "testarg",
57 undefined,
58 true
59 )) as Casted<User>;
60
61 expect(result.value?.getRawValue()).toBe(`<@${userId}>`);
62 expect(result.value?.getValue().id).toBe(userId);
63 expect(result.error).toBeUndefined();
64 expect(fetchMember).toBeCalledWith(data.message.guild, userId);
65 expect(fetchMember).toBeCalledTimes(1);
66 });
67
68 it("should parse a guild member argument with snowflake IDs", async () => {
69 data = initialize({
70 content: `test ${userId}`,
71 userId,
72 guildId,
73 prefix: "!"
74 });
75
76 const result = (await GuildMemberArgument.performCast(
77 data.context,
78 data.content,
79 data.argv,
80 data.argv[1],
81 0,
82 "testarg",
83 undefined,
84 true
85 )) as Casted<User>;
86
87 expect(result.value?.getRawValue()).toBe(`${userId}`);
88 expect(result.value?.getValue().id).toBe(userId);
89 expect(result.error).toBeUndefined();
90 expect(fetchMember).toBeCalledWith(data.message.guild, userId);
91 expect(fetchMember).toBeCalledTimes(1);
92 });
93 });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26