-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathguild-switcher.test.tsx
More file actions
98 lines (79 loc) · 2.34 KB
/
guild-switcher.test.tsx
File metadata and controls
98 lines (79 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { Guild } from "@/api";
import { GuildSwitcher } from "./guild-switcher";
const mockUseGuilds = vi.fn();
const mockUseUserPreferences = vi.fn();
vi.mock("@/hooks/api/use-guilds", () => ({
useGuilds: () => mockUseGuilds(),
}));
vi.mock("@/hooks/api/use-user-preferences", () => ({
useUserPreferences: () => mockUseUserPreferences(),
}));
vi.mock("@/lib/game", () => ({
Game: {
hero: {
id: 123,
},
},
}));
const createGuild = (id: string, name: string): Guild => ({
id,
name,
icon: null,
});
describe("GuildSwitcher", () => {
beforeEach(() => {
vi.clearAllMocks();
mockUseGuilds.mockReturnValue({
data: [
createGuild("guild-1", "Alpha"),
createGuild("guild-2", "Beta"),
createGuild("guild-3", "Gamma"),
],
isFetched: true,
});
mockUseUserPreferences.mockReturnValue({
data: {
guildsOrder: [],
},
});
});
it("renders guilds in the user preferences order and appends missing guilds", () => {
mockUseUserPreferences.mockReturnValue({
data: {
guildsOrder: ["guild-2", "guild-1"],
},
});
render(<GuildSwitcher />);
expect(
screen.getAllByRole("button").map((button) => button.textContent),
).toEqual(["B", "A", "G"]);
});
it("falls back to the first ordered guild when the current selection is missing", async () => {
const handleChange = vi.fn();
mockUseUserPreferences.mockReturnValue({
data: {
guildsOrder: ["guild-2", "guild-1"],
},
});
render(<GuildSwitcher value="missing-guild" onChange={handleChange} />);
await waitFor(() => {
expect(handleChange).toHaveBeenCalledWith("guild-2");
});
});
it("scrolls horizontally when the user uses the mouse wheel", () => {
const { container } = render(<GuildSwitcher />);
const viewport = container.querySelector(
"[data-radix-scroll-area-viewport]",
);
expect(viewport).not.toBeNull();
Object.defineProperty(viewport, "scrollLeft", {
configurable: true,
value: 0,
writable: true,
});
fireEvent.wheel(viewport as HTMLElement, { deltaY: 48 });
expect((viewport as HTMLElement).scrollLeft).toBe(48);
});
});