From d019804ddb1fac09b50300ea9b02cecfac1109df Mon Sep 17 00:00:00 2001 From: panteliselef Date: Wed, 3 Jan 2024 21:31:13 +0200 Subject: [PATCH 1/4] fix(shared): Invitations depends on wrong options (useOrganization) --- packages/shared/src/react/hooks/useOrganization.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/react/hooks/useOrganization.tsx b/packages/shared/src/react/hooks/useOrganization.tsx index 044f1461859..7e7e4632570 100644 --- a/packages/shared/src/react/hooks/useOrganization.tsx +++ b/packages/shared/src/react/hooks/useOrganization.tsx @@ -246,8 +246,8 @@ export const useOrganization: UseOrganization = params => { }, organization?.getInvitations, { - keepPreviousData: membersSafeValues.keepPreviousData, - infinite: membersSafeValues.infinite, + keepPreviousData: invitationsSafeValues.keepPreviousData, + infinite: invitationsSafeValues.infinite, enabled: !!invitationsParams, }, { From 64fc51aa3100d66cfaaa835d9dd51a739adcb90b Mon Sep 17 00:00:00 2001 From: panteliselef Date: Wed, 3 Jan 2024 21:32:49 +0200 Subject: [PATCH 2/4] fix(shared): Invitations is depending on wrong options --- .changeset/thirty-cooks-cheer.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/thirty-cooks-cheer.md diff --git a/.changeset/thirty-cooks-cheer.md b/.changeset/thirty-cooks-cheer.md new file mode 100644 index 00000000000..e9518d8f166 --- /dev/null +++ b/.changeset/thirty-cooks-cheer.md @@ -0,0 +1,5 @@ +--- +'@clerk/shared': patch +--- + +Bug fix: Invitations from useOrganization depends on options of membership.} From 7cfb7d466fffece6775b4725fe88d07d05f465b7 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Thu, 4 Jan 2024 13:46:06 +0200 Subject: [PATCH 3/4] test(clerk-js): Add unit test --- .../__tests__/useCoreOrganization.test.tsx | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) diff --git a/packages/clerk-js/src/ui/hooks/__tests__/useCoreOrganization.test.tsx b/packages/clerk-js/src/ui/hooks/__tests__/useCoreOrganization.test.tsx index d862e650719..c5027a3a251 100644 --- a/packages/clerk-js/src/ui/hooks/__tests__/useCoreOrganization.test.tsx +++ b/packages/clerk-js/src/ui/hooks/__tests__/useCoreOrganization.test.tsx @@ -4,6 +4,7 @@ import { describe } from '@jest/globals'; import { act, renderHook, waitFor } from '../../../testUtils'; import { createFakeDomain, + createFakeOrganizationInvitation, createFakeOrganizationMembershipRequest, } from '../../components/OrganizationProfile/__tests__/utils'; import { createFakeUserOrganizationMembership } from '../../components/OrganizationSwitcher/__tests__/utlis'; @@ -16,6 +17,9 @@ const defaultRenderer = () => domains: { pageSize: 2, }, + invitations: { + pageSize: 2, + }, membershipRequests: { pageSize: 2, }, @@ -403,4 +407,204 @@ describe('useOrganization', () => { ); }); }); + + describe('invitations', () => { + it('fetch with pages', async () => { + const { wrapper, fixtures } = await createFixtures(f => { + f.withOrganizations(); + f.withUser({ + email_addresses: ['test@clerk.com'], + organization_memberships: [{ name: 'Org1', role: 'basic_member' }], + }); + }); + + fixtures.clerk.organization?.getInvitations.mockReturnValue( + Promise.resolve({ + data: [ + createFakeOrganizationInvitation({ + id: '1', + emailAddress: 'admin1@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + createFakeOrganizationInvitation({ + id: '2', + emailAddress: 'member2@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + ], + total_count: 4, + }), + ); + const { result } = renderHook(defaultRenderer, { wrapper }); + expect(result.current.invitations?.isLoading).toBe(true); + expect(result.current.invitations?.isFetching).toBe(true); + expect(result.current.invitations?.count).toBe(0); + + await waitFor(() => expect(result.current.invitations?.isLoading).toBe(false)); + + expect(result.current.invitations?.isFetching).toBe(false); + expect(result.current.invitations?.count).toBe(4); + expect(result.current.invitations?.page).toBe(1); + expect(result.current.invitations?.pageCount).toBe(2); + expect(result.current.invitations?.hasNextPage).toBe(true); + + fixtures.clerk.organization?.getInvitations.mockReturnValue( + Promise.resolve({ + data: [ + createFakeOrganizationInvitation({ + id: '3', + emailAddress: 'admin3@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + createFakeOrganizationInvitation({ + id: '4', + emailAddress: 'member4@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + ], + total_count: 4, + }), + ); + + act(() => result.current.invitations?.fetchNext?.()); + + await waitFor(() => expect(result.current.invitations?.isLoading).toBe(true)); + await waitFor(() => expect(result.current.invitations?.isLoading).toBe(false)); + + expect(result.current.invitations?.page).toBe(2); + expect(result.current.invitations?.hasNextPage).toBe(false); + expect(result.current.invitations?.data).toEqual( + expect.arrayContaining([ + expect.not.objectContaining({ + id: '1', + }), + expect.not.objectContaining({ + id: '2', + }), + expect.objectContaining({ + organizationId: '1', + id: '3', + emailAddress: 'admin3@clerk.com', + }), + expect.objectContaining({ + organizationId: '1', + id: '4', + emailAddress: 'member4@clerk.com', + }), + ]), + ); + }); + + it('infinite fetch', async () => { + const { wrapper, fixtures } = await createFixtures(f => { + f.withOrganizations(); + f.withUser({ + email_addresses: ['test@clerk.com'], + organization_memberships: [{ name: 'Org1', role: 'basic_member' }], + }); + }); + + fixtures.clerk.organization?.getInvitations.mockReturnValueOnce( + Promise.resolve({ + data: [ + createFakeOrganizationInvitation({ + id: '1', + emailAddress: 'admin1@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + createFakeOrganizationInvitation({ + id: '2', + emailAddress: 'member2@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + ], + total_count: 4, + }), + ); + const { result } = renderHook( + () => + useOrganization({ + invitations: { + pageSize: 2, + infinite: true, + }, + }), + { wrapper }, + ); + expect(result.current.invitations?.isLoading).toBe(true); + expect(result.current.invitations?.isFetching).toBe(true); + + await waitFor(() => expect(result.current.invitations?.isLoading).toBe(false)); + expect(result.current.invitations?.isFetching).toBe(false); + + fixtures.clerk.organization?.getInvitations.mockReturnValueOnce( + Promise.resolve({ + data: [ + createFakeOrganizationInvitation({ + id: '1', + emailAddress: 'admin1@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + createFakeOrganizationInvitation({ + id: '2', + emailAddress: 'member2@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + ], + total_count: 4, + }), + ); + + fixtures.clerk.organization?.getInvitations.mockReturnValueOnce( + Promise.resolve({ + data: [ + createFakeOrganizationInvitation({ + id: '3', + emailAddress: 'admin3@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + createFakeOrganizationInvitation({ + id: '4', + emailAddress: 'member4@clerk.com', + organizationId: '1', + createdAt: new Date('2022-01-01'), + }), + ], + total_count: 4, + }), + ); + + act(() => result.current.invitations?.fetchNext?.()); + + await waitFor(() => expect(result.current.invitations?.isFetching).toBe(true)); + expect(result.current.invitations?.isLoading).toBe(false); + + await waitFor(() => expect(result.current.invitations?.isFetching).toBe(false)); + expect(result.current.invitations?.data).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: '1', + }), + expect.objectContaining({ + id: '2', + }), + expect.objectContaining({ + id: '3', + }), + expect.objectContaining({ + id: '4', + }), + ]), + ); + }); + }); }); From 470ab5a9726e88c06fd14569737952c302fbe2e6 Mon Sep 17 00:00:00 2001 From: panteliselef Date: Thu, 4 Jan 2024 13:46:48 +0200 Subject: [PATCH 4/4] chore(clerk-js): Update changeset --- .changeset/thirty-cooks-cheer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/thirty-cooks-cheer.md b/.changeset/thirty-cooks-cheer.md index e9518d8f166..9537a0a571e 100644 --- a/.changeset/thirty-cooks-cheer.md +++ b/.changeset/thirty-cooks-cheer.md @@ -2,4 +2,4 @@ '@clerk/shared': patch --- -Bug fix: Invitations from useOrganization depends on options of membership.} +Fixes a bug where Invitations from `useOrganization` incorrectly depended on options for memberships.