From 2307fe2f57e09714f219c7e693d7e8d5a3fe904a Mon Sep 17 00:00:00 2001 From: Gabriel Logan Date: Wed, 4 Sep 2024 08:04:38 -0300 Subject: [PATCH] test: add mathematics and remake cnpj generator test --- .../generators/Cnpj/__tests__/Cnpj.test.tsx | 99 ++++++++++++++ .../__tests__/LinearEquation.test.tsx | 57 ++++++++ .../mathematics/LinearEquation/index.tsx | 5 +- .../MmcMdc/__tests__/MmcMdc.test.tsx | 63 +++++++++ src/pages/mathematics/MmcMdc/index.tsx | 6 - .../__tests__/QuadraticEquation.test.tsx | 122 ++++++++++++++++++ .../mathematics/QuadraticEquation/index.tsx | 2 + 7 files changed, 346 insertions(+), 8 deletions(-) create mode 100644 src/pages/generators/Cnpj/__tests__/Cnpj.test.tsx create mode 100644 src/pages/mathematics/LinearEquation/__tests__/LinearEquation.test.tsx create mode 100644 src/pages/mathematics/MmcMdc/__tests__/MmcMdc.test.tsx create mode 100644 src/pages/mathematics/QuadraticEquation/__tests__/QuadraticEquation.test.tsx diff --git a/src/pages/generators/Cnpj/__tests__/Cnpj.test.tsx b/src/pages/generators/Cnpj/__tests__/Cnpj.test.tsx new file mode 100644 index 0000000..cff67d0 --- /dev/null +++ b/src/pages/generators/Cnpj/__tests__/Cnpj.test.tsx @@ -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(); + + const title = screen.getByText("Gerador de CNPJ"); + + expect(title).toBeDefined(); + }); + + it("should generate a random CNPJ without punctuation", async () => { + render(); + + 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(); + + 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(); + + 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(); + + 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(); + + const buttonCopy = screen.getByTestId("buttonCopy"); + + fireEvent.press(buttonCopy); + }); + + it("should load checkbox value from AsyncStorage", async () => { + (AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce("true"); + + render(); + + const checkbox = await screen.findByRole("checkbox"); + + expect(checkbox.props.accessibilityState.checked).toBe(true); + }); +}); diff --git a/src/pages/mathematics/LinearEquation/__tests__/LinearEquation.test.tsx b/src/pages/mathematics/LinearEquation/__tests__/LinearEquation.test.tsx new file mode 100644 index 0000000..831f500 --- /dev/null +++ b/src/pages/mathematics/LinearEquation/__tests__/LinearEquation.test.tsx @@ -0,0 +1,57 @@ +import { fireEvent, render, screen } from "@testing-library/react-native"; + +import LinearEquationPage from ".."; + +describe("Linear Equation", () => { + it("should render correctly", () => { + render(); + }); + + it("should calculate the correct result", async () => { + render(); + + 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(); + + 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(); + + 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(); + }); +}); diff --git a/src/pages/mathematics/LinearEquation/index.tsx b/src/pages/mathematics/LinearEquation/index.tsx index c9c74b3..a47e1b6 100644 --- a/src/pages/mathematics/LinearEquation/index.tsx +++ b/src/pages/mathematics/LinearEquation/index.tsx @@ -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")); diff --git a/src/pages/mathematics/MmcMdc/__tests__/MmcMdc.test.tsx b/src/pages/mathematics/MmcMdc/__tests__/MmcMdc.test.tsx new file mode 100644 index 0000000..156ee19 --- /dev/null +++ b/src/pages/mathematics/MmcMdc/__tests__/MmcMdc.test.tsx @@ -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(); + }); + + it("should calculate the correct result", async () => { + render(); + + 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(); + + 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(); + + const button = screen.getByText("Calcular"); + + fireEvent.press(button); + + expect(Alert.alert).toHaveBeenCalledWith( + "Erro", + "Digite valores numéricos separados por vírgulas" + ); + }); +}); diff --git a/src/pages/mathematics/MmcMdc/index.tsx b/src/pages/mathematics/MmcMdc/index.tsx index f2320d3..39058b4 100644 --- a/src/pages/mathematics/MmcMdc/index.tsx +++ b/src/pages/mathematics/MmcMdc/index.tsx @@ -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 = @@ -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]); diff --git a/src/pages/mathematics/QuadraticEquation/__tests__/QuadraticEquation.test.tsx b/src/pages/mathematics/QuadraticEquation/__tests__/QuadraticEquation.test.tsx new file mode 100644 index 0000000..457080a --- /dev/null +++ b/src/pages/mathematics/QuadraticEquation/__tests__/QuadraticEquation.test.tsx @@ -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(); + }); + + describe("calculateQuadraticEquation", () => { + it("should calculate the correct result", async () => { + render(); + + 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(); + + 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(); + + 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(""); + }); + }); +}); diff --git a/src/pages/mathematics/QuadraticEquation/index.tsx b/src/pages/mathematics/QuadraticEquation/index.tsx index 59b467c..968c919 100644 --- a/src/pages/mathematics/QuadraticEquation/index.tsx +++ b/src/pages/mathematics/QuadraticEquation/index.tsx @@ -151,6 +151,7 @@ export default function QuadraticEquationPage() { {t("Usar aproximação ?")} { await AsyncStorage.setItem( @@ -166,6 +167,7 @@ export default function QuadraticEquationPage() { {t("Limpar campos após gerar ?")} { await AsyncStorage.setItem(