File size: 4,470 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
import { renderHook } from '@testing-library/react-hooks';
import { useCallback } from 'react';
import useAsync from '../src/useAsync';
describe('useAsync', () => {
it('should be defined', () => {
expect(useAsync).toBeDefined();
});
describe('a success', () => {
let hook;
let callCount = 0;
const resolver = async () => {
return new Promise((resolve) => {
callCount++;
const wait = setTimeout(() => {
clearTimeout(wait);
resolve('yay');
}, 0);
});
};
beforeEach(() => {
callCount = 0;
hook = renderHook(({ fn }) => useAsync(fn, [fn]), {
initialProps: {
fn: resolver,
},
});
});
it('initially starts loading', async () => {
expect(hook.result.current.loading).toEqual(true);
await hook.waitForNextUpdate();
});
it('resolves', async () => {
expect.assertions(4);
hook.rerender({ fn: resolver });
await hook.waitForNextUpdate();
expect(callCount).toEqual(1);
expect(hook.result.current.loading).toBeFalsy();
expect(hook.result.current.value).toEqual('yay');
expect(hook.result.current.error).toEqual(undefined);
});
});
describe('an error', () => {
let hook;
let callCount = 0;
const rejection = async () => {
return new Promise((_, reject) => {
callCount++;
const wait = setTimeout(() => {
clearTimeout(wait);
reject('yay');
}, 0);
});
};
beforeEach(() => {
callCount = 0;
hook = renderHook(({ fn }) => useAsync(fn, [fn]), {
initialProps: {
fn: rejection,
},
});
});
it('initially starts loading', async () => {
expect(hook.result.current.loading).toBeTruthy();
await hook.waitForNextUpdate();
});
it('resolves', async () => {
expect.assertions(4);
hook.rerender({ fn: rejection });
await hook.waitForNextUpdate();
expect(callCount).toEqual(1);
expect(hook.result.current.loading).toBeFalsy();
expect(hook.result.current.error).toEqual('yay');
expect(hook.result.current.value).toEqual(undefined);
});
});
describe('re-evaluates when dependencies change', () => {
describe('the fn is a dependency', () => {
let hook;
let callCount = 0;
const initialFn = async () => {
callCount++;
return 'value';
};
const differentFn = async () => {
callCount++;
return 'new value';
};
beforeEach((done) => {
callCount = 0;
hook = renderHook(({ fn }) => useAsync(fn, [fn]), {
initialProps: { fn: initialFn },
});
hook.waitForNextUpdate().then(done);
});
it('renders the first value', () => {
expect(hook.result.current.value).toEqual('value');
});
it('renders a different value when deps change', async () => {
expect.assertions(3);
expect(callCount).toEqual(1);
hook.rerender({ fn: differentFn }); // change the fn to initiate new request
await hook.waitForNextUpdate();
expect(callCount).toEqual(2);
expect(hook.result.current.value).toEqual('new value');
});
});
describe('the additional dependencies list changes', () => {
let callCount = 0;
let hook;
const staticFunction = async (counter) => {
callCount++;
return `counter is ${counter} and callCount is ${callCount}`;
};
beforeEach((done) => {
callCount = 0;
hook = renderHook(
({ fn, counter }) => {
const callback = useCallback(() => fn(counter), [counter]);
return useAsync<any>(callback, [callback]);
},
{
initialProps: {
counter: 0,
fn: staticFunction,
},
}
);
hook.waitForNextUpdate().then(done);
});
it('initial renders the first passed pargs', () => {
expect(hook.result.current.value).toEqual('counter is 0 and callCount is 1');
});
it('renders a different value when deps change', async () => {
expect.assertions(1);
hook.rerender({ fn: staticFunction, counter: 1 });
await hook.waitForNextUpdate();
expect(hook.result.current.value).toEqual('counter is 1 and callCount is 2');
});
});
});
});
|