-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
327 additions
and
21 deletions.
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
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 |
---|---|---|
@@ -1,17 +1,93 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { render, fireEvent, waitFor } from '@testing-library/react'; | ||
|
||
import SignIn from '../../pages/SignIn'; | ||
|
||
const mockedHistoryPush = jest.fn(); | ||
const mockedSignIn = jest.fn(); | ||
const mockedAddToast = jest.fn(); | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useHistory: jest.fn(), | ||
useHistory: () => ({ | ||
push: mockedHistoryPush, | ||
}), | ||
Link: ({ children }: { children: React.ReactNode }) => children, | ||
})); | ||
|
||
jest.mock('../../hooks/auth', () => ({ | ||
useAuth: () => ({ | ||
signIn: mockedSignIn, | ||
}), | ||
})); | ||
|
||
jest.mock('../../hooks/toast', () => ({ | ||
useToast: () => ({ | ||
addToast: mockedAddToast, | ||
}), | ||
})); | ||
|
||
describe('SignIn Page', () => { | ||
it('should be able to sign in', () => { | ||
const { debug } = render(<SignIn />); | ||
beforeEach(() => { | ||
mockedHistoryPush.mockClear(); | ||
mockedAddToast.mockClear(); | ||
}); | ||
|
||
it('should be able to sign in', async () => { | ||
const { getByPlaceholderText, getByText } = render(<SignIn />); | ||
|
||
const emailField = getByPlaceholderText('E-mail'); | ||
const passwordField = getByPlaceholderText('Senha'); | ||
const buttonElement = getByText('Entrar'); | ||
|
||
fireEvent.change(emailField, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordField, { target: { value: '123456' } }); | ||
|
||
fireEvent.click(buttonElement); | ||
|
||
await waitFor(() => { | ||
expect(mockedHistoryPush).toHaveBeenCalledWith('/dashboard'); | ||
}); | ||
}); | ||
|
||
it('should not be able to sign in with invalid credentials', async () => { | ||
const { getByPlaceholderText, getByText } = render(<SignIn />); | ||
|
||
const emailField = getByPlaceholderText('E-mail'); | ||
const passwordField = getByPlaceholderText('Senha'); | ||
const buttonElement = getByText('Entrar'); | ||
|
||
fireEvent.change(emailField, { target: { value: 'not-valid-email' } }); | ||
fireEvent.change(passwordField, { target: { value: '123456' } }); | ||
|
||
fireEvent.click(buttonElement); | ||
|
||
await waitFor(() => { | ||
expect(mockedHistoryPush).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should display an error if login fails', async () => { | ||
mockedSignIn.mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
|
||
const { getByPlaceholderText, getByText } = render(<SignIn />); | ||
|
||
const emailField = getByPlaceholderText('E-mail'); | ||
const passwordField = getByPlaceholderText('Senha'); | ||
const buttonElement = getByText('Entrar'); | ||
|
||
fireEvent.change(emailField, { target: { value: '[email protected]' } }); | ||
fireEvent.change(passwordField, { target: { value: '123456' } }); | ||
|
||
fireEvent.click(buttonElement); | ||
|
||
debug(); | ||
await waitFor(() => { | ||
expect(mockedAddToast).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
type: 'error', | ||
}), | ||
); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.