Skip to content

Commit

Permalink
Merge branch 'test/application-new-tests' of https://github.com/fga-e…
Browse files Browse the repository at this point in the history
…ps-mds/2024-1-GEROcuidado-Front into test/application-new-tests
  • Loading branch information
sebazac332 committed Sep 20, 2024
2 parents de7f719 + b791c17 commit db5e9b1
Showing 1 changed file with 60 additions and 127 deletions.
187 changes: 60 additions & 127 deletions src/app/__tests__/MaskHour.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,170 +1,103 @@
import React from "react";
import { render, fireEvent } from "@testing-library/react-native";
import MaskInput from "../components/MaskHour";

describe("MaskInput Component", () => {
it("should apply mask correctly for valid times", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
);

const input = getByPlaceholderText("Enter time");

// Test valid time inputs
fireEvent.changeText(input, "0923");
expect(mockInputMaskChange).toHaveBeenCalledWith("09:23");

fireEvent.changeText(input, "1545");
expect(mockInputMaskChange).toHaveBeenCalledWith("15:45");
});
import MaskInput from "../components/MaskHour"; // ajuste o caminho se necessário
import MaskHour from "../components/MaskHour";

it("should clear the input correctly", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
);

const input = getByPlaceholderText("Enter time");

// Simulate input change with a value
fireEvent.changeText(input, "0930");
expect(mockInputMaskChange).toHaveBeenCalledWith("09:30");
// Mock da função inputMaskChange
const mockInputMaskChange = jest.fn();

// Clear the input
fireEvent.changeText(input, "");
expect(mockInputMaskChange).toHaveBeenCalledWith("");
describe("MaskInput", () => {
beforeEach(() => {
jest.clearAllMocks(); // Limpa chamadas anteriores dos mocks antes de cada teste
});

it("should handle different lengths of input", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
test("aplica a máscara corretamente", () => {
const { getByTestId } = render(
<MaskInput inputMaskChange={mockInputMaskChange} testID="mask-input" />
);

const input = getByPlaceholderText("Enter time");

// Test varying lengths of input
fireEvent.changeText(input, "1");
expect(mockInputMaskChange).toHaveBeenCalledWith("1");

fireEvent.changeText(input, "123");
expect(mockInputMaskChange).toHaveBeenCalledWith("12:3");
const input = getByTestId("mask-input");

// Simula a mudança do texto
fireEvent.changeText(input, "1234");

// Verifica se inputMaskChange foi chamado com o valor mascarado correto
expect(mockInputMaskChange).toHaveBeenCalledWith("12:34");
});

it("should ignore non-numeric characters", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
test("não permite horas inválidas", () => {
const { getByTestId } = render(
<MaskInput inputMaskChange={mockInputMaskChange} testID="mask-input" />
);

const input = getByPlaceholderText("Enter time");
const input = getByTestId("mask-input");

// Simulate input with non-numeric characters
fireEvent.changeText(input, "12ab34");
expect(mockInputMaskChange).toHaveBeenCalledWith("12:34");
// Simula a mudança do texto
fireEvent.changeText(input, "9999");

fireEvent.changeText(input, "hello");
// Verifica se inputMaskChange foi chamado com uma string vazia
expect(mockInputMaskChange).toHaveBeenCalledWith("");
});

it("should not call inputMaskChange if the value does not change", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
test("verifica se o valor do input é atualizado corretamente", () => {
const { getByTestId } = render(
<MaskInput inputMaskChange={mockInputMaskChange} testID="mask-input" />
);

const input = getByPlaceholderText("Enter time");
const input = getByTestId("mask-input");

// Simulate input change
fireEvent.changeText(input, "1200");
// Simula a mudança do texto
fireEvent.changeText(input, "0959");

// Simulate the same input again
fireEvent.changeText(input, "1200");

// The callback should be called only once with the same value
expect(mockInputMaskChange).toHaveBeenCalledTimes(2);
// Verifica se inputMaskChange foi chamado com o valor mascarado correto
expect(mockInputMaskChange).toHaveBeenCalledWith("09:59");
});

it("should apply mask correctly for inputs longer than required", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
test("não permite que o segundo dígito seja maior que 9", () => {
const { getByTestId } = render(
<MaskInput inputMaskChange={mockInputMaskChange} testID="mask-input" />
);

const input = getByPlaceholderText("Enter time");
const input = getByTestId("mask-input");

// Simula a mudança do texto
fireEvent.changeText(input, "2990");

// Simulate input with more than 4 digits
fireEvent.changeText(input, "123456");
expect(mockInputMaskChange).toHaveBeenCalledWith("12:3456");
// Verifica se inputMaskChange foi chamado apenas com o primeiro dígito
expect(mockInputMaskChange).toHaveBeenCalledWith("29:");
});

it("should reset the input if the first digit of the hour is greater than 2", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
test("não permite que o quarto dígito (minuto) seja maior que 5", () => {
const { getByTestId } = render(
<MaskInput inputMaskChange={mockInputMaskChange} testID="mask-input" />
);

const input = getByPlaceholderText("Enter time");
const input = getByTestId("mask-input");

// Simulate invalid hour (first digit greater than 2)
fireEvent.changeText(input, "3923");
expect(mockInputMaskChange).toHaveBeenCalledWith("");
});
// Simula a mudança do texto
fireEvent.changeText(input, "2359");

it("should only keep the first digit if the second digit of the hour is greater than 9", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
);
// Verifica se inputMaskChange foi chamado com o valor adequado (23:59)
expect(mockInputMaskChange).toHaveBeenCalledWith("23:59");

const input = getByPlaceholderText("Enter time");
// Simula a mudança do texto para um valor inválido nos minutos
fireEvent.changeText(input, "2369");

// Simulate invalid hour (second digit greater than 9)
fireEvent.changeText(input, "1944");
expect(mockInputMaskChange).toHaveBeenCalledWith("19:44");
// Verifica se inputMaskChange foi chamado apenas com os três primeiros dígitos
expect(mockInputMaskChange).toHaveBeenCalledWith("23:");
});

it("should only keep the hour part if the first digit of the minute is greater than 5", () => {
const mockInputMaskChange = jest.fn();
const { getByPlaceholderText } = render(
<MaskInput
inputMaskChange={mockInputMaskChange}
placeholder="Enter time"
/>
test("mantém apenas o primeiro dígito quando o segundo dígito é maior que 9", () => {
const { getByTestId } = render(
<MaskInput inputMaskChange={mockInputMaskChange} testID="mask-input" />
);

const input = getByPlaceholderText("Enter time");
const input = getByTestId("mask-input");

// Simula a mudança do texto com um segundo dígito maior que 9
fireEvent.changeText(input, "1990");

// Simulate invalid minutes (first digit of minute greater than 5)
fireEvent.changeText(input, "1259");
expect(mockInputMaskChange).toHaveBeenCalledWith("12:59");
// Verifica se inputMaskChange foi chamado apenas com o primeiro dígito
expect(mockInputMaskChange).toHaveBeenCalledWith("19:");
});
});
});

0 comments on commit db5e9b1

Please sign in to comment.