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 019995d
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 59 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
17 changes: 6 additions & 11 deletions packages/react/src/profile/__tests__/useMutualFollowers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LimitType } from '@lens-protocol/api-bindings';
import {
mockProfileFragment,
mockMutualFollowersResponse,
Expand All @@ -17,22 +16,18 @@ describe(`Given the ${useMutualFollowers.name} hook`, () => {

describe('when the query returns data successfully', () => {
it('should settle with the profiles', async () => {
const args: UseMutualFollowersArgs = {
observer: observerProfileId,
viewing: viewingProfileId,
};

const { renderHook } = setupHookTestScenario([
mockMutualFollowersResponse({
variables: {
observer: observerProfileId,
viewing: viewingProfileId,
limit: LimitType.Ten,
},
variables: args,
items: profiles,
}),
]);

const args: UseMutualFollowersArgs = {
observer: observerProfileId,
viewing: viewingProfileId,
};

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

await waitFor(() => expect(result.current.loading).toBeFalsy());
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);
});
});
});
13 changes: 2 additions & 11 deletions packages/react/src/profile/useMutualFollowers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
LimitType,
Profile,
MutualFollowersRequest,
useMutualFollowers as useMutualFollowersHook,
Expand Down Expand Up @@ -27,19 +26,11 @@ export type UseMutualFollowersArgs = PaginatedArgs<MutualFollowersRequest>;
* });
* ```
*/
export function useMutualFollowers({
observer,
viewing,
limit = LimitType.Ten,
}: UseMutualFollowersArgs): PaginatedReadResult<Profile[]> {
export function useMutualFollowers(args: UseMutualFollowersArgs): PaginatedReadResult<Profile[]> {
return usePaginatedReadResult(
useMutualFollowersHook(
useLensApolloClient({
variables: {
observer,
viewing,
limit,
},
variables: args,
}),
),
);
Expand Down
54 changes: 54 additions & 0 deletions packages/react/src/publication/__tests__/usePublication.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.each([
{
description: 'when invoked with a publication id',
args: { forId: publication.id },
},
{
description: 'when invoked with a txHash',
args: { forTxHash: publication.txHash },
},
])('$description', ({ args }) => {
it('should settle with the publication data', async () => {
const { renderHook } = setupHookTestScenario([
mockPublicationResponse({
variables: {
request: args,
},
result: publication,
}),
]);

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

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

await waitFor(() => expect(result.current.loading).toBeFalsy());
expect(result.current.error).toBeInstanceOf(NotFoundError);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import {
useWhoReactedToPublication,
} from '../useWhoReactedToPublication';

describe.skip(`Given the ${useWhoReactedToPublication.name} hook`, () => {
describe(`Given the ${useWhoReactedToPublication.name} hook`, () => {
const publicationId = mockPublicationId();
const profileReactions = [mockProfileWhoReactedResultFragment()];
const expectations = profileReactions.map(({ profile, reactions }) => ({
profileId: profile.id,
profile: {
id: profile.id,
},
reactions,
}));

Expand Down

0 comments on commit 019995d

Please sign in to comment.