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 3e65027 commit 250e3a6
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 1 deletion.
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: {},
};
}
23 changes: 23 additions & 0 deletions packages/api-bindings/src/lens/__helpers__/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
ProfilesData,
ProfilesDocument,
ProfilesVariables,
ProfileData,
ProfileDocument,
ProfileVariables,
PublicationData,
PublicationDocument,
PublicationVariables,
Expand Down Expand Up @@ -109,3 +112,23 @@ export function mockProfilesResponse({
},
};
}

export function mockProfileResponse({
variables,
profile,
}: {
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);
});
});
});
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);
});
});
});

0 comments on commit 250e3a6

Please sign in to comment.