Fakultas Ilmu Komputer UI

index.test.tsx 4.26 KB
Newer Older
1
import React from 'react';
2
import { render, waitFor, fireEvent } from 'utils/testing';
3
4

import DietReportForNutritionist from '.';
5
import { mockUserReportResponse } from 'mocks/userReport';
6
7
import axios from 'axios';
import * as ROUTES from 'constants/routes';
8

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

describe('DietReportForNutritionist', () => {
14
15
  const userReports = [mockUserReportResponse];

16
  it('renders and submits correctly when given valid comments', async () => {
17
18
19
20
21
22
    mockAxios.request.mockImplementationOnce(() =>
      Promise.resolve({
        status: 200,
        data: userReports,
      }),
    );
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
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
    const { queryByText, getByText, getAllByPlaceholderText } = render(
      <DietReportForNutritionist />,
      ROUTES.clientDietReportNutritionist,
      {
        routeParams: { id: 1 },
      },
    );
    await waitFor(() => expect(mockAxios.request).toBeCalled());

    const textFieldsPage1 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage1.forEach((field) => {
      fireEvent.changeText(field, 'comment');
    });

    fireEvent.press(getByText(/Lanjut/i));

    const textFieldsPage2 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage2.forEach((field) => {
      fireEvent.changeText(field, 'comment (2)');
    });

    fireEvent.press(getByText(/Lanjut/i));

    const textFieldsPage3 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage3.forEach((field) => {
      fireEvent.changeText(field, 'comment (3)');
    });

    fireEvent.press(getByText(/Lanjut/i));

    const textFieldsPage4 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage4.forEach((field) => {
      fireEvent.changeText(field, 'comment (4)');
    });

    mockAxios.request.mockImplementationOnce(() =>
      Promise.resolve({
        status: 201,
        data: mockUserReportResponse,
      }),
    );

    const submitButton = getByText('Selesai');
    await waitFor(() => fireEvent.press(submitButton));
    expect(queryByText(/Daftar Klien/i)).toBeTruthy();
  });

  it('renders and does not redirect when api fails', async () => {
    mockAxios.request.mockImplementationOnce(() =>
      Promise.resolve({
        status: 200,
        data: userReports,
      }),
    );
    const { queryByText, getByText, getAllByPlaceholderText } = render(
      <DietReportForNutritionist />,
      ROUTES.clientDietReportNutritionist,
      {
        routeParams: { id: 1 },
      },
    );
    await waitFor(() => expect(mockAxios.request).toBeCalled());

    const textFieldsPage1 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage1.forEach((field) => {
      fireEvent.changeText(field, 'comment');
    });

    fireEvent.press(getByText(/Lanjut/i));

    const textFieldsPage2 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage2.forEach((field) => {
      fireEvent.changeText(field, 'comment (2)');
    });

    fireEvent.press(getByText(/Lanjut/i));

    const textFieldsPage3 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage3.forEach((field) => {
      fireEvent.changeText(field, 'comment (3)');
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

    fireEvent.press(getByText(/Lanjut/i));

    const textFieldsPage4 = getAllByPlaceholderText(/Tuliskan komentar.../i);
    textFieldsPage4.forEach((field) => {
      fireEvent.changeText(field, 'comment (4)');
    });

    mockAxios.request.mockImplementationOnce(() =>
      Promise.reject({
        status: 400,
        response: {
          data: 'error',
        },
      }),
    );

    const submitButton = getByText('Selesai');
    await waitFor(() => fireEvent.press(submitButton));
    expect(queryByText(/Daftar Klien/i)).toBeFalsy();
  });

  it('shows empty data page when no data', async () => {
    mockAxios.request.mockImplementationOnce(() =>
      Promise.resolve({
        status: 200,
        data: [],
      }),
    );

    const { queryByText } = render(
      <DietReportForNutritionist />,
      ROUTES.clientDietReportNutritionist,
      {
        routeParams: { id: 0 },
      },
    );
141
    await waitFor(() => expect(mockAxios.request).toBeCalled());
142
143
144
145

    expect(
      queryByText(/Klien belum mengisi laporan diet mingguan/i),
    ).toBeTruthy();
146
  });
147
148
149
150

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