Skip to content

Commit

Permalink
test: add mathematics and remake cnpj generator test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Sep 4, 2024
1 parent b547eef commit 2307fe2
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 8 deletions.
99 changes: 99 additions & 0 deletions src/pages/generators/Cnpj/__tests__/Cnpj.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import { fireEvent, render, screen } from "@testing-library/react-native";

import CnpjGeneratorPage from "..";

describe("Cnpj Generator", () => {
beforeEach(() => {
jest.clearAllMocks();
AsyncStorage.clear();
});

afterEach(() => {
jest.resetAllMocks();
});

it("should render correctly", () => {
render(<CnpjGeneratorPage />);

const title = screen.getByText("Gerador de CNPJ");

expect(title).toBeDefined();
});

it("should generate a random CNPJ without punctuation", async () => {
render(<CnpjGeneratorPage />);

const input = screen.getByPlaceholderText("Clique no botão abaixo");

expect(input).toBeDefined();

const button = screen.getByText("Gerar CNPJ");

expect(button).toBeDefined();

fireEvent.press(button);

expect(input.props.value).toMatch(/^\d{14}$/);
});

it("should change checkbox value", async () => {
render(<CnpjGeneratorPage />);

const checkbox = screen.getByRole("checkbox");

fireEvent(checkbox, "onValueChange", true);

expect(checkbox.props.accessibilityState.checked).toBe(true);

fireEvent(checkbox, "onValueChange", false);

expect(checkbox.props.accessibilityState.checked).toBe(false);
});

it("should generate a random CNPJ with punctuation", async () => {
render(<CnpjGeneratorPage />);

const input = screen.getByPlaceholderText("Clique no botão abaixo");

const checkbox = screen.getByRole("checkbox");

const button = screen.getByText("Gerar CNPJ");

fireEvent(checkbox, "onValueChange", true);

fireEvent.press(button);

expect(input.props.value).toMatch(/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/);
});

it("should copy generated CNPJ to clipboard", async () => {
render(<CnpjGeneratorPage />);

const buttonCopy = screen.getByTestId("buttonCopy");

const button = screen.getByText("Gerar CNPJ");

fireEvent.press(button);

fireEvent.press(buttonCopy);
});

it("should not copy if there is no generated CNPJ", async () => {
render(<CnpjGeneratorPage />);

const buttonCopy = screen.getByTestId("buttonCopy");

fireEvent.press(buttonCopy);
});

it("should load checkbox value from AsyncStorage", async () => {
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce("true");

render(<CnpjGeneratorPage />);

const checkbox = await screen.findByRole("checkbox");

expect(checkbox.props.accessibilityState.checked).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { fireEvent, render, screen } from "@testing-library/react-native";

import LinearEquationPage from "..";

describe("Linear Equation", () => {
it("should render correctly", () => {
render(<LinearEquationPage />);
});

it("should calculate the correct result", async () => {
render(<LinearEquationPage />);

const input = screen.getByPlaceholderText("ax + b = c");

const button = screen.getByText("Calcular");

fireEvent.changeText(input, "2x + 3 = 7");

fireEvent.press(button);

const result = await screen.findByText("O valor de x é: 2");

expect(result).toBeDefined();
});

it("should show error message for invalid equation", async () => {
render(<LinearEquationPage />);

const input = screen.getByPlaceholderText("ax + b = c");

const button = screen.getByText("Calcular");

fireEvent.changeText(input, "2x + 3 = 7 = 8");

fireEvent.press(button);

const result = await screen.findByText("Equação inválida. Use o formato");

expect(result).toBeDefined();
});

it("should show error message for a = 0", async () => {
render(<LinearEquationPage />);

const input = screen.getByPlaceholderText("ax + b = c");

const button = screen.getByText("Calcular");

fireEvent.changeText(input, "0x + 3 = 7");

fireEvent.press(button);

const result = await screen.findByText("a não pode ser zero.");

expect(result).toBeDefined();
});
});
5 changes: 3 additions & 2 deletions src/pages/mathematics/LinearEquation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export default function LinearEquationPage() {
const stylesWithTheme = styles(theme);

const calculateLinearEquation = () => {
// Verifique se a entrada é válida
const cleanSpaces = equation.trim().replace(/\s+/g, "");

const regex = /^(-?\d+)\s?x\s?([+-]?\d+)\s?=\s?(-?\d+)$/;
const match = equation.match(regex);
const match = cleanSpaces.match(regex);

if (!match) {
setResult(t("Equação inválida. Use o formato"));
Expand Down
63 changes: 63 additions & 0 deletions src/pages/mathematics/MmcMdc/__tests__/MmcMdc.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { fireEvent, render, screen } from "@testing-library/react-native";
import { Alert } from "react-native";

import MmcMdcPage from "..";

describe("MmcMdc", () => {
it("should render correctly", () => {
render(<MmcMdcPage />);
});

it("should calculate the correct result", async () => {
render(<MmcMdcPage />);

const input = screen.getByPlaceholderText("Ex. 4, 8, 12");

const button = screen.getByText("Calcular");

fireEvent.changeText(input, "4, 8, 12");

fireEvent.press(button);

const resultMmc = await screen.findByText("Mmc entre 4, 8, 12 = 24");
const resultMdc = await screen.findByText("Mdc entre 4, 8, 12 = 4");

expect(resultMmc).toBeDefined();

expect(resultMdc).toBeDefined();
});

it("should Alert.alert error message for invalid input", async () => {
jest.spyOn(Alert, "alert");

render(<MmcMdcPage />);

const input = screen.getByPlaceholderText("Ex. 4, 8, 12");

const button = screen.getByText("Calcular");

fireEvent.changeText(input, "ads15");

fireEvent.press(button);

expect(Alert.alert).toHaveBeenCalledWith(
"Erro",
"Digite valores numéricos separados por vírgulas"
);
});

it("should Alert.alert error message for empty input", async () => {
jest.spyOn(Alert, "alert");

render(<MmcMdcPage />);

const button = screen.getByText("Calcular");

fireEvent.press(button);

expect(Alert.alert).toHaveBeenCalledWith(
"Erro",
"Digite valores numéricos separados por vírgulas"
);
});
});
6 changes: 0 additions & 6 deletions src/pages/mathematics/MmcMdc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export default function MmcMdcPage() {

// Função para calcular o MMC de uma array de números
const calculateMmc = (numbers: number[]) => {
if (numbers.length === 0) {
return null;
}
let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
result =
Expand All @@ -73,9 +70,6 @@ export default function MmcMdcPage() {

// Função para calcular o MDC de uma array de números
const calculateMdc = (numbers: number[]) => {
if (numbers.length === 0) {
return null;
}
let result = numbers[0];
for (let i = 1; i < numbers.length; i++) {
result = calculateMdcTwoNumbers(result, numbers[i]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
fireEvent,
render,
screen,
waitFor,
} from "@testing-library/react-native";

import QuadraticEquationPage from "..";

describe("QuadraticEquation", () => {
beforeEach(() => {
jest.clearAllMocks();
});

afterEach(() => {
jest.resetAllMocks();
});

it("should render correctly", () => {
render(<QuadraticEquationPage />);
});

describe("calculateQuadraticEquation", () => {
it("should calculate the correct result", async () => {
render(<QuadraticEquationPage />);

const inputA = screen.getByPlaceholderText(
"Termo que acompanha o x², ex: 5x²"
);
const inputB = screen.getByPlaceholderText(
"Termo que acompanha o x, ex: 3x"
);
const inputC = screen.getByPlaceholderText("Termo independente, ex: -4");

const button = screen.getByText("Calcular");

fireEvent.changeText(inputA, "5");
fireEvent.changeText(inputB, "3");
fireEvent.changeText(inputC, "-4");

fireEvent.press(button);

const result = await screen.findByText(
"Resultado: First root:0.6433981132056603 Second root:-1.2433981132056604"
);

expect(result).toBeDefined();
});

it("should calculate the correct result using approximation", async () => {
render(<QuadraticEquationPage />);

const inputA = screen.getByPlaceholderText(
"Termo que acompanha o x², ex: 5x²"
);
const inputB = screen.getByPlaceholderText(
"Termo que acompanha o x, ex: 3x"
);
const inputC = screen.getByPlaceholderText("Termo independente, ex: -4");

const checkbox = screen.getByTestId("checkbox-aproximate");

const button = screen.getByText("Calcular");

fireEvent.changeText(inputA, "5");
fireEvent.changeText(inputB, "3");
fireEvent.changeText(inputC, "-4");

fireEvent(checkbox, "onValueChange", true);

await waitFor(() => {
expect(checkbox.props.accessibilityState.checked).toBe(true);
});

fireEvent.press(button);

const result = await screen.findByText(
"Resultado: First root:0.64 Second root:-1.24"
);

expect(result).toBeDefined();
});

it("should calculate the correct result and clear the fields", async () => {
render(<QuadraticEquationPage />);

const inputA = screen.getByPlaceholderText(
"Termo que acompanha o x², ex: 5x²"
);
const inputB = screen.getByPlaceholderText(
"Termo que acompanha o x, ex: 3x"
);
const inputC = screen.getByPlaceholderText("Termo independente, ex: -4");

const checkbox = screen.getByTestId("checkbox-deleteAfter");

const button = screen.getByText("Calcular");

fireEvent.changeText(inputA, "5");
fireEvent.changeText(inputB, "3");
fireEvent.changeText(inputC, "-4");

fireEvent(checkbox, "onValueChange", true);

await waitFor(() => {
expect(checkbox.props.accessibilityState.checked).toBe(true);
});

fireEvent.press(button);

const result = await screen.findByText(
"Resultado: First root:0.6433981132056603 Second root:-1.2433981132056604"
);

expect(result).toBeDefined();

expect(inputA.props.value).toBe("");
expect(inputB.props.value).toBe("");
expect(inputC.props.value).toBe("");
});
});
});
2 changes: 2 additions & 0 deletions src/pages/mathematics/QuadraticEquation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export default function QuadraticEquationPage() {
<View style={stylesWithTheme.checkboxContainer}>
<Text style={stylesWithTheme.label}>{t("Usar aproximação ?")}</Text>
<CheckBox
testID="checkbox-aproximate"
value={aproxima}
onValueChange={async (value) => {
await AsyncStorage.setItem(
Expand All @@ -166,6 +167,7 @@ export default function QuadraticEquationPage() {
{t("Limpar campos após gerar ?")}
</Text>
<CheckBox
testID="checkbox-deleteAfter"
value={deleteAfter}
onValueChange={async (value) => {
await AsyncStorage.setItem(
Expand Down

0 comments on commit 2307fe2

Please sign in to comment.