Skip to content

Commit 5536429

Browse files
authored
fix(shared): Avoid revalidating first page on infinite pagination (#7153)
1 parent f97679a commit 5536429

File tree

5 files changed

+119
-172
lines changed

5 files changed

+119
-172
lines changed

.changeset/thirty-pumas-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/shared': patch
3+
---
4+
5+
Avoid revalidating first page on infinite pagination.

packages/clerk-js/src/test/create-fixtures.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ClerkOptions, ClientJSON, EnvironmentJSON, LoadedClerk } from '@clerk/shared/types';
2+
import { useState } from 'react';
23
import { vi } from 'vitest';
34

45
import { Clerk as ClerkCtor } from '@/core/clerk';
@@ -83,6 +84,7 @@ const unboundCreateFixtures = (
8384

8485
const MockClerkProvider = (props: any) => {
8586
const { children } = props;
87+
const [swrConfig] = useState(() => ({ provider: () => new Map() }));
8688

8789
const componentsWithoutContext = [
8890
'UsernameSection',
@@ -106,7 +108,7 @@ const unboundCreateFixtures = (
106108
<CoreClerkContextWrapper
107109
clerk={clerkMock}
108110
// Clear swr cache
109-
swrConfig={{ provider: () => new Map() }}
111+
swrConfig={swrConfig}
110112
>
111113
<EnvironmentProvider value={environmentMock}>
112114
<OptionsProvider value={optionsMock}>

packages/clerk-js/src/ui/hooks/__tests__/useCoreOrganization.test.tsx

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useOrganization } from '@clerk/shared/react';
2+
import { createDeferredPromise } from '@clerk/shared/utils/index';
23
import { describe, expect, it } from 'vitest';
34

45
import { bindCreateFixtures } from '@/test/create-fixtures';
@@ -42,7 +43,7 @@ const undefinedPaginatedResource = {
4243

4344
describe('useOrganization', () => {
4445
it('returns default values', async () => {
45-
const { wrapper } = await createFixtures(f => {
46+
const { wrapper, fixtures } = await createFixtures(f => {
4647
f.withOrganizations();
4748
f.withUser({
4849
email_addresses: ['[email protected]'],
@@ -65,10 +66,15 @@ describe('useOrganization', () => {
6566
expect(result.current.memberships).toEqual(expect.objectContaining(undefinedPaginatedResource));
6667
expect(result.current.domains).toEqual(expect.objectContaining(undefinedPaginatedResource));
6768
expect(result.current.membershipRequests).toEqual(expect.objectContaining(undefinedPaginatedResource));
69+
70+
expect(fixtures.clerk.organization?.getMemberships).not.toHaveBeenCalled();
71+
expect(fixtures.clerk.organization?.getDomains).not.toHaveBeenCalled();
72+
expect(fixtures.clerk.organization?.getMembershipRequests).not.toHaveBeenCalled();
73+
expect(fixtures.clerk.organization?.getInvitations).not.toHaveBeenCalled();
6874
});
6975

70-
it('returns null when a organization is not active ', async () => {
71-
const { wrapper } = await createFixtures(f => {
76+
it('returns null when a organization is not active', async () => {
77+
const { wrapper, fixtures } = await createFixtures(f => {
7278
f.withOrganizations();
7379
f.withUser({
7480
email_addresses: ['[email protected]'],
@@ -83,6 +89,8 @@ describe('useOrganization', () => {
8389
expect(result.current.memberships).toBeNull();
8490
expect(result.current.domains).toBeNull();
8591
expect(result.current.membershipRequests).toBeNull();
92+
93+
expect(fixtures.clerk.organization).toBeNull();
8694
});
8795

8896
describe('memberships', () => {
@@ -543,52 +551,34 @@ describe('useOrganization', () => {
543551
await waitFor(() => expect(result.current.invitations?.isLoading).toBe(false));
544552
expect(result.current.invitations?.isFetching).toBe(false);
545553

546-
fixtures.clerk.organization?.getInvitations.mockReturnValueOnce(
547-
Promise.resolve({
548-
data: [
549-
createFakeOrganizationInvitation({
550-
id: '1',
551-
emailAddress: '[email protected]',
552-
organizationId: '1',
553-
createdAt: new Date('2022-01-01'),
554-
}),
555-
createFakeOrganizationInvitation({
556-
id: '2',
557-
emailAddress: '[email protected]',
558-
organizationId: '1',
559-
createdAt: new Date('2022-01-01'),
560-
}),
561-
],
562-
total_count: 4,
563-
}),
564-
);
565-
566-
fixtures.clerk.organization?.getInvitations.mockReturnValueOnce(
567-
Promise.resolve({
568-
data: [
569-
createFakeOrganizationInvitation({
570-
id: '3',
571-
emailAddress: '[email protected]',
572-
organizationId: '1',
573-
createdAt: new Date('2022-01-01'),
574-
}),
575-
createFakeOrganizationInvitation({
576-
id: '4',
577-
emailAddress: '[email protected]',
578-
organizationId: '1',
579-
createdAt: new Date('2022-01-01'),
580-
}),
581-
],
582-
total_count: 4,
583-
}),
584-
);
554+
const deferred = createDeferredPromise();
585555

556+
fixtures.clerk.organization?.getInvitations.mockReturnValueOnce(deferred.promise);
586557
act(() => result.current.invitations?.fetchNext?.());
587558

588559
await waitFor(() => expect(result.current.invitations?.isFetching).toBe(true));
589560
expect(result.current.invitations?.isLoading).toBe(false);
590561

562+
deferred.resolve({
563+
data: [
564+
createFakeOrganizationInvitation({
565+
id: '3',
566+
emailAddress: '[email protected]',
567+
organizationId: '1',
568+
createdAt: new Date('2022-01-01'),
569+
}),
570+
createFakeOrganizationInvitation({
571+
id: '4',
572+
emailAddress: '[email protected]',
573+
organizationId: '1',
574+
createdAt: new Date('2022-01-01'),
575+
}),
576+
],
577+
total_count: 4,
578+
});
579+
591580
await waitFor(() => expect(result.current.invitations?.isFetching).toBe(false));
581+
592582
expect(result.current.invitations?.data).toEqual(
593583
expect.arrayContaining([
594584
expect.objectContaining({

0 commit comments

Comments
 (0)