Skip to content

Commit

Permalink
use setupHookTestScenario everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Oct 6, 2023
1 parent a7e5d98 commit d5dd0bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
43 changes: 18 additions & 25 deletions packages/react/src/discovery/__tests__/useSearchProfiles.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
import { LimitType, Profile } from '@lens-protocol/api-bindings';
import {
mockLensApolloClient,
mockProfileFragment,
mockSearchProfilesResponse,
} from '@lens-protocol/api-bindings/mocks';
import { mockProfileFragment, mockSearchProfilesResponse } from '@lens-protocol/api-bindings/mocks';
import { waitFor } from '@testing-library/react';

import { renderHookWithMocks } from '../../__helpers__/testing-library';
import { setupHookTestScenario } from '../../__helpers__/setupHookTestScenario';
import { DEFAULT_PAGINATED_QUERY_LIMIT } from '../../utils';
import { UseSearchProfilesArgs, useSearchProfiles } from '../useSearchProfiles';

function setupTestScenario({ result, ...args }: UseSearchProfilesArgs & { result: Profile[] }) {
return renderHookWithMocks(() => useSearchProfiles(args), {
mocks: {
apolloClient: mockLensApolloClient([
mockSearchProfilesResponse({
variables: {
...args,
limit: LimitType.Ten,
},
items: result,
}),
]),
},
});
}

describe(`Given the ${useSearchProfiles.name} hook`, () => {
const query = 'query_test';
const profiles = [mockProfileFragment()];
const expectations = profiles.map(({ id }) => ({ __typename: 'Profile', id }));

describe('when the query returns data successfully', () => {
it('should return profiles that match the search criteria', async () => {
const { result } = setupTestScenario({ query, result: profiles });
const { renderHook } = setupHookTestScenario([
mockSearchProfilesResponse({
variables: {
query,
limit: DEFAULT_PAGINATED_QUERY_LIMIT,
},
items: profiles,
}),
]);

await waitFor(() => expect(result.current.loading).toBeFalsy());
const args: UseSearchProfilesArgs = {
query,
};

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

await waitFor(() => expect(result.current.loading).toBeFalsy());
expect(result.current.data).toMatchObject(expectations);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,46 @@
import {
LimitType,
PrimaryPublication,
PublicationMetadataMainFocusType,
SearchPublicationType,
} from '@lens-protocol/api-bindings';
import {
mockCommentFragment,
mockLensApolloClient,
mockPostFragment,
mockSearchPublicationsResponse,
} from '@lens-protocol/api-bindings/mocks';
import { waitFor } from '@testing-library/react';

import { renderHookWithMocks } from '../../__helpers__/testing-library';
import { setupHookTestScenario } from '../../__helpers__/setupHookTestScenario';
import { DEFAULT_PAGINATED_QUERY_LIMIT } from '../../utils';
import { UseSearchPublicationsArgs, useSearchPublications } from '../useSearchPublications';

function setupTestScenario({
result,
...args
}: UseSearchPublicationsArgs & {
result: PrimaryPublication[];
}) {
return renderHookWithMocks(() => useSearchPublications(args), {
mocks: {
apolloClient: mockLensApolloClient([
mockSearchPublicationsResponse({
variables: {
...args,
limit: LimitType.Ten,
},
items: result,
}),
]),
},
});
}

describe(`Given the ${useSearchPublications.name} hook`, () => {
const query = 'query_test';
const publications = [mockPostFragment(), mockCommentFragment()];
const expectations = publications.map(({ __typename, id }) => ({ __typename, id }));

describe('when the query returns data successfully', () => {
it('should return publications that match the search result', async () => {
const { result } = setupTestScenario({
const args: UseSearchPublicationsArgs = {
query,
where: {
publicationTypes: [SearchPublicationType.Post],
metadata: {
mainContentFocus: [PublicationMetadataMainFocusType.Audio],
},
},
result: publications,
});
};

const { renderHook } = setupHookTestScenario([
mockSearchPublicationsResponse({
variables: {
...args,
limit: DEFAULT_PAGINATED_QUERY_LIMIT,
},
items: publications,
}),
]);

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

await waitFor(() => expect(result.current.loading).toBeFalsy());
expect(result.current.data).toMatchObject(expectations);
Expand Down

0 comments on commit d5dd0bf

Please sign in to comment.