From 1519bf312123b4f4231a7ec1e7d3e5c654daa1e7 Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Fri, 29 Sep 2023 14:35:59 +0200 Subject: [PATCH] add tests for useProfile and usePublication --- .../apollo/cache/createProfileTypePolicy.ts | 7 ++ .../src/lens/__helpers__/queries.ts | 23 +++++ packages/react/package.json | 2 +- .../src/profile/__tests__/useProfile.spec.ts | 83 +++++++++++++++++ .../__tests__/usePublication.spec.ts | 91 +++++++++++++++++++ 5 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 packages/api-bindings/src/apollo/cache/createProfileTypePolicy.ts create mode 100644 packages/react/src/profile/__tests__/useProfile.spec.ts create mode 100644 packages/react/src/publication/__tests__/usePublication.spec.ts diff --git a/packages/api-bindings/src/apollo/cache/createProfileTypePolicy.ts b/packages/api-bindings/src/apollo/cache/createProfileTypePolicy.ts new file mode 100644 index 0000000000..6dae02f95f --- /dev/null +++ b/packages/api-bindings/src/apollo/cache/createProfileTypePolicy.ts @@ -0,0 +1,7 @@ +import { StrictTypedTypePolicies } from '../../lens'; + +export function createProfileTypePolicy(): StrictTypedTypePolicies['Profile'] { + return { + fields: {}, + }; +} diff --git a/packages/api-bindings/src/lens/__helpers__/queries.ts b/packages/api-bindings/src/lens/__helpers__/queries.ts index dd15752c97..fd285dd069 100644 --- a/packages/api-bindings/src/lens/__helpers__/queries.ts +++ b/packages/api-bindings/src/lens/__helpers__/queries.ts @@ -8,6 +8,9 @@ import { ProfilesData, ProfilesDocument, ProfilesVariables, + ProfileData, + ProfileDocument, + ProfileVariables, PublicationData, PublicationDocument, PublicationVariables, @@ -42,6 +45,26 @@ export function mockPublicationResponse({ }; } +export function mockProfileResponse({ + variables, + profile, +}: { + variables: ProfileVariables; + profile: Profile; +}): MockedResponse { + return { + request: { + query: ProfileDocument, + variables, + }, + result: { + data: { + result: profile, + }, + }, + }; +} + export function mockPublicationsResponse({ variables, publications, diff --git a/packages/react/package.json b/packages/react/package.json index c86fc9d72a..c1812afaf6 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -29,7 +29,7 @@ "lint:fix": "pnpm run prettier:fix && pnpm run eslint:fix && pnpm run tsc", "prettier:fix": "prettier --write .", "prettier": "prettier --check .", - "test": "exit 0", + "test": "jest", "test:watch": "jest --watch", "tsc": "tsc --noEmit" }, diff --git a/packages/react/src/profile/__tests__/useProfile.spec.ts b/packages/react/src/profile/__tests__/useProfile.spec.ts new file mode 100644 index 0000000000..1da48cd3dc --- /dev/null +++ b/packages/react/src/profile/__tests__/useProfile.spec.ts @@ -0,0 +1,83 @@ +import { Profile } from '@lens-protocol/api-bindings'; +import { + mockLensApolloClient, + mockProfileResponse, + mockProfileFragment, + mockSources, + simulateNotAuthenticated, +} from '@lens-protocol/api-bindings/mocks'; +import { waitFor } from '@testing-library/react'; + +import { NotFoundError } from '../../NotFoundError'; +import { renderHookWithMocks } from '../../__helpers__/testing-library'; +import { + defaultMediaTransformsConfig, + mediaTransformConfigToQueryVariables, +} from '../../mediaTransforms'; +import { useProfile, UseProfileArgs } from '../useProfile'; + +function setupTestScenario({ profile, ...args }: UseProfileArgs & { profile: Profile | null }) { + const sources = mockSources(); + + return renderHookWithMocks(() => useProfile(args), { + mocks: { + sources, + mediaTransforms: defaultMediaTransformsConfig, + apolloClient: mockLensApolloClient([ + mockProfileResponse({ + profile, + variables: { + request: args.forProfileId + ? { + forProfileId: args.forProfileId, + } + : { + forHandle: args.forHandle, + }, + ...mediaTransformConfigToQueryVariables(defaultMediaTransformsConfig), + }, + }), + ]), + }, + }); +} + +describe(`Given the ${useProfile.name} hook`, () => { + const profile = mockProfileFragment(); + const expectations = { __typename: 'Profile', id: profile.id }; + + beforeAll(() => { + simulateNotAuthenticated(); + }); + + describe.each([ + { + description: 'when invoked with a profile id', + args: { forProfileId: profile.id }, + }, + { + description: 'when invoked with a profile handle', + args: { forHandle: profile.handle }, + }, + ])('$description', ({ args }) => { + it('should settle with the profile data', async () => { + const { result } = setupTestScenario({ + ...args, + profile, + }); + + await waitFor(() => expect(result.current.loading).toBeFalsy()); + expect(result.current.data).toMatchObject(expectations); + }); + + it(`should settle with a ${NotFoundError.name} if not found`, async () => { + const { result } = setupTestScenario({ + ...args, + profile: null, + }); + + await waitFor(() => expect(result.current.loading).toBeFalsy()); + expect(result.current.error).toBeInstanceOf(NotFoundError); + }); + }); +}); diff --git a/packages/react/src/publication/__tests__/usePublication.spec.ts b/packages/react/src/publication/__tests__/usePublication.spec.ts new file mode 100644 index 0000000000..89bb32fc42 --- /dev/null +++ b/packages/react/src/publication/__tests__/usePublication.spec.ts @@ -0,0 +1,91 @@ +import { MockedResponse } from '@apollo/client/testing'; +import { + mockLensApolloClient, + mockPostFragment, + mockSources, + mockPublicationResponse, + simulateNotAuthenticated, +} from '@lens-protocol/api-bindings/mocks'; +import { RenderHookResult, waitFor } from '@testing-library/react'; + +import { NotFoundError } from '../../NotFoundError'; +import { renderHookWithMocks } from '../../__helpers__/testing-library'; +import { + defaultMediaTransformsConfig, + mediaTransformConfigToQueryVariables, +} from '../../mediaTransforms'; +import { usePublication } from '../usePublication'; + +const sources = mockSources(); + +function setupTestScenario(mocks: MockedResponse[]) { + const client = mockLensApolloClient(mocks); + + return { + renderHook( + callback: (props: TProps) => TResult, + ): RenderHookResult { + return renderHookWithMocks(callback, { + mocks: { + sources, + mediaTransforms: defaultMediaTransformsConfig, + apolloClient: client, + }, + }); + }, + }; +} + +describe(`Given the ${usePublication.name} hook`, () => { + const publication = mockPostFragment(); + const expectations = { __typename: 'Post', id: publication.id }; + + beforeAll(() => { + simulateNotAuthenticated(); + }); + + describe('when the queried publication exists', () => { + const { renderHook } = setupTestScenario([ + mockPublicationResponse({ + variables: { + request: { + forId: publication.id, + }, + profileStatsArg: { + forApps: sources, + }, + ...mediaTransformConfigToQueryVariables(defaultMediaTransformsConfig), + }, + publication, + }), + ]); + + it('should settle with the publication data', async () => { + const { result } = renderHook(() => usePublication({ forId: publication.id })); + + await waitFor(() => expect(result.current.loading).toBeFalsy()); + expect(result.current.data).toMatchObject(expectations); + }); + }); + + describe('when the queried publication does not exist', () => { + const { renderHook } = setupTestScenario([ + mockPublicationResponse({ + variables: { + request: { + forId: publication.id, + }, + ...mediaTransformConfigToQueryVariables(defaultMediaTransformsConfig), + }, + publication: null, + }), + ]); + + it(`should settle with a ${NotFoundError.name} state`, async () => { + const { result } = renderHook(() => usePublication({ forId: publication.id })); + + await waitFor(() => expect(result.current.loading).toBeFalsy()); + expect(result.current.error).toBeInstanceOf(NotFoundError); + }); + }); +});