Fakultas Ilmu Komputer UI

index.test.tsx 1.73 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
    }),
  };
});

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

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

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

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

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

  it('redirects to finish route when button "Selesai" is pressed', () => {
    const { getByText } = render(
52
53
54
55
56
      <StepByStepForm
        {...props}
        currentPage={pages.length}
        finishRedirectRoute="finish"
      />,
Wulan Mantiri's avatar
Wulan Mantiri committed
57
58
59
60
61
62
63
64
    );
    const finishButton = getByText(/Selesai/i);
    expect(finishButton).toBeTruthy();
    fireEvent.press(finishButton);

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