Skip to content

Commit

Permalink
adding test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
atarek12 committed Apr 22, 2022
1 parent 84982ef commit 4bd8d76
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/react/ssr/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,37 @@ describe('useLazyQuery Hook SSR', () => {
]
};

it('should not execute the query and properly render the component', () => {
const link = mockSingleLink({
request: { query: CAR_QUERY },
result: { data: CAR_RESULT_DATA }
});

const client = new ApolloClient({
cache: new InMemoryCache(),
link,
ssrMode: true
});

let isRendered = false;
const Component = () => {
useLazyQuery(CAR_QUERY);
isRendered = true;
return null;
};

const app = (
<ApolloProvider client={client}>
<Component />
</ApolloProvider>
);

return renderToStringWithData(app).then(markup => {
expect(isRendered).toBeTruthy();
expect(markup).toEqual('');
});
});

it('should run query only after calling the lazy mode execute function', () => {
const link = mockSingleLink({
request: { query: CAR_QUERY },
Expand Down
35 changes: 35 additions & 0 deletions src/react/ssr/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ describe('useQuery Hook SSR', () => {
});
});

it('should skip SSR tree rendering if `fetchPolicy` option is `standby`', () => {
const link = mockSingleLink({
request: { query: CAR_QUERY },
result: { data: CAR_RESULT_DATA }
});

const client = new ApolloClient({
cache: new InMemoryCache(),
link,
ssrMode: true
});

let renderCount = 0;
const Component = () => {
const { data, loading } = useQuery(CAR_QUERY, { fetchPolicy:'standby' });
renderCount += 1;
if (!loading && data) {
const { make } = data.cars[0];
return <div>{make}</div>;
}
return null;
};

const app = (
<ApolloProvider client={client}>
<Component />
</ApolloProvider>
);

return renderToStringWithData(app).then(result => {
expect(renderCount).toBe(1);
expect(result).toEqual('');
});
});

it('should skip both SSR tree rendering and SSR component rendering if `ssr` option is `false` and `ssrMode` is `true`',
async () => {
const link = mockSingleLink({
Expand Down

0 comments on commit 4bd8d76

Please sign in to comment.