Fakultas Ilmu Komputer UI

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

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

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

describe('DietReportForNutritionist', () => {
17
  const data = mockUserReportResponse;
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  it('renders correctly when fetching previous comments', async () => {
    mockAxios.request.mockImplementationOnce(() =>
      Promise.resolve({
        status: 200,
        data: [mockUserReportComment],
      }),
    );
    render(<DietReportForNutritionist />, ROUTES.clientDietReportNutritionist, {
      routeParams: data,
    });
    await waitFor(() => expect(mockAxios.request).toBeCalled());
  });

  it('renders correctly when first creating valid comments', async () => {
    mockAxios.request.mockImplementationOnce(() =>
      Promise.resolve({
        status: 200,
        data: [],
      }),
    );
39
40
41
42
    const { queryByText, getByText, getAllByPlaceholderText } = render(
      <DietReportForNutritionist />,
      ROUTES.clientDietReportNutritionist,
      {
43
        routeParams: data,
44
45
      },
    );
46
    await waitFor(() => expect(mockAxios.request).toBeCalled());
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

    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));
83
    expect(queryByText(/Riwayat Laporan Diet Klien/i)).toBeTruthy();
84
85
  });

86
87
88
89
90
91
92
  it('renders correctly but does not redirect when api fails', async () => {
    mockAxios.request.mockImplementationOnce(() =>
      Promise.resolve({
        status: 200,
        data: [],
      }),
    );
93
94
95
96
    const { queryByText, getByText, getAllByPlaceholderText } = render(
      <DietReportForNutritionist />,
      ROUTES.clientDietReportNutritionist,
      {
97
        routeParams: data,
98
99
      },
    );
100
    await waitFor(() => expect(mockAxios.request).toBeCalled());
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

    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)');
119
    });
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

    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));
139
    expect(queryByText(/Riwayat Laporan Diet Klien/i)).toBeFalsy();
140
141
  });

142
143
144
  afterAll(() => {
    jest.clearAllMocks();
  });
145
});