Skip to content

Commit

Permalink
Teste dos componentes da primeira história concluídos e testando a pá…
Browse files Browse the repository at this point in the history
…gina de Login
  • Loading branch information
adrianatwatanabe authored Jun 12, 2022
2 parents d253d61 + bd5b60a commit a2ab61d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/components/Button/Button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('Button component', () => {

it('O botão deve chamar a função senForm()', () => {
const sendForm = jest.fn();
render(<Button onClick={sendForm}>ENTRAR</Button>);
const buttonTest = screen.getByText('ENTRAR');
render(<Button onClick={sendForm} role="login">ENTRAR</Button>);
const buttonTest = screen.getByRole('login');
user.click(buttonTest);
expect(sendForm).toHaveBeenCalledTimes(1);
});
Expand Down
6 changes: 3 additions & 3 deletions src/components/Input/Input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ describe('Input component', () => {
});

it('Deve chamar uma função sempre que o seu valor é alterado', () => {
const sendForm = jest.fn();
render(<Input onChange={sendForm} type="text" placeholder="Digite aqui" />);
const addInputValue = jest.fn();
render(<Input onChange={addInputValue} type="text" placeholder="Digite aqui" />);
const inputTest = screen.getByPlaceholderText('Digite aqui');
const text = 'oi';
user.type(inputTest, text);
expect(sendForm).toHaveBeenCalledTimes(text.length);
expect(addInputValue).toHaveBeenCalledTimes(text.length);
});
})
3 changes: 1 addition & 2 deletions src/hooks/useForm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';

const useForm = () => {

const [form, setForm] = React.useState({
name: '',
email: '',
Expand Down Expand Up @@ -49,7 +48,7 @@ const useForm = () => {
checked[index].classList.remove('input-radio:checked:before');
}

return { addInputValue, validatedForm, cleanForm, form };
return { addInputValue, validatedForm, cleanForm, form, setForm };
}

export default useForm;
49 changes: 49 additions & 0 deletions src/pages/Login/Login.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { MemoryRouter } from 'react-router-dom';
import { render, screen, waitFor } from '@testing-library/react';
import user from '@testing-library/user-event';
import Login from '.';
import * as storage from '../../services/storage';

let email = '';
let password = '';

const API = jest.mock('../../services/user', () => ({
...jest.requireActual('../../services/user'),
userLogin: jest.fn()
.mockImplementation(email, password)
.mockResolvedValue('')
}));

describe('Login and UseForm', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('Deverá logar usuário corretamente, validanto todos os testes', async () => {
render(
<MemoryRouter>
<Login />
</MemoryRouter>
);
email = '[email protected]';
password = '123456';

/* Event change on input */
const getEmail = screen.getByPlaceholderText('Digite o seu email');
const getPassword = screen.getByPlaceholderText('Digite a sua senha');
user.type(getEmail, email);
user.type(getPassword, password);

/* Saving value */
getEmail.value = email;
getPassword.value = password;

/* Event click on ENTER button*/
const login = screen.getByRole('login');
user.click(login);
storage.setUserData('Tiemi', password, 'admin');
expect(API.userLogin).toHaveBeenCalled();
expect(API.userLogin).toHaveBeenCalledTimes(1);
storage.deleteUserData();
});
});
6 changes: 3 additions & 3 deletions src/pages/Login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const Login = () => {
value={form.email}
text='EMAIL'
type='text'
placeholder='Digite o email'
placeholder='Digite o seu email'
onChange={addInputValue}
/>
<Input
Expand All @@ -58,11 +58,11 @@ const Login = () => {
value={form.password}
text='SENHA'
type='password'
placeholder='Digite a senha'
placeholder='Digite a sua senha'
onChange={addInputValue}
/>
{message && <Text class='form-message'>{message}</Text>}
<Button type='submit' class='button' onClick={sendForm}>
<Button type='submit' class='button' onClick={sendForm} role="login">
ENTRAR
</Button>
</Form>
Expand Down

0 comments on commit a2ab61d

Please sign in to comment.