Skip to content

Commit 4b7ea36

Browse files
committed
fixed pagination tests
1 parent 05e4aa1 commit 4b7ea36

3 files changed

Lines changed: 27 additions & 23 deletions

File tree

packages/contentstack-export-to-csv/src/utils/api-client.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,18 @@ async function getUsers(
169169
try {
170170
const users = await managementAPIClient.organization(organization.uid).getInvitations(params) as unknown as OrgUsersResponse;
171171

172-
if (!users.items || (users.items && !users.items.length)) {
172+
if (!users.items || users.items.length < params.limit) {
173+
if (users.items?.length) {
174+
result = result.concat(users.items);
175+
}
173176
return result;
174-
} else {
175-
result = result.concat(users.items);
176-
params.skip = params.page * params.limit;
177-
params.page++;
178-
await wait(200);
179-
return getUsers(managementAPIClient, organization, params, result);
180177
}
178+
179+
result = result.concat(users.items);
180+
params.skip = params.page * params.limit;
181+
params.page++;
182+
await wait(200);
183+
return getUsers(managementAPIClient, organization, params, result);
181184
} catch {
182185
return result;
183186
}

packages/contentstack-export-to-csv/test/unit/utils/api-client.functional.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ describe('api-client functional', () => {
131131
}),
132132
} as any;
133133
const res = await getOrgUsers(client, 'org-1');
134-
expect(res).to.equal(invitations);
134+
expect(res.items).to.have.lengthOf(1);
135+
expect(res.items[0].email).to.equal('a@b.com');
135136
});
136137

137138
it('getOrgUsers rejects when org uid missing', async () => {
@@ -148,8 +149,8 @@ describe('api-client functional', () => {
148149

149150
it('getOrgUsers resolves paginated invitations for non-owner admin org', async () => {
150151
const getInvitations = sandbox.stub();
151-
getInvitations.onFirstCall().resolves({ items: [{ email: 'a@b.com' }] });
152-
getInvitations.onSecondCall().resolves({ items: [] });
152+
getInvitations.onFirstCall().resolves({ items: Array.from({ length: 100 }, (_, i) => ({ email: `u${i}@b.com` })) });
153+
getInvitations.onSecondCall().resolves({ items: [{ email: 'a@b.com' }] });
153154
const client = {
154155
getUser: () =>
155156
Promise.resolve({
@@ -160,7 +161,7 @@ describe('api-client functional', () => {
160161
}),
161162
} as any;
162163
const res = await getOrgUsers(client, 'org-1');
163-
expect(res.items).to.have.lengthOf(1);
164+
expect(res.items).to.have.lengthOf(101);
164165
expect(getInvitations.calledTwice).to.equal(true);
165166
});
166167

packages/contentstack-export-to-csv/test/unit/utils/api-client.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@ describe('api-client', () => {
108108

109109
describe('getOrgUsers', () => {
110110
it('should paginate getInvitations for organization owners', async () => {
111-
const page1 = Array.from({ length: 10 }, (_, i) => makeUser(i));
112-
const page2 = Array.from({ length: 10 }, (_, i) => makeUser(i + 10));
113-
const page3 = Array.from({ length: 5 }, (_, i) => makeUser(i + 20));
111+
const page1 = Array.from({ length: config.limit }, (_, i) => makeUser(i));
112+
const page2 = Array.from({ length: config.limit }, (_, i) => makeUser(i + config.limit));
113+
const page3 = Array.from({ length: 25 }, (_, i) => makeUser(i + config.limit * 2));
114114

115115
const { client, getInvitations, invitationParams } = createPaginatedMockClient(
116116
{ is_owner: true },
117-
[page1, page2, page3, []],
117+
[page1, page2, page3],
118118
);
119119

120120
const result = await getOrgUsers(client, ORG_UID);
121121

122-
expect(result.items).to.have.lengthOf(25);
123-
expect(getInvitations.callCount).to.equal(4);
122+
expect(result.items).to.have.lengthOf(225);
123+
expect(getInvitations.callCount).to.equal(3);
124124
expect(invitationParams[0]).to.deep.equal({ skip: 0, page: 1, limit: config.limit });
125125
expect(invitationParams[1]).to.deep.equal({ skip: config.limit, page: 2, limit: config.limit });
126126
expect(invitationParams[2]).to.deep.equal({
@@ -131,19 +131,19 @@ describe('api-client', () => {
131131
});
132132

133133
it('should paginate getInvitations for organization admins', async () => {
134-
const page1 = Array.from({ length: 10 }, (_, i) => makeUser(i));
135-
const page2 = Array.from({ length: 10 }, (_, i) => makeUser(i + 10));
136-
const page3 = Array.from({ length: 5 }, (_, i) => makeUser(i + 20));
134+
const page1 = Array.from({ length: config.limit }, (_, i) => makeUser(i));
135+
const page2 = Array.from({ length: config.limit }, (_, i) => makeUser(i + config.limit));
136+
const page3 = Array.from({ length: 25 }, (_, i) => makeUser(i + config.limit * 2));
137137

138138
const { client, getInvitations, invitationParams } = createPaginatedMockClient(
139139
{ is_owner: false, org_roles: [{ admin: true }] },
140-
[page1, page2, page3, []],
140+
[page1, page2, page3],
141141
);
142142

143143
const result = await getOrgUsers(client, ORG_UID);
144144

145-
expect(result.items).to.have.lengthOf(25);
146-
expect(getInvitations.callCount).to.equal(4);
145+
expect(result.items).to.have.lengthOf(225);
146+
expect(getInvitations.callCount).to.equal(3);
147147
expect(invitationParams[0]).to.deep.equal({ skip: 0, page: 1, limit: config.limit });
148148
});
149149

0 commit comments

Comments
 (0)