Fakultas Ilmu Komputer UI

index.test.tsx 5.47 KB
Newer Older
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
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import axios from 'axios';

import WeeklyReport from '.';
import {
  dietReportTextFields,
  dietReportSelectFields,
} from 'constants/weeklyReport';
import { RadioButton } from 'components/form';

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

const mockedNavigate = jest.fn();

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

describe('WeeklyReport', () => {
  const validFormValues: { [_: string]: any } = {
    weight: '52',
    height: '90',
30
    waist_size: '100',
31
32
33
34
35
36
37
38
39
40
41
42
    changes_felt: 1,
    hunger_level: 1,
    fullness_level: 1,
    heavy_meal: 1,
    snacks: 1,
    sweet_beverages: 1,
    sugary_ingredients: 1,
    fried_snacks: 1,
    umami_snacks: 1,
    sweet_snacks: 1,
    fruits_portion: 1,
    vegetables_portion: 1,
43
    water_consumption: '8',
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
    physical_activity: [1],
    physical_activity_other: '',
    time_for_activity: 1,
    feeling_rating: 1,
    lesson_learned: 'hai',
    problem_faced_and_feedbacks: 'feedback',
  };

  it('initially has disabled next button', () => {
    const { getByText, queryByText } = render(<WeeklyReport />);

    const weightTextField = queryByText(/Berat Badan/i);
    expect(weightTextField).toBeTruthy();

    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

    expect(queryByText(/Berat Badan/i)).toBeTruthy();
  });

  it('redirects to choose week for report if all form values are valid and submit success', async () => {
    const createUserReportApi = () =>
      Promise.resolve({
        status: 201,
        data: validFormValues,
      });
    mockAxios.request.mockImplementationOnce(createUserReportApi);

    const { getByText, getByPlaceholderText, getByTestId } = render(
      <WeeklyReport />,
    );

    dietReportTextFields.dietReportPage1.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });
    fireEvent.press(getByText(/Lanjut/i));

    dietReportTextFields.dietReportPage2.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });
    dietReportSelectFields.dietReportPage2.forEach(({ name }) => {
      const formField = getByTestId(name);
      const firstChoice = formField.findAllByType(RadioButton)[0];
      fireEvent.press(firstChoice);
    });
    fireEvent.press(getByText(/Lanjut/i));

    dietReportSelectFields.dietReportPage3.forEach(({ name }) => {
      const formField = getByTestId(name);
      const firstChoice = formField.findAllByType(RadioButton)[0];
      fireEvent.press(firstChoice);
    });
    fireEvent.press(getByText(/Lanjut/i));

    dietReportSelectFields.dietReportPage4.forEach(({ choiceList }) => {
      const firstChoice = getByText(choiceList[0]);
      fireEvent.press(firstChoice);
    });
    dietReportTextFields.dietReportPage4.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });

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

    expect(mockedNavigate).toHaveBeenCalledTimes(1);
  });

  it('does not redirect to choose week for report if all form values are valid but submit fails', async () => {
    const createUserReportApi = () =>
      Promise.reject({
        status: 400,
        response: {
          data: 'error',
        },
      });
    mockAxios.request.mockImplementationOnce(createUserReportApi);

    const { getByText, getByPlaceholderText, getByTestId } = render(
      <WeeklyReport />,
    );

    dietReportTextFields.dietReportPage1.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });
    fireEvent.press(getByText(/Lanjut/i));

    dietReportTextFields.dietReportPage2.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });
    dietReportSelectFields.dietReportPage2.forEach(({ name }) => {
      const formField = getByTestId(name);
      const firstChoice = formField.findAllByType(RadioButton)[0];
      fireEvent.press(firstChoice);
    });
    fireEvent.press(getByText(/Lanjut/i));

    dietReportSelectFields.dietReportPage3.forEach(({ name }) => {
      const formField = getByTestId(name);
      const firstChoice = formField.findAllByType(RadioButton)[0];
      fireEvent.press(firstChoice);
    });
    fireEvent.press(getByText(/Lanjut/i));

    dietReportSelectFields.dietReportPage4.forEach(({ choiceList }) => {
      const firstChoice = getByText(choiceList[0]);
      fireEvent.press(firstChoice);
    });
    dietReportTextFields.dietReportPage4.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });

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

    expect(mockedNavigate).toHaveBeenCalledTimes(1);
  });

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