Fakultas Ilmu Komputer UI

index.test.tsx 1.71 KB
Newer Older
Wulan Mantiri's avatar
Wulan Mantiri committed
1
2
3
4
5
6
7
8
9
10
11
import React from 'react';
import { fireEvent, render } from '@testing-library/react-native';

import StepByStepForm from '.';

const mockedNavigate = jest.fn();

jest.mock('@react-navigation/native', () => {
  return {
    useNavigation: () => ({
      navigate: mockedNavigate,
12
      reset: mockedNavigate,
Wulan Mantiri's avatar
Wulan Mantiri committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    }),
  };
});

describe('StepByStepForm component', () => {
  const pages = ['hai', 'hello', 'hei'];
  const props = {
    pages: pages.map((name) => ({
      name,
      route: name,
    })),
    currentPage: 0,
    finishRedirectRoute: 'finishroute',
  };

28
  it('only has "Isi" button when form is never filled', () => {
Wulan Mantiri's avatar
Wulan Mantiri committed
29
    const { getByText, queryByText } = render(<StepByStepForm {...props} />);
30
    const nextButton = getByText(/Isi/i);
Wulan Mantiri's avatar
Wulan Mantiri committed
31
32
33
34
35
36
    expect(nextButton).toBeTruthy();
    expect(queryByText(/Ubah/i)).toBeFalsy();
  });

  it('has "Ubah" button when form has been filled', () => {
    const { queryByText } = render(
37
      <StepByStepForm {...props} currentPage={2} />,
Wulan Mantiri's avatar
Wulan Mantiri committed
38
39
40
41
    );
    expect(queryByText(/Ubah/i)).toBeTruthy();
  });

42
  it('redirects to designated route when button "Ubah" or "Isi" is pressed', () => {
Wulan Mantiri's avatar
Wulan Mantiri committed
43
    const { getByText } = render(<StepByStepForm {...props} currentPage={1} />);
44
    const nextButton = getByText(/Isi/i);
Wulan Mantiri's avatar
Wulan Mantiri committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

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

  it('redirects to finish route when button "Selesai" is pressed', () => {
    const { getByText } = render(
      <StepByStepForm {...props} currentPage={pages.length} />,
    );
    const finishButton = getByText(/Selesai/i);
    expect(finishButton).toBeTruthy();
    fireEvent.press(finishButton);

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