Fakultas Ilmu Komputer UI

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

import ChooseWeek from '.';
import { mockUserReportHistory } from 'mocks/userReport';

const mockedNavigate = jest.fn();

jest.mock('@react-navigation/native', () => {
  return {
    useNavigation: () => ({
      navigate: mockedNavigate,
    }),
  };
});

describe('ChooseWeek component', () => {
  it('has "Isi" button when form is never filled and accessed by client', () => {
    const { getByText } = render(<ChooseWeek data={mockUserReportHistory} />);
    const fillButton = getByText(/Isi/i);
    expect(fillButton).toBeTruthy();
    fireEvent.press(fillButton);

    expect(mockedNavigate).toHaveBeenCalled();
  });

  it('does not have "Isi" button when form is never filled but accessed by nutritionist', () => {
    const { queryByText } = render(
      <ChooseWeek data={mockUserReportHistory} isNutritionist />,
    );
    expect(queryByText(/Isi/i)).toBeFalsy();
  });

  it('does not have "Isi" button when form is already filled', () => {
    const { queryByText } = render(
      <ChooseWeek
        data={{ ...mockUserReportHistory, has_not_filled_form: false }}
      />,
    );
    expect(queryByText(/Isi/i)).toBeFalsy();
  });

  it('redirects to designated route when button "Lihat" is pressed', () => {
    const { getAllByText } = render(
      <ChooseWeek data={mockUserReportHistory} />,
    );
    const viewButton = getAllByText(/Lihat/i)[0];
    expect(viewButton).toBeTruthy();
    fireEvent.press(viewButton);

    expect(mockedNavigate).toHaveBeenCalled();
  });
});