Skip to content

Commit

Permalink
update usages of account context
Browse files Browse the repository at this point in the history
  • Loading branch information
swain committed Nov 20, 2023
1 parent 37eee4f commit 27b0207
Show file tree
Hide file tree
Showing 39 changed files with 157 additions and 238 deletions.
18 changes: 15 additions & 3 deletions src/components/Circles/__tests__/Post.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
render,
} from '../../../common/testHelpers/testing-library-wrapper';
import { PostItem } from '../PostItem';
import { Post as PostType, Priority } from '../../../hooks';
import {
ActiveAccountProvider,
Post as PostType,
Priority,
} from '../../../hooks';

jest.unmock('@react-navigation/native');
jest.mock('../ReactionsToolbar');
Expand Down Expand Up @@ -34,15 +38,23 @@ const post: PostType = {
};

test('renders a post', () => {
const postItem = render(<PostItem post={post} onComment={jest.fn()} />);
const postItem = render(
<ActiveAccountProvider account="mockaccount">
<PostItem post={post} onComment={jest.fn()} />
</ActiveAccountProvider>,
);
expect(postItem.getByText('Announcement')).toBeDefined();
expect(postItem.getByText(post.message!)).toBeDefined();
expect(postItem.getByText(post.author!.profile.displayName)).toBeDefined();
});

