Skip to content

Commit

Permalink
add tests for useProfile and usePublication
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Oct 9, 2023
1 parent 17b1b2c commit 695ee46
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 35 deletions.
3 changes: 2 additions & 1 deletion packages/api-bindings/src/apollo/cache/createTypePolicies.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TypePolicy } from '@apollo/client';

import { StrictTypedTypePolicies } from '../../lens';
import { createFeedFieldPolicy } from './createFeedFieldPolicy';
import { createPublicationTypePolicy } from './createPublicationTypePolicy';
import { createQueryParamsLocalFields, QueryParams } from './createQueryParamsLocalFields';
import {
createFeedFieldPolicy,
createFollowersFieldPolicy,
createFollowingFieldPolicy,
createMutualFollowersFieldPolicy,
Expand Down Expand Up @@ -50,6 +50,7 @@ export function createTypePolicies(
searchPublications: createSearchPublicationsFieldPolicy(),
whoActedOnPublication: createWhoActedOnPublicationFieldPolicy(),
whoReactedPublication: createWhoReactedPublicationFieldPolicy(),

...createQueryParamsLocalFields(params),
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cursorBasedPagination } from './utils/cursorBasedPagination';
import { cursorBasedPagination } from '../utils/cursorBasedPagination';

export function createFeedFieldPolicy() {
return cursorBasedPagination([['request', ['where', ['feedEventItemTypes', 'metadata', 'for']]]]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './createFeedFieldPolicy';
export * from './createFollowersFieldPolicy';
export * from './createFollowingFieldPolicy';
export * from './createMutualFollowersFieldPolicy';
Expand Down
36 changes: 18 additions & 18 deletions packages/api-bindings/src/lens/__helpers__/queries/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ import {
import { mockPaginatedResultInfo } from '../fragments';
import { mockAnyPaginatedResponse, mockAnyResponse } from './mockAnyPaginatedResponse';

export function mockProfileResponse({
variables,
result,
}: {
variables: ProfileVariables;
result: Profile | null;
}) {
return mockAnyResponse({
request: {
query: ProfileDocument,
variables,
},
result: {
data: { result },
},
});
}

export function mockProfilesResponse({
variables,
items,
Expand Down Expand Up @@ -187,21 +205,3 @@ export function mockWhoReactedToPublicationResponse({
query: WhoReactedPublicationDocument,
});
}

export function mockProfileResponse({
variables,
result,
}: {
variables: ProfileVariables;
result: Profile | null;
}) {
return mockAnyResponse({
request: {
query: ProfileDocument,
variables,
},
result: {
data: { result },
},
});
}
21 changes: 6 additions & 15 deletions packages/api-bindings/src/lens/__helpers__/queries/publication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { faker } from '@faker-js/faker';

import { Cursor } from '../../Cursor';
import {
PaginatedResultInfo,
PublicationDocument,
Expand All @@ -12,30 +9,24 @@ import {
} from '../../graphql/generated';
import { AnyPublication, PrimaryPublication } from '../../publication';
import { mockPaginatedResultInfo } from '../fragments';
import { mockAnyPaginatedResponse } from './mockAnyPaginatedResponse';

export function mockCursor(): Cursor {
return faker.random.alphaNumeric(10) as Cursor;
}
import { mockAnyPaginatedResponse, mockAnyResponse } from './mockAnyPaginatedResponse';

export function mockPublicationResponse({
variables,
publication,
result,
}: {
variables: PublicationVariables;
publication: AnyPublication | null;
result: AnyPublication | null;
}) {
return {
return mockAnyResponse({
request: {
query: PublicationDocument,
variables,
},
result: {
data: {
result: publication,
},
data: { result },
},
};
});
}

export function mockPublicationsResponse({
Expand Down
54 changes: 54 additions & 0 deletions packages/react/src/profile/__tests__/useProfile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { mockProfileFragment, mockProfileResponse } from '@lens-protocol/api-bindings/mocks';
import { waitFor } from '@testing-library/react';

import { NotFoundError } from '../../NotFoundError';
import { setupHookTestScenario } from '../../__helpers__/setupHookTestScenario';
import { useProfile } from '../useProfile';

describe(`Given the ${useProfile.name} hook`, () => {
const profile = mockProfileFragment();
const expectations = { __typename: 'Profile', id: profile.id };

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 { renderHook } = setupHookTestScenario([
mockProfileResponse({
variables: {
request: args,
},
result: profile,
}),
]);

const { result } = renderHook(() => useProfile(args));

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 { renderHook } = setupHookTestScenario([
mockProfileResponse({
variables: {
request: args,
},
result: null,
}),
]);

const { result } = renderHook(() => useProfile(args));

await waitFor(() => expect(result.current.loading).toBeFalsy());
expect(result.current.error).toBeInstanceOf(NotFoundError);
});
});
});
51 changes: 51 additions & 0 deletions packages/react/src/publication/__tests__/usePublication.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { mockPostFragment, mockPublicationResponse } from '@lens-protocol/api-bindings/mocks';
import { waitFor } from '@testing-library/react';

import { NotFoundError } from '../../NotFoundError';
import { setupHookTestScenario } from '../../__helpers__/setupHookTestScenario';
import { usePublication } from '../usePublication';

describe(`Given the ${usePublication.name} hook`, () => {
const publication = mockPostFragment();
const expectations = { __typename: 'Post', id: publication.id };

describe('when the queried publication exists', () => {
const { renderHook } = setupHookTestScenario([
mockPublicationResponse({
variables: {
request: {
forId: publication.id,
},
},
result: 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 } = setupHookTestScenario([
mockPublicationResponse({
variables: {
request: {
forId: publication.id,
},
},
result: 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);
});
});
});

0 comments on commit 695ee46

Please sign in to comment.