Fakultas Ilmu Komputer UI

index.test.tsx 1.74 KB
Newer Older
1
import React from 'react';
2
3
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import axios from 'axios';
4
5

import Questionnaire1 from '.';
Wulan Mantiri's avatar
Wulan Mantiri committed
6
import { mockDietQuestionnaire } from 'mocks/dietQuestionnaire';
7
import { textFields } from 'constants/questionnaire';
8
9
import { mockUserContext } from 'mocks/userContext';
import { UserContext } from 'provider';
10
11
12
13
14
15

const mockedNavigate = jest.fn();

jest.mock('@react-navigation/native', () => {
  return {
    useNavigation: () => ({
16
      reset: mockedNavigate,
17
18
19
20
    }),
  };
});

21
22
23
jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>;

24
describe('Questionnaire1', () => {
25
26
27
28
29
30
31
  const validFormValues: { [_: string]: string } = {
    city_and_area_of_residence: 'Jakarta',
    handphone_no: '08123567',
    whatsapp_no: '09',
    waist_size: '765',
  };

32
33
34
35
  const userProviderValues = {
    ...mockUserContext,
  };

36
37
38
39
40
41
42
43
44
  it('does not redirect to extended questionnaire if form values are invalid', async () => {
    const updateDietQuestionnaireApi = () =>
      Promise.resolve({
        status: 201,
        data: mockDietQuestionnaire,
      });
    mockAxios.request.mockImplementationOnce(updateDietQuestionnaireApi);

    const { getByPlaceholderText, getByText } = render(
45
46
47
      <UserContext.Provider value={userProviderValues}>
        <Questionnaire1 route={{ params: mockDietQuestionnaire }} />
      </UserContext.Provider>,
48
49
50
51
52
53
54
55
56
57
58
59
60
    );

    textFields.identity.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });

    const submitButton = getByText(/Simpan/i);
    await waitFor(() => fireEvent.press(submitButton));
  });

  afterAll(() => {
    jest.clearAllMocks();
61
62
  });
});