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 4, 2023
1 parent 2e70116 commit c0fa571
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { StrictTypedTypePolicies } from '../../lens';

export function createProfileTypePolicy(): StrictTypedTypePolicies['Profile'] {
return {
fields: {},
};
}
26 changes: 25 additions & 1 deletion packages/api-bindings/src/lens/__helpers__/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { faker } from '@faker-js/faker';
import { Cursor } from '../Cursor';
import {
PaginatedResultInfo,
Profile,
ProfileData,
ProfileDocument,
ProfileVariables,
PublicationData,
PublicationDocument,
PublicationVariables,
Expand All @@ -12,7 +16,7 @@ import {
PublicationsVariables,
} from '../graphql/generated';
import { AnyPublication } from '../utils';
import { mockPaginatedResultInfo, mockPostFragment } from './fragments';
import { mockPaginatedResultInfo, mockPostFragment, mockProfileFragment } from './fragments';

export function mockCursor(): Cursor {
return faker.random.alphaNumeric(10) as Cursor;
Expand Down Expand Up @@ -104,3 +108,23 @@ export function mockGetPublicationsResponse({
},
};
}

export function mockProfileResponse({
variables,
profile = mockProfileFragment(),
}: {
variables: ProfileVariables;
profile?: Profile;
}): MockedResponse<ProfileData> {
return {
request: {
query: ProfileDocument,
variables,
},
result: {
data: {
result: profile,
},
},
};
}
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
83 changes: 83 additions & 0 deletions packages/react/src/profile/__tests__/useProfile.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
4 changes: 2 additions & 2 deletions packages/react/src/profile/useProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export function useProfile({
variables: useMediaTransformFromConfig(
useProfileStatsArgFromConfig({
request: {
forHandle,
forProfileId,
...(forHandle && { forHandle }),
...(forProfileId && { forProfileId }),
},
}),
),
Expand Down
91 changes: 91 additions & 0 deletions packages/react/src/publication/__tests__/usePublication.spec.ts
Original file line number Diff line number Diff line change
@@ -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<TProps, TResult>(
callback: (props: TProps) => TResult,
): RenderHookResult<TResult, TProps> {
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);
});
});
});
4 changes: 2 additions & 2 deletions packages/react/src/publication/usePublication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function usePublication({
variables: useMediaTransformFromConfig(
useProfileStatsArgFromConfig({
request: {
forId,
forTxHash,
...(forId && { forId }),
...(forTxHash && { forTxHash }),
},
}),
),
Expand Down

0 comments on commit c0fa571

Please sign in to comment.