Skip to content

Commit

Permalink
Merge pull request #560 from lifeomic/fix-failing-async-tests
Browse files Browse the repository at this point in the history
Fix Flaky Async Tests
  • Loading branch information
sternetj committed May 21, 2024
2 parents 9ef1ade + eaee15b commit 96809c3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/hooks/Circles/useJoinCircles.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const queryClient = new QueryClient({
},
});

const renderHookInContext = async () => {
const renderHookInContext = () => {
return renderHook(() => useJoinCircles(), {
wrapper: ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
Expand Down Expand Up @@ -92,7 +92,7 @@ test('joins circles listed in app-config', async () => {
axiosMock
.onPatch('/v1/life-research/projects/projectId/app-config/circles')
.reply(200, {});
const { result } = await renderHookInContext();
const { result } = renderHookInContext();
await waitFor(() => expect(result.current.isSuccess).toBeDefined());
expect(axiosMock.history.patch[0].url).toBe(
'/v1/life-research/projects/projectId/app-config/circles',
Expand All @@ -117,7 +117,7 @@ test('does nothing if all circles are joined', async () => {
},
});
axiosMock.reset();
const { result } = await renderHookInContext();
const { result } = renderHookInContext();
await waitFor(() => expect(result.current.isSuccess).toBeDefined());
await waitFor(() => expect(axiosMock.history.patch.length).toEqual(0));
});
64 changes: 36 additions & 28 deletions src/hooks/Circles/usePrivatePost.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,44 +111,52 @@ describe('useCreatePrivatePostAttachmentMutation', () => {
});

test('throws on missing asset type', async () => {
expect.assertions(1);
const { result } = renderHookWithInjectedClient();
const { mutateAsync } = result.current;

await act(async () => {
try {
await mutateAsync({
asset: {
id: 'assetId',
type: undefined, // purpose of the test
uri: 'https://some-image-url',
},
userIds: ['user1', 'user2'],
});
} catch (e) {
expect(e).toEqual(new Error("Unknown asset type, can't upload"));
}
let exception: Error | undefined;

act(() => {
mutateAsync({
asset: {
id: 'assetId',
type: undefined, // purpose of the test
uri: 'https://some-image-url',
},
userIds: ['user1', 'user2'],
}).catch((e) => {
exception = e;
});
});

await waitFor(() => {
expect(exception).toEqual(new Error("Unknown asset type, can't upload"));
});
});

test('throws on unknown asset type', async () => {
expect.assertions(1);
const { result } = renderHookWithInjectedClient();
const { mutateAsync } = result.current;

await act(async () => {
try {
await mutateAsync({
asset: {
id: 'assetId',
type: 'application/pdf', // purpose of the test
uri: 'https://some-image-url',
},
userIds: ['user1', 'user2'],
});
} catch (e) {
expect(e).toEqual(new Error('Unsupported file type: application/pdf'));
}
let exception: Error | undefined;

act(() => {
mutateAsync({
asset: {
id: 'assetId',
type: 'application/pdf', // purpose of the test
uri: 'https://some-image-url',
},
userIds: ['user1', 'user2'],
}).catch((e) => {
exception = e;
});
});

await waitFor(() => {
expect(exception).toEqual(
new Error('Unsupported file type: application/pdf'),
);
});
});
});
34 changes: 16 additions & 18 deletions src/hooks/useFhirClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,13 @@ describe('useCreateResourceMutation', () => {
value: 73.5,
},
};
await act(async () => {
await result.current.mutateAsync(observation);
act(() => {
result.current.mutateAsync(observation);
});

await waitFor(() => {
expect(result.current.data).toEqual(resultBundle);
});
expect(axiosMock.history.post[0].url).toBe('/v1/fhir/dstu3/Observation');
expect(JSON.parse(axiosMock.history.post[0].data)).toEqual({
...observation,
Expand All @@ -403,9 +406,6 @@ describe('useCreateResourceMutation', () => {
],
},
});
await waitFor(() => {
expect(result.current.data).toEqual(resultBundle);
});
});
});

Expand Down Expand Up @@ -462,10 +462,13 @@ describe('useUpsertMutation', () => {
value: 1.8,
},
};
await act(async () => {
await result.current.mutateAsync([observation1, observation2]);
act(() => {
result.current.mutateAsync([observation1, observation2]);
});

await waitFor(() => {
expect(result.current.data).toEqual(resultBundle);
});
expect(axiosMock.history.post[0].url).toBe(
'https://fhir.us.lifeomic.com/acct1/dstu3',
);
Expand Down Expand Up @@ -511,9 +514,6 @@ describe('useUpsertMutation', () => {
},
],
});
await waitFor(() => {
expect(result.current.data).toEqual(resultBundle);
});
});
});

Expand All @@ -529,19 +529,17 @@ describe('useDeleteResourceMutation', () => {
return useDeleteResourceMutation();
}
const { result } = renderHookInContext(useTestHook);
await act(async () => {
await result.current.mutateAsync({
act(() => {
result.current.mutateAsync({
id: observationId,
resourceType: 'Observation',
});
});

await act(async () => {
await waitFor(() => {
expect(axiosMock.history.delete[0].url).toBe(
`/v1/fhir/dstu3/Observation/${observationId}`,
);
});
await waitFor(() => {
expect(axiosMock.history.delete[0].url).toBe(
`/v1/fhir/dstu3/Observation/${observationId}`,
);
});
});
});

0 comments on commit 96809c3

Please sign in to comment.