File size: 1,463 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
import { act, renderHook } from '@testing-library/react-hooks';
import useGetSet from '../src/useGetSet';

const setUp = (initialValue: any) => renderHook(() => useGetSet(initialValue));

beforeEach(() => {
  jest.useFakeTimers();
});

it('should init getter and setter', () => {
  const { result } = setUp('foo');
  const [get, set] = result.current;

  expect(get).toBeInstanceOf(Function);
  expect(set).toBeInstanceOf(Function);
});

it('should get current value', () => {
  const { result } = setUp('foo');
  const [get] = result.current;

  const currentValue = get();

  expect(currentValue).toBe('foo');
});

it('should set new value', () => {
  const { result } = setUp('foo');
  const [get, set] = result.current;

  act(() => set('bar'));

  const currentValue = get();
  expect(currentValue).toBe('bar');
});

/**
 * This test implements the special demo in storybook that increments a number
 * after 1 second on each click.
 */
it('should get and set expected values when used in nested functions', () => {
  const onClick = jest.fn(() => {
    setTimeout(() => {
      set(get() + 1);
    }, 1000);
  });

  const { result } = setUp(0);
  const [get, set] = result.current;

  // simulate 3 clicks
  onClick();
  onClick();
  onClick();

  // fast-forward until all timers have been executed
  act(() => {
    jest.runAllTimers();
  });

  const currentValue = get();
  expect(currentValue).toBe(3);
  expect(onClick).toHaveBeenCalledTimes(3);
});