aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-boolean/use-boolean.test.ts
blob: 22d3cdc532ed2907b3a3344cac1c6641a03aa6a7 (plain)
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
import { describe, expect, it } from '@jest/globals';
import { act, renderHook } from '@testing-library/react';
import { useBoolean } from './use-boolean';

describe('use-boolean', () => {
  it('returns the initial state', () => {
    const initialState = true;
    const { result } = renderHook(() => useBoolean(initialState));

    expect(result.current.state).toBe(initialState);
  });

  it('can set the state to false', () => {
    const { result } = renderHook(() => useBoolean());

    act(() => {
      result.current.deactivate();
    });

    expect(result.current.state).toBe(false);
  });

  it('can set the state to true', () => {
    const { result } = renderHook(() => useBoolean());

    act(() => {
      result.current.activate();
    });

    expect(result.current.state).toBe(true);
  });

  it('can switch the state', () => {
    const initialState = true;
    const { result } = renderHook(() => useBoolean(initialState));

    expect(result.current.state).toBe(initialState);

    act(() => {
      result.current.toggle();
    });

    expect(result.current.state).toBe(!initialState);
  });
});