Fakultas Ilmu Komputer UI

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

Merge branch 'pbi-14-progress-diet-nutritionist' into 'staging'

improve test for DietReportForNutritionist

See merge request !74
parents 094388fd 72fbcf73
Pipeline #82166 failed with stages
in 7 minutes and 49 seconds
import React from 'react'; import React from 'react';
import { render, waitFor } from 'utils/testing'; import { render, waitFor, fireEvent } from 'utils/testing';
import DietReportForNutritionist from '.'; import DietReportForNutritionist from '.';
import { mockUserReportResponse } from '__mocks__/userReport'; import { mockUserReportResponse } from '__mocks__/userReport';
import axios from 'axios'; import axios from 'axios';
import * as ROUTES from 'constants/routes'; import * as ROUTES from 'constants/routes';
jest.mock('react-native-toast-message');
jest.mock('axios'); jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>; const mockAxios = axios as jest.Mocked<typeof axios>;
describe('DietReportForNutritionist', () => { describe('DietReportForNutritionist', () => {
const userReports = [mockUserReportResponse]; const userReports = [mockUserReportResponse];
it('renders correctly', async () => { it('renders and submits correctly when given valid comments', async () => {
mockAxios.request.mockImplementationOnce(() => mockAxios.request.mockImplementationOnce(() =>
Promise.resolve({ Promise.resolve({
status: 200, status: 200,
data: userReports, data: userReports,
}), }),
); );
render(<DietReportForNutritionist />, ROUTES.clientDietReportNutritionist, { const { queryByText, getByText, getAllByPlaceholderText } = render(
routeParams: { id: 1 }, <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()); await waitFor(() => expect(mockAxios.request).toBeCalled());
expect(
queryByText(/Klien belum mengisi laporan diet mingguan/i),
).toBeTruthy();
}); });
afterAll(() => { afterAll(() => {
......
...@@ -37,8 +37,8 @@ const DietReportForNutritionist: FC = () => { ...@@ -37,8 +37,8 @@ const DietReportForNutritionist: FC = () => {
getTextInputProps, getTextInputProps,
handleSubmit, handleSubmit,
isSubmitting, isSubmitting,
isFormUntouched,
isFieldError, isFieldError,
values: formValues,
} = useForm({ } = useForm({
initialValues: dietReportCommentInitialValues, initialValues: dietReportCommentInitialValues,
validationSchema: generateValidationSchema(fieldValidations), validationSchema: generateValidationSchema(fieldValidations),
...@@ -48,7 +48,7 @@ const DietReportForNutritionist: FC = () => { ...@@ -48,7 +48,7 @@ const DietReportForNutritionist: FC = () => {
...values, ...values,
}; };
const response = await createNutritionistCommentApi(payload); const response = await createNutritionistCommentApi(payload);
if (response.success && response.data) { if (response.success) {
Toast.show({ Toast.show({
type: 'success', type: 'success',
text1: 'Sukses membuat komen', text1: 'Sukses membuat komen',
...@@ -76,19 +76,23 @@ const DietReportForNutritionist: FC = () => { ...@@ -76,19 +76,23 @@ const DietReportForNutritionist: FC = () => {
const userReport = userReports[userReports.length - 1]; const userReport = userReports[userReports.length - 1];
const isCurrentPageError = (): boolean => { const isCurrentPageError = (): boolean => {
if (activeSlide === 1) {
return [
formValues.height,
formValues.weight,
formValues.waist_size,
].reduce((acc: boolean, item) => acc || item === '', false);
}
if (activeSlide === 3) { if (activeSlide === 3) {
return isFieldError('average_consumption'); return isFieldError('average_consumption');
} }
const fields = [ const fields = [
...(dietReportTextFields[`dietReportPage${activeSlide}`] || []), ...dietReportTextFields[`dietReportPage${activeSlide}`],
...(dietReportSelectFields[`dietReportPage${activeSlide}`] || []), ...dietReportSelectFields[`dietReportPage${activeSlide}`],
]; ];
return ( return fields.reduce(
isFormUntouched() || (acc: boolean, item) => acc || isFieldError(item.name),
fields.reduce( false,
(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