Fakultas Ilmu Komputer UI

Commit 72fbcf73 authored by Kefas Satrio Bangkit Solidedantyo's avatar Kefas Satrio Bangkit Solidedantyo
Browse files

improve test for DietReportForNutritionist

parent 094388fd
import React from 'react';
import { render, waitFor } from 'utils/testing';
import { render, waitFor, fireEvent } from 'utils/testing';
import DietReportForNutritionist from '.';
import { mockUserReportResponse } from '__mocks__/userReport';
import axios from 'axios';
import * as ROUTES from 'constants/routes';
jest.mock('react-native-toast-message');
jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>;
describe('DietReportForNutritionist', () => {
const userReports = [mockUserReportResponse];
it('renders correctly', async () => {
it('renders and submits correctly when given valid comments', async () => {
mockAxios.request.mockImplementationOnce(() =>
Promise.resolve({
status: 200,
data: userReports,
}),
);
render(<DietReportForNutritionist />, ROUTES.clientDietReportNutritionist, {
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)');
});
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 },
},
);
await waitFor(() => expect(mockAxios.request).toBeCalled());
expect(
queryByText(/Klien belum mengisi laporan diet mingguan/i),
).toBeTruthy();
});
afterAll(() => {
......
......@@ -37,8 +37,8 @@ const DietReportForNutritionist: FC = () => {
getTextInputProps,
handleSubmit,
isSubmitting,
isFormUntouched,
isFieldError,
values: formValues,
} = useForm({
initialValues: dietReportCommentInitialValues,
validationSchema: generateValidationSchema(fieldValidations),
......@@ -48,7 +48,7 @@ const DietReportForNutritionist: FC = () => {
...values,
};
const response = await createNutritionistCommentApi(payload);
if (response.success && response.data) {
if (response.success) {
Toast.show({
type: 'success',
text1: 'Sukses membuat komen',
......@@ -76,19 +76,23 @@ const DietReportForNutritionist: FC = () => {
const userReport = userReports[userReports.length - 1];
const isCurrentPageError = (): boolean => {
if (activeSlide === 1) {
return [
formValues.height,
formValues.weight,
formValues.waist_size,
].reduce((acc: boolean, item) => acc || item === '', false);
}
if (activeSlide === 3) {
return isFieldError('average_consumption');
}
const fields = [
...(dietReportTextFields[`dietReportPage${activeSlide}`] || []),
...(dietReportSelectFields[`dietReportPage${activeSlide}`] || []),
...dietReportTextFields[`dietReportPage${activeSlide}`],
...dietReportSelectFields[`dietReportPage${activeSlide}`],
];
return (
isFormUntouched() ||
fields.reduce(
return fields.reduce(
(acc: boolean, item) => acc || isFieldError(item.name),
false,
)
);
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment