-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add mathematics and remake cnpj generator test
- Loading branch information
1 parent
b547eef
commit 2307fe2
Showing
7 changed files
with
346 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
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); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
src/pages/mathematics/LinearEquation/__tests__/LinearEquation.test.tsx
This file contains 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
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(); | ||
}); | ||
}); |
This file contains 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
This file contains 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
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" | ||
); | ||
}); | ||
}); |
This file contains 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
122 changes: 122 additions & 0 deletions
122
src/pages/mathematics/QuadraticEquation/__tests__/QuadraticEquation.test.tsx
This file contains 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
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(""); | ||
}); | ||
}); | ||
}); |
This file contains 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