Skip to content

Commit

Permalink
test: adding test for blank required fields in cadastrarIdoso file
Browse files Browse the repository at this point in the history
  • Loading branch information
GustaaSZ committed Sep 16, 2024
1 parent 311cd27 commit 41eb7c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
41 changes: 40 additions & 1 deletion src/app/__tests__/cadastrarIdoso.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe("CadastrarIdoso component", () => {
expect(router.push).toHaveBeenCalledWith("/private/pages/listarIdosos");
});
});

it("Cadastra um idoso com sucesso quando todos os dados estão válidos", async () => {
const { getByText, getByPlaceholderText } = render(<CadastrarIdoso />);

Expand All @@ -175,4 +175,43 @@ describe("CadastrarIdoso component", () => {
expect(replace).toHaveBeenCalledWith("/private/pages/listarIdosos");
});
});

it("Navega para a tela anterior ao clicar no botão de voltar quando canGoBack é falso", async () => {
(router.canGoBack as jest.Mock).mockReturnValue(false);

const { getByTestId } = render(<CadastrarIdoso />);

const backButton = getByTestId("back-button-pressable");
fireEvent.press(backButton);

await waitFor(() => {
expect(router.push).toHaveBeenCalledWith("/private/pages/listarIdosos");
});
});

it("Exibe mensagem de erro ao deixar campos obrigatórios em branco", async () => {
const { getByText, getByTestId, getByPlaceholderText } = render(<CadastrarIdoso />);

const nome = getByPlaceholderText("Nome");
const dataNascimento = getByPlaceholderText("Data de Nascimento");
const telefone = getByPlaceholderText("Telefone Responsável");
const cadastrar = getByText("Cadastrar");

act(() => {
fireEvent.changeText(nome, "");
fireEvent.changeText(dataNascimento, "");
fireEvent.changeText(telefone, "");
fireEvent.press(cadastrar);
});

await waitFor(() => {
const erroNome = getByTestId("Erro-nome");
const erroData = getByTestId("Erro-data");
const erroTelefone = getByTestId("Erro-telefone");

expect(erroNome.props.children.props.text).toBe("Campo obrigatório!");
expect(erroData.props.children.props.text).toBe("Campo obrigatório!");
expect(erroTelefone.props.children.props.text).toBe("Campo obrigatório!");
});
});
});
2 changes: 1 addition & 1 deletion src/app/private/pages/cadastrarIdoso.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default function CadastrarIdoso() {


if (!dataNascimento) {
erros.dataNascimento = "Campo obrigatório";
erros.dataNascimento = "Campo obrigatório!";
} else if (!/^\d{2}\/\d{2}\/\d{4}$/.test(dataNascimento)) {
erros.dataNascimento = "Data deve ser no formato dd/mm/yyyy!";
}
Expand Down

0 comments on commit 41eb7c7

Please sign in to comment.