|
2 | 2 | import { useEffect } from 'react';
|
3 | 3 | import * as axios from 'axios';
|
4 | 4 |
|
| 5 | +import { keyStore } from 'utils'; |
5 | 6 | import { MockUseState } from 'testUtils';
|
6 | 7 | import * as hooks from './textHooks';
|
7 | 8 |
|
8 | 9 | jest.mock('axios', () => ({
|
9 | 10 | get: jest.fn(),
|
10 | 11 | }));
|
11 | 12 |
|
| 13 | +const hookKeys = keyStore(hooks); |
12 | 14 | const state = new MockUseState(hooks);
|
| 15 | + |
13 | 16 | let hook;
|
14 | 17 |
|
15 | 18 | const testValue = 'test-value';
|
@@ -46,30 +49,45 @@ describe('Text file preview hooks', () => {
|
46 | 49 | hook = hooks.rendererHooks(props);
|
47 | 50 | [[cb, prereqs]] = useEffect.mock.calls;
|
48 | 51 | };
|
49 |
| - test('useEffect, predicated on url changes', () => { |
| 52 | + it('calls fetchFile method, predicated on setContent, url, and callbacks', () => { |
| 53 | + jest.spyOn(hooks, hookKeys.fetchFile).mockImplementationOnce(() => {}); |
50 | 54 | loadHook();
|
51 | 55 | expect(useEffect).toHaveBeenCalled();
|
52 |
| - expect(prereqs[3]).toEqual(props.url); |
53 |
| - }); |
54 |
| - describe('onSuccess', () => { |
55 |
| - it('calls get', async () => { |
56 |
| - const testData = 'test-data'; |
57 |
| - axios.get.mockReturnValueOnce(Promise.resolve({ data: testData })); |
58 |
| - loadHook(); |
59 |
| - await cb(testValue); |
60 |
| - expect(props.onSuccess).toHaveBeenCalled(); |
61 |
| - expect(state.setState[state.keys.content]).toHaveBeenCalledWith(testData); |
| 56 | + expect(prereqs).toEqual([ |
| 57 | + props.onError, |
| 58 | + props.onSuccess, |
| 59 | + state.setState.content, |
| 60 | + props.url, |
| 61 | + ]); |
| 62 | + expect(hooks.fetchFile).not.toHaveBeenCalled(); |
| 63 | + cb(); |
| 64 | + expect(hooks.fetchFile).toHaveBeenCalledWith({ |
| 65 | + onError: props.onError, |
| 66 | + onSuccess: props.onSuccess, |
| 67 | + setContent: state.setState.content, |
| 68 | + url: props.url, |
62 | 69 | });
|
63 | 70 | });
|
64 |
| - describe('onError', () => { |
65 |
| - it('calls get on the passed url when it changes', async () => { |
66 |
| - axios.get.mockReturnValueOnce(Promise.reject( |
67 |
| - { response: { status: testValue } }, |
68 |
| - )); |
69 |
| - loadHook(); |
70 |
| - await cb(testValue); |
71 |
| - expect(props.onError).toHaveBeenCalledWith(testValue); |
72 |
| - }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + describe('fetchFile', () => { |
| 74 | + describe('onSuccess', () => { |
| 75 | + it('calls get', async () => { |
| 76 | + const testData = 'test-data'; |
| 77 | + axios.get.mockReturnValueOnce(Promise.resolve({ data: testData })); |
| 78 | + await hooks.fetchFile({ ...props, setContent: state.setState.content }); |
| 79 | + expect(props.onSuccess).toHaveBeenCalled(); |
| 80 | + expect(state.setState[state.keys.content]).toHaveBeenCalledWith(testData); |
| 81 | + }); |
| 82 | + }); |
| 83 | + describe('onError', () => { |
| 84 | + it('calls get on the passed url when it changes', async (done) => { |
| 85 | + axios.get.mockReturnValueOnce(Promise.reject( |
| 86 | + { response: { status: testValue } }, |
| 87 | + )); |
| 88 | + await hooks.fetchFile({ ...props, setContent: state.setState.content }); |
| 89 | + expect(props.onError).toHaveBeenCalledWith(testValue); |
| 90 | + done(); |
73 | 91 | });
|
74 | 92 | });
|
75 | 93 | });
|
|
0 commit comments