|
1 | | -import React from 'react'; |
2 | | -import { render } from '@testing-library/react'; |
| 1 | +import React, { useContext } from 'react'; |
| 2 | +import { render, fireEvent, wait } from '@testing-library/react'; |
| 3 | +import { renderHook } from '@testing-library/react-hooks'; |
| 4 | +import { ThemeContext, DefaultTheme } from 'styled-components'; |
3 | 5 |
|
4 | 6 | import SignIn from '../../pages/SignIn'; |
| 7 | +import { ThemeProvider } from '../../hooks/theme'; |
| 8 | + |
| 9 | +const mockedHistoryPush = jest.fn(); |
| 10 | +const mockedAddToast = jest.fn(); |
| 11 | +const mockedSignIn = jest.fn(); |
| 12 | + |
| 13 | +jest.mock('react-router-dom', () => { |
| 14 | + return { |
| 15 | + useHistory: () => ({ |
| 16 | + push: mockedHistoryPush, |
| 17 | + }), |
| 18 | + Link: ({ children }: { children: React.ReactNode }) => children, |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +jest.mock('../../hooks/auth', () => { |
| 23 | + return { |
| 24 | + useAuth: () => ({ |
| 25 | + signIn: mockedSignIn, |
| 26 | + }), |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +jest.mock('../../hooks/toast', () => { |
| 31 | + return { |
| 32 | + useToast: () => ({ |
| 33 | + addToast: mockedAddToast, |
| 34 | + }), |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +jest.mock('react', () => { |
| 39 | + return { |
| 40 | + ...jest.requireActual('react'), |
| 41 | + useContext: jest.fn(), |
| 42 | + }; |
| 43 | +}); |
5 | 44 |
|
6 | 45 | describe('SignIn Page', () => { |
7 | | - it('should be able to SignIn', () => { |
8 | | - const { debug } = render(<SignIn />); |
| 46 | + beforeEach(() => { |
| 47 | + mockedHistoryPush.mockClear(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should be able to sign in', async () => { |
| 51 | + const { getByPlaceholderText, getByText } = render(<SignIn />); |
| 52 | + renderHook(() => useContext<DefaultTheme>(ThemeContext), { |
| 53 | + wrapper: ThemeProvider, |
| 54 | + }); |
| 55 | + |
| 56 | + const emailField = getByPlaceholderText('E-mail'); |
| 57 | + const passwordField = getByPlaceholderText('Senha'); |
| 58 | + const buttonElement = getByText('Entrar'); |
| 59 | + |
| 60 | + fireEvent.change(emailField, { target: { value: '[email protected]' } }); |
| 61 | + fireEvent.change(passwordField, { target: { value: '123456' } }); |
| 62 | + |
| 63 | + fireEvent.click(buttonElement); |
| 64 | + |
| 65 | + await wait(() => { |
| 66 | + expect(mockedAddToast).toHaveBeenCalledWith( |
| 67 | + expect.objectContaining({ |
| 68 | + type: 'success', |
| 69 | + }), |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should not be able to sign in with invalid crendentials', async () => { |
| 75 | + const { getByPlaceholderText, getByText } = render(<SignIn />); |
| 76 | + |
| 77 | + const emailField = getByPlaceholderText('E-mail'); |
| 78 | + const passwordField = getByPlaceholderText('Senha'); |
| 79 | + const buttonElement = getByText('Entrar'); |
| 80 | + |
| 81 | + fireEvent.change(emailField, { target: { value: 'invalid-credential' } }); |
| 82 | + fireEvent.change(passwordField, { target: { value: '123456' } }); |
| 83 | + |
| 84 | + fireEvent.click(buttonElement); |
| 85 | + |
| 86 | + await wait(() => { |
| 87 | + expect(mockedHistoryPush).not.toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should display an error if login fails', async () => { |
| 92 | + mockedSignIn.mockImplementation(() => { |
| 93 | + throw new Error(); |
| 94 | + }); |
| 95 | + |
| 96 | + const { getByPlaceholderText, getByText } = render(<SignIn />); |
| 97 | + |
| 98 | + const emailField = getByPlaceholderText('E-mail'); |
| 99 | + const passwordField = getByPlaceholderText('Senha'); |
| 100 | + const buttonElement = getByText('Entrar'); |
| 101 | + |
| 102 | + fireEvent.change(emailField, { target: { value: '[email protected]' } }); |
| 103 | + fireEvent.change(passwordField, { target: { value: '123456' } }); |
| 104 | + |
| 105 | + fireEvent.click(buttonElement); |
9 | 106 |
|
10 | | - debug(); |
| 107 | + await wait(() => { |
| 108 | + expect(mockedAddToast).toHaveBeenCalledWith( |
| 109 | + expect.objectContaining({ |
| 110 | + type: 'error', |
| 111 | + }), |
| 112 | + ); |
| 113 | + }); |
11 | 114 | }); |
12 | 115 | }); |
0 commit comments