-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathpagination.test.ts
More file actions
165 lines (146 loc) · 4.79 KB
/
pagination.test.ts
File metadata and controls
165 lines (146 loc) · 4.79 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Companies } from "../../src/api/resources/companies/client/Client";
import { Contacts } from "../../src/api/resources/contacts/client/Client";
import { Conversations } from "../../src/api/resources/conversations/client/Client";
import { Collections } from "../../src/api/resources/helpCenters/resources/collections/client/Client";
import { Notes } from "../../src/api/resources/notes/client/Client";
import { createClient } from "./utils/createClient";
import { randomString } from "./utils/random";
type TestClient = Collections | Companies | Contacts | Conversations | Notes;
describe("Pagination", () => {
const client = createClient();
interface TestCase {
name: string;
client: TestClient;
limit: number;
perPage: number;
greaterThan: number;
setup?: () => Promise<any>;
additionalParams?: Record<string, any>;
}
const testCases: TestCase[] = [
{
name: "helpCenters collections",
client: client.helpCenters.collections,
limit: 2,
perPage: 1,
greaterThan: 1,
},
{
name: "companies",
client: client.companies,
limit: 10,
perPage: 1,
greaterThan: 0,
},
{
name: "contacts",
client: client.contacts,
limit: 100,
perPage: 50,
greaterThan: 0,
},
{
name: "conversations",
client: client.conversations,
limit: 2,
perPage: 1,
greaterThan: 1,
async setup() {
const contact = await client.contacts.create({
external_id: randomString(),
});
await client.conversations.create({
from: { id: contact.id, type: "user" },
body: "Test conversation 1",
});
await client.conversations.create({
from: { id: contact.id, type: "user" },
body: "Test conversation 2",
});
return {};
},
},
{
name: "notes",
client: client.notes,
limit: 2,
perPage: 1,
greaterThan: 1,
async setup() {
const contact = await client.contacts.create({
email: `${randomString()}@test.com`,
});
await client.notes.create({
contact_id: contact.id,
body: "one",
});
await client.notes.create({
contact_id: contact.id,
body: "two",
});
return { contact_id: contact.id };
},
},
];
async function testIterator({
client,
limit,
params,
}: {
client: TestClient;
limit: number;
params: Record<string, any>;
}) {
const iterator = await client.list(params as any);
expect(iterator).toBeDefined();
let count = 0;
for await (const item of iterator) {
expect(item).toBeDefined();
expect(item.id).toBeDefined();
count++;
if (count >= limit) {
break;
}
}
return count;
}
async function testPager({
client,
limit,
params,
}: {
client: TestClient;
limit: number;
params: Record<string, any>;
}) {
const pager = await client.list(params as any);
expect(pager).toBeDefined();
let count = pager.data.length;
while (pager.hasNextPage()) {
await pager.getNextPage();
count += pager.data.length;
if (count >= limit) {
break;
}
}
return count;
}
testCases.forEach(({ name, client, limit, perPage, greaterThan, setup, additionalParams }) => {
it(name, async () => {
let params: Record<string, any> = { per_page: perPage };
if (setup) {
const setupParams = await setup();
params = { ...params, ...setupParams };
}
if (additionalParams) {
params = { ...params, ...additionalParams };
}
const iteratorCount = await testIterator({ client, limit, params: { ...params } });
const pagerCount = await testPager({ client, limit, params: { ...params } });
expect(iteratorCount).toBeGreaterThan(greaterThan);
expect(pagerCount).toBeGreaterThan(greaterThan);
// Confirm iterator and pager return same count.
expect(pagerCount).toEqual(iteratorCount);
});
});
});