/[sudobot]/trunk/tests/framework/pagination/Pagination.test.ts
ViewVC logotype

Contents of /trunk/tests/framework/pagination/Pagination.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 632 - (show annotations)
Thu Oct 10 17:53:11 2024 UTC (5 months, 2 weeks ago) by rakinar2
File MIME type: application/typescript
File size: 5595 byte(s)
chore: synchronize

1 import Pagination from "@framework/widgets/Pagination";
2 import { describe, expect, it, vi } from "vitest";
3 import { createApplication } from "../../mocks/application.mock";
4
5 describe("Pagination", () => {
6 createApplication();
7
8 const emojiResolver = (_name: string) => {
9 return undefined;
10 };
11
12 it("should initialize with default values", () => {
13 const pagination = new Pagination(undefined, emojiResolver);
14
15 expect(pagination.state).toEqual({ page: 1 });
16 expect(pagination.limit).toBe(10);
17 });
18
19 it("should set fetcher correctly", () => {
20 const fetcher = vi.fn();
21 const pagination = new Pagination(undefined, emojiResolver).setFetcher(fetcher);
22
23 expect(pagination["_fetcher"]).toBe(fetcher);
24 });
25
26 it("should set count getter correctly", () => {
27 const getCount = vi.fn();
28 const pagination = new Pagination(undefined, emojiResolver).setCountGetter(getCount);
29
30 expect(pagination["_getCount"]).toBe(getCount);
31 });
32
33 it("should set initial message correctly", () => {
34 const message = {} as any;
35 const pagination = new Pagination(undefined, emojiResolver).setInitialMessage(message);
36
37 expect(pagination["_initialMessage"]).toBe(message);
38 });
39
40 it("should set data correctly", () => {
41 const data = [1, 2, 3];
42 const pagination = new Pagination(undefined, emojiResolver).setData(data);
43
44 expect(pagination["_cachedData"]).toBe(data);
45 });
46
47 it("should set limit correctly", () => {
48 const limit = 20;
49 const pagination = new Pagination(undefined, emojiResolver).setLimit(limit);
50
51 expect(pagination["_limit"]).toBe(limit);
52 });
53
54 it("should set message options builder correctly", () => {
55 const builder = vi.fn();
56 const pagination = new Pagination(undefined, emojiResolver).setMessageOptionsBuilder(
57 builder
58 );
59
60 expect(pagination["_builder"]).toBe(builder);
61 });
62
63 it("should set action row builder correctly", () => {
64 const builder = vi.fn();
65 const pagination = new Pagination(undefined, emojiResolver).setActionRowBuilder(builder);
66
67 expect(pagination["_actionRowBuilder"]).toBe(builder);
68 });
69
70 it("should calculate offset correctly", () => {
71 const pagination = new Pagination(undefined, emojiResolver).setLimit(10);
72
73 expect(pagination["calculateOffset"].call(pagination, 1)).toBe(0);
74 expect(pagination["calculateOffset"].call(pagination, 2)).toBe(10);
75 expect(pagination["calculateOffset"].call(pagination, 3)).toBe(20);
76 });
77
78 it("should get slice correctly", async () => {
79 const data = [1, 2, 3, 4, 5];
80 const pagination = new Pagination(undefined, emojiResolver).setData(data).setLimit(2);
81
82 expect(await pagination["getSlice"].call(pagination, 1)).toEqual([1, 2]);
83 expect(await pagination["getSlice"].call(pagination, 2)).toEqual([3, 4]);
84 expect(await pagination["getSlice"].call(pagination, 3)).toEqual([5]);
85 });
86
87 it("should get message options correctly", async () => {
88 const data = [1, 2, 3];
89 const builder = vi.fn().mockResolvedValue({});
90 const pagination = new Pagination(undefined, emojiResolver)
91 .setData(data)
92 .setMessageOptionsBuilder(builder);
93
94 await pagination["getMessageOptions"].call(pagination);
95
96 expect(builder).toHaveBeenCalledWith({
97 data,
98 pagination,
99 state: pagination.state,
100 maxPages: await pagination["calculateMaxPages"].call(pagination),
101 page: pagination.state.page
102 });
103 });
104
105 it("should destroy pagination correctly", async () => {
106 const message = {
107 edit: vi.fn()
108 } as any;
109 const pagination = new Pagination(undefined, emojiResolver).setInitialMessage(message);
110 pagination["getMessageOptions"] = vi.fn().mockResolvedValue({});
111
112 await pagination.destroy();
113
114 expect(pagination["_destroyed"]).toBe(true);
115 expect(pagination["_timeout"]).toBeUndefined();
116 expect(pagination["_initialMessage"]?.edit).toHaveBeenCalledWith({});
117 });
118
119 it("should calculate max pages correctly", async () => {
120 const getCount = vi.fn().mockResolvedValue(15);
121 const pagination = new Pagination(undefined, emojiResolver)
122 .setCountGetter(getCount)
123 .setLimit(5);
124
125 expect(await pagination["calculateMaxPages"].call(pagination)).toBe(3);
126 });
127
128 it("should get action row correctly", async () => {
129 const pagination = new Pagination(undefined, emojiResolver).setLimit(10);
130 pagination["calculateMaxPages"] = vi.fn().mockResolvedValue(3);
131
132 const actionRow = await pagination["getActionRow"].call(pagination);
133
134 expect(actionRow).toBeDefined();
135 expect(actionRow.components.length).toBe(4);
136 });
137
138 it("should create pagination with fetcher", () => {
139 const fetcher = vi.fn();
140 const pagination = Pagination.withFetcher(fetcher);
141
142 expect(pagination["_fetcher"]).toBe(fetcher);
143 });
144
145 it("should create pagination with data", () => {
146 const data = [1, 2, 3];
147 const pagination = Pagination.withData(data);
148
149 expect(pagination["_cachedData"]).toBe(data);
150 });
151
152 it("should create pagination with data using 'of' method", () => {
153 const data = [1, 2, 3];
154 const pagination = Pagination.of(data);
155
156 expect(pagination["_cachedData"]).toBe(data);
157 });
158 });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26