Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
mock useClientRequest & test calls
  • Loading branch information
jackdclark committed Feb 19, 2019
commit aacbea65569f2765a73af8598b1e2e08b4c158cf
27 changes: 23 additions & 4 deletions test/unit/useQuery.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { testHook } from 'react-testing-library';
import { ClientContext, useQuery } from '../../src';
import { ClientContext, useQuery, useClientRequest } from '../../src';

let mockQueryReq, mockState;
jest.mock('../../src/useClientRequest', () => () => [mockQueryReq, mockState]);
jest.mock('../../src/useClientRequest');

let mockQueryReq, mockState, mockClient;

let mockClient;
const Wrapper = props => (
<ClientContext.Provider value={mockClient}>
{props.children}
Expand All @@ -22,6 +22,8 @@ describe('useQuery', () => {
beforeEach(() => {
mockQueryReq = jest.fn();
mockState = { loading: true, cacheHit: false };
useClientRequest.mockReturnValue([mockQueryReq, mockState]);

mockClient = {
ssrMode: false,
ssrPromises: []
Expand All @@ -32,6 +34,23 @@ describe('useQuery', () => {
jest.unmock('../../src/useClientRequest');
});

it('calls useClientRequest with query', () => {
testHook(() => useQuery(TEST_QUERY), { wrapper: Wrapper });
expect(useClientRequest).toHaveBeenCalledWith(TEST_QUERY, {
useCache: true
});
});

it('calls useClientRequest with options', () => {
testHook(() => useQuery(TEST_QUERY, { useCache: false, extra: 'option' }), {
wrapper: Wrapper
});
expect(useClientRequest).toHaveBeenCalledWith(TEST_QUERY, {
useCache: false,
extra: 'option'
});
});

it('returns initial state from useClientRequest & refetch', () => {
let state;
testHook(() => (state = useQuery(TEST_QUERY)), { wrapper: Wrapper });
Expand Down