test('clicking comment calls onComment', () => {
const onComment = jest.fn();
const postItem = render(<PostItem post={post} onComment={onComment} />);
const postItem = render(
<ActiveAccountProvider account="mockaccount">
<PostItem post={post} onComment={onComment} />
</ActiveAccountProvider>,
);

fireEvent.press(postItem.getByText('COMMENT'));

Expand Down
21 changes: 16 additions & 5 deletions src/components/Circles/__tests__/PostList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
} from '../../../common/testHelpers/testing-library-wrapper';
import { PostsList } from '../PostsList';
import { CircleTile } from '../../../hooks/useAppConfig';
import { useInfinitePosts, useCreatePost } from '../../../hooks';
import {
useInfinitePosts,
useCreatePost,
ActiveAccountProvider,
} from '../../../hooks';
import { CreateEditPostModal } from '../CreateEditPostModal';

jest.unmock('@react-navigation/native');
Expand All @@ -28,6 +32,13 @@ jest.mock('../../../hooks/Circles/useActiveCircleTile', () => ({
const useInfinitePostsMock = useInfinitePosts as jest.Mock;
const useCreatePostMock = useCreatePost as jest.Mock;

const renderSafe = (element: React.ReactNode) =>
render(
<ActiveAccountProvider account="mockaccount">
{element}
</ActiveAccountProvider>,
);

test('renders no post text when no posts are returned', () => {
useInfinitePostsMock.mockReturnValue({
data: {
Expand All @@ -41,7 +52,7 @@ test('renders no post text when no posts are returned', () => {
},
});

const postsList = render(<PostsList onOpenPost={jest.fn()} />);
const postsList = renderSafe(<PostsList onOpenPost={jest.fn()} />);
expect(postsList.getByText('No posts yet.')).toBeDefined();
});

Expand Down Expand Up @@ -71,7 +82,7 @@ test('renders a post', () => {
},
});

const postsList = render(<PostsList onOpenPost={jest.fn()} />);
const postsList = renderSafe(<PostsList onOpenPost={jest.fn()} />);
expect(postsList.getByText('Hello!')).toBeDefined();
expect(postsList.getByText('Joe Shmoe')).toBeDefined();
});
Expand All @@ -94,7 +105,7 @@ test('FAB shows the new post modal', () => {
mutate: mutateMock,
});

const postsList = render(
const postsList = renderSafe(
<>
<CreateEditPostModal />
<PostsList onOpenPost={jest.fn()} />
Expand Down Expand Up @@ -136,7 +147,7 @@ test('clicking post or comment calls onOpenPost with correct data', () => {
});

const onOpenPost = jest.fn();
const postsList = render(<PostsList onOpenPost={onOpenPost} />);
const postsList = renderSafe(<PostsList onOpenPost={onOpenPost} />);

fireEvent.press(postsList.getByText('Hello!'));

Expand Down
11 changes: 9 additions & 2 deletions src/components/Circles/__tests__/Thread.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
waitFor,
} from '@testing-library/react-native';
import { Thread } from '../Thread';
import { usePost, Post, useLoadReplies } from '../../../hooks';
import {
usePost,
Post,
useLoadReplies,
ActiveAccountProvider,
} from '../../../hooks';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

jest.unmock('@react-navigation/native');
Expand Down Expand Up @@ -89,7 +94,9 @@ const mockPost: Post = {
const render = (children: React.ReactNode) => {
return renderComponent(
<QueryClientProvider client={new QueryClient()}>
{children}
<ActiveAccountProvider account="mockaccount">
{children}
</ActiveAccountProvider>
</QueryClientProvider>,
);
};
Expand Down
10 changes: 7 additions & 3 deletions src/components/Circles/__tests__/ThreadComment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
render,
} from '../../../common/testHelpers/testing-library-wrapper';
import { ThreadComment } from '../ThreadComment';
import { Post } from '../../../hooks';
import { ActiveAccountProvider, Post } from '../../../hooks';

jest.unmock('@react-navigation/native');
jest.mock('date-fns', () => ({
Expand Down Expand Up @@ -39,7 +39,9 @@ const mockPost: Post = {
describe('ThreadComment', () => {
test('renders the component', () => {
const { getByText } = render(
<ThreadComment post={mockPost} onReply={jest.fn()} />,
<ActiveAccountProvider account="mockaccount">
<ThreadComment post={mockPost} onReply={jest.fn()} />
</ActiveAccountProvider>,
);

expect(getByText('Shaggy')).toBeDefined();
Expand All @@ -51,7 +53,9 @@ describe('ThreadComment', () => {
const onReply = jest.fn();

const { getByText } = render(
<ThreadComment post={mockPost} onReply={onReply} />,
<ActiveAccountProvider account="mockaccount">
<ThreadComment post={mockPost} onReply={onReply} />
</ActiveAccountProvider>,
);

fireEvent.press(getByText('REPLY'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jest.mock('../../../../hooks/useHttpClient', () => ({

jest.mock('../../../../hooks/useActiveAccount', () => ({
useActiveAccount: () => ({
account: { id: 'account-id' },
account: 'account-id',
}),
}));

Expand Down
20 changes: 8 additions & 12 deletions src/components/TrackTile/hooks/useAxiosTrackTileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const useAxiosTrackTileService = (): TrackTileService => {
const [previousUserId, setPreviousUserId] = useState(userId);
const { ontology: devConfigOntology } = useDeveloperConfig();

const accountId = account?.id || '';
const projectId = activeProject.id;
const { includePublic = false } = appConfig?.homeTab?.trackTileSettings ?? {};

Expand Down Expand Up @@ -209,7 +208,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {
};

return {
accountId,
accountId: account,
projectId,
patientId,

Expand All @@ -228,10 +227,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {

const url = `/v1/track-tiles/trackers${params && `?${params}`}`;

const res = await httpClient.get(
url,
axiosConfig({ account: accountId }),
);
const res = await httpClient.get(url, axiosConfig({ account }));
const fetchedTrackers: Tracker[] = res.data;

cache.trackers = fetchedTrackers;
Expand All @@ -243,7 +239,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {
const res = await httpClient.put<InstalledMetric>(
`/v1/track-tiles/metrics/installs/${metricId}`,
settings,
axiosConfig({ account: accountId }),
axiosConfig({ account }),
);

return updateSettingsInCache(res.data) || res.data;
Expand All @@ -253,7 +249,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {
await httpClient.put(
'/v1/track-tiles/metrics/installs',
settings,
axiosConfig({ account: accountId }),
axiosConfig({ account }),
);

settings.forEach(updateSettingsInCache);
Expand Down Expand Up @@ -283,7 +279,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {
},
query: FETCH_TRACKER_VALUES_BY_DATES_QUERY,
},
axiosConfig({ account: accountId }),
axiosConfig({ account }),
);

const trackerValues = extractTrackerValues(res.data);
Expand All @@ -302,7 +298,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {
query: DELETE_RESOURCE(resourceType),
variables: { id },
},
axiosConfig({ account: accountId }),
axiosConfig({ account }),
);

if (!res.data.data) {
Expand Down Expand Up @@ -335,7 +331,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {
: MUTATE_PROCEDURE_RESOURCE(resource.id ? 'Update' : 'Create'),
variables: { resource },
},
axiosConfig({ account: accountId }),
axiosConfig({ account }),
);

if (!res.data.data) {
Expand Down Expand Up @@ -404,7 +400,7 @@ export const useAxiosTrackTileService = (): TrackTileService => {
code: codeBelow,
},
},
axiosConfig({ account: accountId }),
axiosConfig({ account }),
);

if (!res.data.data) {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/Circles/useInfinitePosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function useInfinitePosts({ circleId }: useInfinitePostsProps) {
hasNextPage,
fetchNextPage,
} = useInfiniteQuery(['posts', circleTile?.circleId], queryPosts, {
enabled: !!accountHeaders?.['LifeOmic-Account'] && !!circleId,
enabled: !!circleId,
getNextPageParam: (lastPage) => {
return lastPage.postsV2.pageInfo.hasNextPage
? lastPage.postsV2.pageInfo.endCursor
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/Circles/useJoinCircles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function useJoinCircles() {
}
},
{
enabled: !!accountHeaders && !!data?.homeTab?.circleTiles,
enabled: !!data?.homeTab?.circleTiles,
},
);
}
46 changes: 6 additions & 40 deletions src/hooks/Circles/useLoadReplies.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import React from 'react';
import { renderHook, waitFor, act } from '@testing-library/react-native';
import { useActiveAccount } from '../useActiveAccount';
import { ActiveAccountProvider } from '../useActiveAccount';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { GraphQLClientContextProvider } from '../useGraphQLClient';
import { mockGraphQLResponse } from '../../common/testHelpers/mockGraphQLResponse';
import { Post } from './types';
import { PostRepliesQueryResponse } from './useInfinitePosts';
import { useLoadReplies } from './useLoadReplies';

jest.mock('../useActiveAccount', () => ({
useActiveAccount: jest.fn(),
}));

const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -25,47 +21,17 @@ const renderHookWithInjectedClient = () => {
return renderHook(() => useLoadReplies(), {
wrapper: ({ children }) => (
<QueryClientProvider client={queryClient}>
<GraphQLClientContextProvider baseURL={baseURL}>
{children}
</GraphQLClientContextProvider>
<ActiveAccountProvider account="unittest">
<GraphQLClientContextProvider baseURL={baseURL}>
{children}
</GraphQLClientContextProvider>
</ActiveAccountProvider>
</QueryClientProvider>
),
});
};

const useActiveAccountMock = useActiveAccount as jest.Mock;

describe('useLoadReplies', () => {
beforeEach(() => {
useActiveAccountMock.mockReturnValue({
accountHeaders: {
'LifeOmic-Account': 'unittest',
},
});
});

test('does not fetch the result if there are no account headers', () => {
useActiveAccountMock.mockReturnValue({
isFetched: true,
accountHeaders: undefined,
});

const { result } = renderHookWithInjectedClient();

act(() => {
result.current.loadReplies({
id: 'id',
replies: {
pageInfo: {
endCursor: 'next',
},
},
} as Post);
});

expect(result.current.isFetched).toEqual(false);
});

test('returns query result', async () => {
const response: PostRepliesQueryResponse = {
post: {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/Circles/useLoadReplies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useActiveCircleTile } from './useActiveCircleTile';
export const useLoadReplies = () => {
const { graphQLClient } = useGraphQLClient();
const queryClient = useQueryClient();
const { isFetched, accountHeaders } = useActiveAccount();
const { accountHeaders } = useActiveAccount();
const [queryVariables, setQueryVariables] = useState({
after: undefined as string | undefined,
id: '',
Expand All @@ -37,7 +37,7 @@ export const useLoadReplies = () => {
],
queryForPostReplies,
{
enabled: isFetched && !!accountHeaders && !!queryVariables.id,
enabled: !!queryVariables.id,
onSuccess(data) {
if (!data) {
return;
Expand Down
Loading

0 comments on commit 27b0207

Please sign in to comment.