Created
July 19, 2021 19:59
-
-
Save tiagolpadua/de1b01ea7f52cbffc1696c71a153454c to your computer and use it in GitHub Desktop.
Exemplo de teste de hook customizado
This file contains hidden or 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
| import { useState } from 'react'; | |
| function useErros(validacoes) { | |
| const estadoInicial = criarEstadoInicial(validacoes); | |
| const [erros, setErros] = useState(estadoInicial); | |
| function validarCampos(event) { | |
| const { name, value } = event.target; | |
| const novoEstado = { ...erros }; | |
| novoEstado[name] = validacoes[name](value); | |
| setErros(novoEstado); | |
| } | |
| function possoEnviar() { | |
| for (let campo in erros) { | |
| if (!erros[campo].valido) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| return [erros, validarCampos, possoEnviar]; | |
| } | |
| function criarEstadoInicial(validacoes) { | |
| const estadoInicial = {}; | |
| for (let campo in validacoes) { | |
| estadoInicial[campo] = { valido: true, texto: "" }; | |
| } | |
| return estadoInicial; | |
| } | |
| export default useErros; |
This file contains hidden or 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
| import { renderHook, act } from '@testing-library/react-hooks' | |
| import useErros from './useErros' | |
| test('não deve permitir enviar campo com erro', () => { | |
| const validacoes = { | |
| marca: dado => { | |
| if (dado && dado.length >= 3) { | |
| return { valido: true }; | |
| } else { | |
| return { valido: false, texto: "Marca deve ter ao menos 3 letras." } | |
| } | |
| } | |
| } | |
| const { result } = renderHook(() => useErros(validacoes)) | |
| act(() => { | |
| // eslint-disable-next-line no-unused-vars | |
| const [erros, validarCampos, possoEnviar] = result.current; | |
| validarCampos({ target: { name: 'marca', value: 'x' } }); | |
| }) | |
| // eslint-disable-next-line no-unused-vars | |
| const [erros, validarCampos, possoEnviar] = result.current; | |
| expect(erros).toEqual({ | |
| marca: { | |
| texto: "Marca deve ter ao menos 3 letras.", | |
| valido: false | |
| } | |
| }); | |
| expect(possoEnviar()).toBe(false); | |
| }) | |
| test('deve permitir enviar campo sem erro', () => { | |
| const validacoes = { | |
| marca: dado => { | |
| if (dado && dado.length >= 3) { | |
| return { valido: true }; | |
| } else { | |
| return { valido: false, texto: "Marca deve ter ao menos 3 letras." } | |
| } | |
| } | |
| } | |
| const { result } = renderHook(() => useErros(validacoes)) | |
| act(() => { | |
| // eslint-disable-next-line no-unused-vars | |
| const [erros, validarCampos, possoEnviar] = result.current; | |
| validarCampos({ target: { name: 'marca', value: 'xxx' } }); | |
| }) | |
| // eslint-disable-next-line no-unused-vars | |
| const [erros, validarCampos, possoEnviar] = result.current; | |
| expect(erros).toEqual({ marca: { valido: true } }); | |
| expect(possoEnviar()).toBe(true); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment