Skip to content

Commit

Permalink
Add test to ensure errorPolicy: 'ignore' works as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 5, 2023
1 parent f295b5c commit 64b23a0
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/react/query-preloader/__tests__/createQueryPreloader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,70 @@ test("returns error when error policy is 'all'", async () => {
dispose();
});

test("discards error when error policy is 'none'", async () => {
// Disable error messages shown by React when an error is thrown to an error
// boundary
using _consoleSpy = spyOnConsole("error");
const { query } = useSimpleCase();
const mocks = [
{ request: { query }, result: { errors: [new GraphQLError("Oops")] } },
];
const client = createDefaultClient(mocks);
const Profiler = createProfiler({
initialSnapshot: {
result: null as UseReadQueryResult<SimpleCaseData | undefined> | null,
error: null as Error | null,
},
});

const preloadQuery = createQueryPreloader(client);
const [queryRef, dispose] = preloadQuery(query, { errorPolicy: "ignore" });

const { SuspenseFallback, ReadQueryHook } = createDefaultProfiledComponents(
Profiler,
queryRef
);

function ErrorFallback({ error }: { error: Error }) {
useTrackRenders();
Profiler.mergeSnapshot({ error });

return null;
}

function App() {
return (
<ErrorBoundary FallbackComponent={ErrorFallback}>
<Suspense fallback={<SuspenseFallback />}>
<ReadQueryHook />
</Suspense>
</ErrorBoundary>
);
}

renderWithClient(<App />, { client, wrapper: Profiler });

{
const { renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual([SuspenseFallback]);
}

{
const { snapshot, renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual([ReadQueryHook]);
expect(snapshot.result).toEqual({
data: undefined,
error: undefined,
networkStatus: NetworkStatus.ready,
});
expect(snapshot.error).toEqual(null);
}

dispose();
});

test("passes context to the link", async () => {
interface QueryData {
context: Record<string, any>;
Expand Down

0 comments on commit 64b23a0

Please sign in to comment.