Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for src/screens/OrganizationActionItems/ActionItemUpdateModal.tsx #1834

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 235 additions & 0 deletions src/screens/OrganizationActionItems/ActionItemUpdateModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ActionItemUpdateModal from './ActionItemUpdateModal';
import { MockedProvider } from '@apollo/react-testing';
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import { store } from 'state/store';
import i18nForTest from 'utils/i18nForTest';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import type { InterfaceMemberInfo } from 'utils/interfaces';
import { t } from 'i18next';

const mockMembersData: InterfaceMemberInfo[] = [
{
_id: '1',
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
image: 'https://example.com/john-doe.jpg',
createdAt: '2022-01-01T00:00:00.000Z',
organizationsBlockedBy: [],
},
{
_id: '2',
firstName: 'Jane',
lastName: 'Smith',
email: '[email protected]',
image: 'https://example.com/jane-smith.jpg',
createdAt: '2022-02-01T00:00:00.000Z',
organizationsBlockedBy: [],
},
];

const mockFormState = {
assigneeId: '1',
assignee: 'John Doe',
assigner: 'Jane Smith',
isCompleted: false,
preCompletionNotes: 'Test pre-completion notes',
postCompletionNotes: '',
};

const mockDueDate = new Date('2023-05-01');
const mockCompletionDate = new Date('2023-05-15');

const mockHideUpdateModal = jest.fn();
const mockSetFormState = jest.fn();
const mockUpdateActionItemHandler = jest.fn();
const mockSetDueDate = jest.fn();
const mockSetCompletionDate = jest.fn();
const mockT = (key: string): string => key;

describe('ActionItemUpdateModal', () => {
test('renders modal correctly', () => {
render(
<MockedProvider addTypename={false}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<ActionItemUpdateModal
actionItemUpdateModalIsOpen
hideUpdateModal={mockHideUpdateModal}
formState={mockFormState}
setFormState={mockSetFormState}
updateActionItemHandler={mockUpdateActionItemHandler}
t={mockT}
membersData={mockMembersData}
dueDate={mockDueDate}
setDueDate={mockSetDueDate}
completionDate={mockCompletionDate}
setCompletionDate={mockSetCompletionDate}
/>
</LocalizationProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>,
);

expect(screen.getByText('actionItemDetails')).toBeInTheDocument();
expect(
screen.getByTestId('updateActionItemModalCloseBtn'),
).toBeInTheDocument();
expect(screen.getByTestId('formUpdateAssignee')).toBeInTheDocument();
expect(screen.getByLabelText('preCompletionNotes')).toBeInTheDocument();
expect(screen.getByLabelText('dueDate')).toBeInTheDocument();
expect(screen.getByLabelText('completionDate')).toBeInTheDocument();
expect(screen.getByTestId('editActionItemBtn')).toBeInTheDocument();
});

test('closes modal when close button is clicked', () => {
render(
<MockedProvider addTypename={false}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<ActionItemUpdateModal
actionItemUpdateModalIsOpen
hideUpdateModal={mockHideUpdateModal}
formState={mockFormState}
setFormState={mockSetFormState}
updateActionItemHandler={mockUpdateActionItemHandler}
t={mockT}
membersData={mockMembersData}
dueDate={mockDueDate}
setDueDate={mockSetDueDate}
completionDate={mockCompletionDate}
setCompletionDate={mockSetCompletionDate}
/>
</LocalizationProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>,
);

fireEvent.click(screen.getByTestId('updateActionItemModalCloseBtn'));
expect(mockHideUpdateModal).toHaveBeenCalled();
});

test('updates form state when assignee is changed', () => {
render(
<MockedProvider addTypename={false}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<ActionItemUpdateModal
actionItemUpdateModalIsOpen
hideUpdateModal={mockHideUpdateModal}
formState={mockFormState}
setFormState={mockSetFormState}
updateActionItemHandler={mockUpdateActionItemHandler}
t={mockT}
membersData={mockMembersData}
dueDate={mockDueDate}
setDueDate={mockSetDueDate}
completionDate={mockCompletionDate}
setCompletionDate={mockSetCompletionDate}
/>
</LocalizationProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>,
);

const assigneeSelect = screen.getByTestId('formUpdateAssignee');
userEvent.selectOptions(assigneeSelect, '2');
expect(mockSetFormState).toHaveBeenCalledWith({
...mockFormState,
assigneeId: '2',
});
});

test('tests the condition for formState.preCompletionNotes', () => {
const mockFormState = {
assigneeId: '1',
assignee: 'John Doe',
assigner: 'Jane Smith',
isCompleted: false,
preCompletionNotes: '',
postCompletionNotes: '',
};
render(
<MockedProvider addTypename={false}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<ActionItemUpdateModal
actionItemUpdateModalIsOpen
hideUpdateModal={mockHideUpdateModal}
formState={mockFormState}
setFormState={mockSetFormState}
updateActionItemHandler={mockUpdateActionItemHandler}
t={mockT}
membersData={mockMembersData}
dueDate={mockDueDate}
setDueDate={mockSetDueDate}
completionDate={mockCompletionDate}
setCompletionDate={mockSetCompletionDate}
/>
</LocalizationProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>,
);
const preCompletionNotesInput = screen.getByLabelText('preCompletionNotes');
fireEvent.change(preCompletionNotesInput, {
target: { value: 'New pre-completion notes' },
});
expect(mockSetFormState).toHaveBeenCalledWith({
...mockFormState,
preCompletionNotes: 'New pre-completion notes',
});
});

test('calls updateActionItemHandler when form is submitted', () => {
render(
<MockedProvider addTypename={false}>
<Provider store={store}>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<ActionItemUpdateModal
actionItemUpdateModalIsOpen
hideUpdateModal={mockHideUpdateModal}
formState={mockFormState}
setFormState={mockSetFormState}
updateActionItemHandler={mockUpdateActionItemHandler}
t={mockT}
membersData={mockMembersData}
dueDate={mockDueDate}
setDueDate={mockSetDueDate}
completionDate={mockCompletionDate}
setCompletionDate={mockSetCompletionDate}
/>
</LocalizationProvider>
</I18nextProvider>
</BrowserRouter>
</Provider>
</MockedProvider>,
);

fireEvent.submit(screen.getByTestId('editActionItemBtn'));
expect(mockUpdateActionItemHandler).toHaveBeenCalled();
});
});
22 changes: 14 additions & 8 deletions src/screens/OrganizationActionItems/ActionItemUpdateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,29 @@ const ActionItemUpdateModal: React.FC<InterfaceActionItemCreateModalProps> = ({
label={t('dueDate')}
className={styles.datebox}
value={dayjs(dueDate)}
onChange={(date: Dayjs | null): void => {
if (date) {
setDueDate(date?.toDate());
onChange={
/* istanbul ignore next */ (date: Dayjs | null): void => {
/* istanbul ignore next */
if (date) {
setDueDate(date?.toDate());
}
}
}}
}
/>
</div>
<div>
<DatePicker
label={t('completionDate')}
className={styles.datebox}
value={dayjs(completionDate)}
onChange={(date: Dayjs | null): void => {
if (date) {
setCompletionDate(date?.toDate());
onChange={
/* istanbul ignore next */ (date: Dayjs | null): void => {
/* istanbul ignore next */
if (date) {
setCompletionDate(date?.toDate());
}
}
}}
}
/>
</div>
</div>
Expand Down
Loading