From 578749f1c08432a99ac0edfb96d3928b4e4a7771 Mon Sep 17 00:00:00 2001 From: Shubham Sharma <68867418+skv93-coder@users.noreply.github.com> Date: Sat, 10 Aug 2024 00:15:57 +0530 Subject: [PATCH] Corrected loading issue and extra space in standup submit (#1230) Co-authored-by: Shubham Sharma --- .../Components/Standup/FormComponent.test.tsx | 47 ++++++++++++---- .../Unit/Components/Standup/MainForm.test.tsx | 48 ++++++++++++++++ .../Standup/StandupContainer.test.tsx | 6 -- src/components/standup/FormInputComponent.tsx | 33 ++++------- src/components/standup/MainForm.tsx | 17 ++++++ src/components/standup/index.tsx | 56 +++++++------------ 6 files changed, 133 insertions(+), 74 deletions(-) create mode 100644 __tests__/Unit/Components/Standup/MainForm.test.tsx create mode 100644 src/components/standup/MainForm.tsx diff --git a/__tests__/Unit/Components/Standup/FormComponent.test.tsx b/__tests__/Unit/Components/Standup/FormComponent.test.tsx index bf5399b3a..2536fc268 100644 --- a/__tests__/Unit/Components/Standup/FormComponent.test.tsx +++ b/__tests__/Unit/Components/Standup/FormComponent.test.tsx @@ -7,6 +7,7 @@ import { Provider } from 'react-redux'; import { setupServer } from 'msw/node'; import { failedPostStandup } from '../../../../__mocks__/handlers/standup.handler'; import handlers from '../../../../__mocks__/handlers'; +import { cleanup } from '@testing-library/react'; const server = setupServer(...handlers); @@ -15,6 +16,9 @@ describe('FormComponent', () => { server.listen(); }); afterEach(() => { + cleanup(); + jest.clearAllMocks(); + jest.resetModules(); server.resetHandlers(); }); afterAll(() => { @@ -24,11 +28,7 @@ describe('FormComponent', () => { test('should be able to submit the standup form', async () => { const { container } = renderWithRouter( - { - true; - }} - /> + , { @@ -75,11 +75,7 @@ describe('FormComponent', () => { server.use(failedPostStandup); const { container } = renderWithRouter( - { - true; - }} - /> + , { @@ -119,4 +115,35 @@ describe('FormComponent', () => { ).toBeInTheDocument() ); }); + test('should disable the submit button after first click', async () => { + const { container } = renderWithRouter( + + + + , + { + asPath: '/standup', + replace: jest.fn(), + } + ); + + const YesterdayInptutField = screen.getByTestId('Yesterday0'); + fireEvent.change(YesterdayInptutField, { + target: { value: 'Working on a backend Go project1' }, + }); + + const todayInputField = screen.getByTestId('Today0'); + fireEvent.change(todayInputField, { + target: { value: 'Working on a live-site project' }, + }); + + const BlockerInputField = screen.getByTestId('Blocker0'); + fireEvent.change(BlockerInputField, { + target: { value: 'None' }, + }); + const submitButton = container.getElementsByClassName('submitButton'); + expect(submitButton[0]).not.toBeDisabled(); + fireEvent.submit(screen.getByRole('form')); + expect(submitButton[0]).toBeDisabled(); + }); }); diff --git a/__tests__/Unit/Components/Standup/MainForm.test.tsx b/__tests__/Unit/Components/Standup/MainForm.test.tsx new file mode 100644 index 000000000..a9458fb27 --- /dev/null +++ b/__tests__/Unit/Components/Standup/MainForm.test.tsx @@ -0,0 +1,48 @@ +import { setupServer } from 'msw/node'; +import handlers from '../../../../__mocks__/handlers'; +import MainForm from '@/components/standup/MainForm'; +import { renderWithRouter } from '@/test_utils/createMockRouter'; +import { Provider } from 'react-redux'; +import { store } from '@/app/store'; +import { screen, waitFor } from '@testing-library/react'; + +const server = setupServer(...handlers); + +describe('MainFOrm', () => { + beforeAll(() => { + server.listen(); + }); + afterEach(() => { + server.resetHandlers(); + }); + afterAll(() => { + server.close(); + }); + + test('If isFormVisible is true it should render form for standup. ', async () => { + renderWithRouter( + + + + ); + + await waitFor(() => + expect(screen.getByRole('form')).toBeInTheDocument() + ); + }); + test('If isFormVisible is false it should render form for standup. ', async () => { + renderWithRouter( + + + + ); + + await waitFor(() => + expect( + screen.getByText( + 'Your standup for the day has already been submitted, please fill out the form tomorrow after 6:00 a.m.' + ) + ).toBeInTheDocument() + ); + }); +}); diff --git a/__tests__/Unit/Components/Standup/StandupContainer.test.tsx b/__tests__/Unit/Components/Standup/StandupContainer.test.tsx index 83f5b0698..6549dcb3a 100644 --- a/__tests__/Unit/Components/Standup/StandupContainer.test.tsx +++ b/__tests__/Unit/Components/Standup/StandupContainer.test.tsx @@ -61,7 +61,6 @@ describe('StandupContainer', () => { replace: jest.fn(), } ); - const addButton = container.getElementsByClassName('addButton'); const removeButton = container.getElementsByClassName('removeButton'); const YesterdayInptutField = screen.getByTestId('Yesterday0'); @@ -96,11 +95,6 @@ describe('StandupContainer', () => { expect( screen.getByText('User Progress document created successfully.') ).toBeInTheDocument(); - expect( - screen.getByText( - 'Your standup for the day has already been submitted, please fill out the form tomorrow after 6:00 a.m.' - ) - ).toBeInTheDocument(); }); }); }); diff --git a/src/components/standup/FormInputComponent.tsx b/src/components/standup/FormInputComponent.tsx index 290b38c62..3e19641c4 100644 --- a/src/components/standup/FormInputComponent.tsx +++ b/src/components/standup/FormInputComponent.tsx @@ -1,26 +1,18 @@ -import { FC, useState } from 'react'; +import { useState } from 'react'; import { useRouter } from 'next/router'; import { useSaveProgressMutation } from '@/app/services/progressesApi'; import { toast, ToastTypes } from '@/helperFunctions/toast'; import styles from '@/components/standup/standupContainer.module.scss'; import SectionComponent from './SectionComponent'; -const intialSection = [ - { title: 'Yesterday', inputs: [''] }, - { title: 'Today', inputs: [''] }, - { title: 'Blocker', inputs: [''] }, -]; - -interface setIsFormVisibleProps { - setIsFormVisible: React.Dispatch>; -} - -const FormInputComponent: FC = ({ - setIsFormVisible, -}) => { +const FormInputComponent = () => { const router = useRouter(); - const [sections, setSections] = useState(intialSection); - const [addStandup] = useSaveProgressMutation(); + const [sections, setSections] = useState([ + { title: 'Yesterday', inputs: [''] }, + { title: 'Today', inputs: [''] }, + { title: 'Blocker', inputs: [''] }, + ]); + const [addStandup, { isLoading }] = useSaveProgressMutation(); const { SUCCESS, ERROR } = ToastTypes; const handleAddField = (sectionIndex: number) => { @@ -60,11 +52,10 @@ const FormInputComponent: FC = ({ e.preventDefault(); const newData = { type: 'user', - completed: sections[0].inputs.join('.'), - planned: sections[1].inputs.join('. '), - blockers: sections[2].inputs.join('. '), + completed: sections[0].inputs.join(' ').trim(), + planned: sections[1].inputs.join(' ').trim(), + blockers: sections[2].inputs.join(' ').trim(), }; - setIsFormVisible(false); await addStandup(newData) .unwrap() .then((data) => { @@ -100,7 +91,7 @@ const FormInputComponent: FC = ({ ))}