|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import * as React from 'react';
|
3 | 3 | import * as TestRenderer from 'react-test-renderer';
|
4 |
| -import useForceUpdate from '../src/use-force-update'; |
| 4 | +import useForceUpdate from '../use-force-update'; |
| 5 | + |
| 6 | +type VoidFunction = () => void; |
5 | 7 |
|
6 | 8 | describe('useForceUpdate', () => {
|
7 |
| - let forceUpdate: () => void; |
| 9 | + const forceUpdates: VoidFunction[] = []; |
8 | 10 | let renders: number;
|
9 |
| - let TestComponent: React.FunctionComponent<{}>; |
| 11 | + let TestComponent: React.FunctionComponent<{}> = () => null; |
10 | 12 |
|
11 | 13 | beforeEach(() => {
|
12 | 14 |
|
| 15 | + // Reset array of forceUpdate functions. |
| 16 | + forceUpdates.splice(0, forceUpdates.length); |
| 17 | + |
13 | 18 | // Reset render count.
|
14 | 19 | renders = 0;
|
15 | 20 |
|
16 | 21 | // Create the component.
|
17 | 22 | // This creates a new forceUpdate hook for each test.
|
18 | 23 | TestComponent = () => {
|
19 |
| - forceUpdate = useForceUpdate(); |
| 24 | + forceUpdates.push(useForceUpdate()); |
20 | 25 | renders++;
|
21 | 26 | return null;
|
22 | 27 | };
|
23 | 28 |
|
24 | 29 | });
|
25 | 30 |
|
26 |
| - it('should update a component', () => { |
| 31 | + it('should accept no parameters', () => { |
| 32 | + TestRenderer.create(<TestComponent />); |
| 33 | + expect(forceUpdates[0].length).to.equal(0); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should maintain the same reference', () => { |
| 37 | + TestRenderer.create(<TestComponent />); |
| 38 | + forceUpdates[0](); |
| 39 | + expect(forceUpdates[0]).to.equal(forceUpdates[1]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return undefined', () => { |
| 43 | + TestRenderer.create(<TestComponent />); |
| 44 | + expect(forceUpdates[0]()).to.be.undefined; |
| 45 | + }); |
| 46 | + |
| 47 | + it('should update the component', () => { |
27 | 48 | expect(renders).to.equal(0);
|
28 | 49 | TestRenderer.create(<TestComponent />);
|
29 | 50 | expect(renders).to.equal(1);
|
30 |
| - forceUpdate(); |
| 51 | + forceUpdates[0](); |
31 | 52 | expect(renders).to.equal(2);
|
32 |
| - }); |
33 |
| - |
34 |
| - it('update function should return undefined', () => { |
35 |
| - expect(forceUpdate()).to.be.undefined; |
36 |
| - // @ts-ignore test with parameters :shrug: |
37 |
| - expect(forceUpdate('anything')).to.be.undefined; |
| 53 | + forceUpdates[1](); |
| 54 | + expect(renders).to.equal(3); |
38 | 55 | });
|
39 | 56 |
|
40 | 57 | });
|
0 commit comments