-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEV-1314] EmailFormWrapper unit tests (#581)
- Loading branch information
1 parent
3d01f5b
commit 4caeaaf
Showing
7 changed files
with
675 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"nextjs-website": patch | ||
--- | ||
|
||
Add EmailFormWrapper unit tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// eslint-disable-next-line functional/immutable-data, functional/no-expression-statements | ||
module.exports = {}; |
125 changes: 125 additions & 0 deletions
125
apps/nextjs-website/src/__tests__/components/EmailFormWrapper.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import EmailFormWrapper from '@/components/organisms/EmailFormWrapper/EmailFormWrapper'; | ||
import Wrapper from './Wrapper'; | ||
|
||
describe('EmailFormWrapper', () => { | ||
const mockOnCancel = jest.fn(); | ||
const mockOnSave = jest.fn((email: string) => Promise.resolve()); | ||
const mockOnEdit = jest.fn(); | ||
|
||
const item = { | ||
editable: true, | ||
name: 'email', | ||
title: 'Email', | ||
value: '[email protected]', | ||
onEdit: mockOnEdit, | ||
}; | ||
|
||
it('should render EditEmailForm when isEditing is true', () => { | ||
const { getByRole } = render( | ||
<Wrapper> | ||
<EmailFormWrapper | ||
item={item} | ||
isEditing={true} | ||
onCancel={mockOnCancel} | ||
onSave={mockOnSave} | ||
onEdit={mockOnEdit} | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
expect(getByRole('textbox')).toBeTruthy(); | ||
}); | ||
|
||
it('should render InfoCardItem when isEditing is false', () => { | ||
const { getAllByText } = render( | ||
<Wrapper> | ||
<EmailFormWrapper | ||
item={item} | ||
isEditing={false} | ||
onCancel={mockOnCancel} | ||
onSave={mockOnSave} | ||
onEdit={mockOnEdit} | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
expect(getAllByText(item.value)).toBeTruthy(); | ||
}); | ||
|
||
it('should call onEdit when edit button is clicked', () => { | ||
const { getByRole } = render( | ||
<Wrapper> | ||
<EmailFormWrapper | ||
item={item} | ||
isEditing={false} | ||
onCancel={mockOnCancel} | ||
onSave={mockOnSave} | ||
onEdit={mockOnEdit} | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
fireEvent.click(getByRole('button')); | ||
expect(mockOnEdit).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onCancel when cancel button is clicked', () => { | ||
const { getAllByRole } = render( | ||
<Wrapper> | ||
<EmailFormWrapper | ||
item={item} | ||
isEditing={true} | ||
onCancel={mockOnCancel} | ||
onSave={mockOnSave} | ||
onEdit={mockOnEdit} | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
fireEvent.click(getAllByRole('button', { name: /annulla/i })[0]); | ||
expect(mockOnCancel).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call onSave when save button is clicked and email input has not valid value', () => { | ||
const { getByRole, getAllByRole } = render( | ||
<Wrapper> | ||
<EmailFormWrapper | ||
item={item} | ||
isEditing={true} | ||
onCancel={mockOnCancel} | ||
onSave={mockOnSave} | ||
onEdit={mockOnEdit} | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
fireEvent.change(getByRole('textbox'), { | ||
target: { value: 'updated' }, | ||
}); | ||
|
||
fireEvent.click(getAllByRole('button', { name: /conferma/i })[0]); | ||
expect(mockOnSave).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onSave when save button is clicked and email input is valid', () => { | ||
const { getByRole, getAllByRole } = render( | ||
<Wrapper> | ||
<EmailFormWrapper | ||
item={item} | ||
isEditing={true} | ||
onCancel={mockOnCancel} | ||
onSave={mockOnSave} | ||
onEdit={mockOnEdit} | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
fireEvent.change(getByRole('textbox'), { | ||
target: { value: '[email protected]' }, | ||
}); | ||
|
||
fireEvent.click(getAllByRole('button', { name: /conferma/i })[0]); | ||
expect(mockOnSave).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NextIntlClientProvider } from 'next-intl'; | ||
import { PropsWithChildren } from 'react'; | ||
|
||
import messages from '@/messages/it.json'; | ||
|
||
const Wrapper = ({ children }: PropsWithChildren) => ( | ||
<NextIntlClientProvider locale={'it'} messages={messages}> | ||
{children} | ||
</NextIntlClientProvider> | ||
); | ||
|
||
export default Wrapper; |
Oops, something went wrong.