Fakultas Ilmu Komputer UI

index.test.tsx 5.13 KB
Newer Older
1
import React from 'react';
2
3
import { render, fireEvent, waitFor } from 'utils/testing';
import * as ROUTES from 'constants/routes';
4
import axios from 'axios';
5
6

import AllAccessQuestionnaire from '.';
7
import { allAccessQuestions, textFields } from './schema';
8

9
10
11
12
jest.mock('react-native-toast-message');
jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>;

13
14
15
16
17
18
19
describe('AllAccessQuestionnaire', () => {
  const validFormValues: { [_: string]: any } = {
    name: 'Dietela',
    email: 'dietela@gmail.com',
    age: '29',
    weight: '82',
    height: '178',
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    gender: 1,
    special_condition: 1,
    body_activity: 1,
    vegetables_in_one_day: 1,
    fruits_in_one_day: 1,
    fried_food_in_one_day: 1,
    sweet_snacks_in_one_day: 1,
    sweet_drinks_in_one_day: 1,
    packaged_food_in_one_day: 1,
    large_meal_in_one_day: 1,
    snacks_in_one_day: 1,
    breakfast_type: 1,
    current_condition: 1,
    problem_to_solve: 1,
    health_problem: [1],
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  };

  it('shows biodata form and all required errors after submit with empty form values', async () => {
    const { getByText, queryByText } = render(
      <AllAccessQuestionnaire />,
      ROUTES.allAccessQuestionnaire,
    );

    allAccessQuestions.forEach(() => {
      const nextButton = getByText(/Lanjut/i);
      expect(nextButton).toBeTruthy();
      fireEvent.press(nextButton);
    });

    const submitButton = getByText('Selesai');
    expect(submitButton).toBeTruthy();
    await waitFor(() => fireEvent.press(submitButton));

53
54
    const preQuizHeader = getByText(/Data Diri/i);
    expect(preQuizHeader).toBeTruthy();
55
56

    textFields.forEach(({ label }) => {
57
      const errorMessage = getByText(`${label} harus diisi`);
58
59
60
      expect(errorMessage).toBeTruthy();
    });

61
    const genderErrorMessage = getByText('Pilihan harus diisi');
62
63
64
65
66
67
68
    expect(genderErrorMessage).toBeTruthy();

    const sampleNextPage = queryByText(/Pertanyaan/i);
    expect(sampleNextPage).toBeFalsy();
  });

  it('shows the foremost page with error after submit', async () => {
69
    const { getByText, getByPlaceholderText } = render(
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
      <AllAccessQuestionnaire />,
      ROUTES.allAccessQuestionnaire,
    );

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

    const maleChoice = getByText(/Pria/i);
    fireEvent.press(maleChoice);

    allAccessQuestions.forEach(() => {
      const nextButton = getByText(/Lanjut/i);
      fireEvent.press(nextButton);
    });

    const submitButton = getByText('Selesai');
    await waitFor(() => fireEvent.press(submitButton));

90
91
92
93
    const nextPage = getByText(/Pertanyaan/i);
    expect(nextPage).toBeTruthy();
  });

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  // it('redirects to quiz result page if all form values are valid and submit success', async () => {
  //   const createDietProfileApi = () =>
  //     Promise.resolve({
  //       status: 201,
  //       data: {
  //         id: 1,
  //         ...validFormValues,
  //       },
  //     });
  //   mockAxios.request.mockImplementationOnce(createDietProfileApi);

  //   const { getByText, getByPlaceholderText } = render(
  //     <AllAccessQuestionnaire />,
  //     ROUTES.allAccessQuestionnaire,
  //   );

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

  //   const maleChoice = getByText(/Pria/i);
  //   fireEvent.press(maleChoice);

  //   allAccessQuestions.forEach(({ choiceList }) => {
  //     const nextButton = getByText(/Lanjut/i);
  //     fireEvent.press(nextButton);

  //     const firstChoice = getByText(choiceList[0]);
  //     fireEvent.press(firstChoice);
  //   });

  //   const submitButton = getByText('Selesai');
  //   await waitFor(() => fireEvent.press(submitButton));

  //   const quizResultPage = getByText(/Quiz Result/i);
  //   expect(quizResultPage).toBeTruthy();
  // });
132

133
  it('does not redirect to quiz result page if all form values are valid but submit fails', async () => {
134
135
136
137
138
139
140
    const createDietProfileApi = () =>
      Promise.reject({
        status: 400,
        data: 'error',
      });
    mockAxios.request.mockImplementationOnce(createDietProfileApi);

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    const { getByText, getByPlaceholderText, queryByText } = render(
      <AllAccessQuestionnaire />,
      ROUTES.allAccessQuestionnaire,
    );

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

    const maleChoice = getByText(/Pria/i);
    fireEvent.press(maleChoice);

    allAccessQuestions.forEach(({ choiceList }) => {
      const nextButton = getByText(/Lanjut/i);
      fireEvent.press(nextButton);

      const firstChoice = getByText(choiceList[0]);
      fireEvent.press(firstChoice);
    });

    const submitButton = getByText('Selesai');
    await waitFor(() => fireEvent.press(submitButton));

165
166
167
168
169
170
    const quizResultPage = queryByText(/Quiz Result/i);
    expect(quizResultPage).toBeFalsy();
  });

  afterAll(() => {
    jest.clearAllMocks();
171
172
  });
});