Fakultas Ilmu Komputer UI

index.test.tsx 1.66 KB
Newer Older
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
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';

import CheckboxGroup from '.';
import { checkbox } from 'constants/options';

describe('CheckboxGroup component', () => {
  const props = {
    choices: [
      {
        label: 'Choice 1',
        value: 1,
      },
      {
        label: 'Choice 2',
        value: 2,
      },
    ],
  };

  it('is able to select multiple', () => {
    let value: Array<string | number> = [];

    const { getByText } = render(
      <CheckboxGroup
        {...props}
        value={value}
        onChange={(v: typeof value) => (value = v)}
      />,
    );

    expect(value).toStrictEqual([]);

    fireEvent.press(getByText(/Choice 1/i));
    expect(value).toContain(1);

    fireEvent.press(getByText(/Choice 2/i));
    expect(value).toContain(2);
  });

  it('is able to unselect checkbox', () => {
    let value: Array<string | number> = [2];

    const { getByText } = render(
      <CheckboxGroup
        {...props}
        value={value}
        onChange={(v: typeof value) => (value = v)}
      />,
    );

    expect(value).toContain(2);

    fireEvent.press(getByText(/Choice 2/i));
    expect(value).toStrictEqual([]);
  });

  it('shows other options when hasOtherChoice prop is true', () => {
    let value: Array<string | number> = [];

    const { getByText } = render(
      <CheckboxGroup
        {...props}
        value={value}
        onChange={(v: typeof value) => (value = v)}
        hasOtherChoice
      />,
    );

    const otherChoice = getByText(/Lainnya/i);
    expect(otherChoice).toBeTruthy();

    fireEvent.press(otherChoice);
    expect(value).toContain(checkbox.OTHER);
  });
});