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
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
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,
    }),
  };
});

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

  it('only has "Lanjut" button when form is never filled', () => {
    const { getByText, queryByText } = render(<StepByStepForm {...props} />);
    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    expect(queryByText(/Ubah/i)).toBeFalsy();
  });

  it('has "Ubah" button when form has been filled', () => {
    const { queryByText } = render(
      <StepByStepForm {...props} currentPage={1} />,
    );
    expect(queryByText(/Ubah/i)).toBeTruthy();
  });

  it('redirects to designated route when button "Ubah" or "Lanjut" is pressed', () => {
    const { getByText } = render(<StepByStepForm {...props} currentPage={1} />);
    const nextButton = getByText(/Lanjut/i);
    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();
  });
});