diff --git a/jest/setupFiles.js b/jest/setupFiles.js index 60cb858..be21625 100644 --- a/jest/setupFiles.js +++ b/jest/setupFiles.js @@ -1,6 +1,8 @@ // include this line for mocking react-native-gesture-handler import "react-native-gesture-handler/jestSetup"; +import mockClipboard from "@react-native-clipboard/clipboard/jest/clipboard-mock.js"; + // include this section and the NativeAnimatedHelper section for mocking react-native-reanimated jest.mock("react-native-reanimated", () => { const Reanimated = require("react-native-reanimated/mock"); @@ -60,3 +62,18 @@ jest.mock("react-native-responsive-screen", () => ({ width: jest.fn().mockImplementation(() => 10), height: jest.fn().mockImplementation(() => 10), })); + +jest.mock("@react-native-clipboard/clipboard", () => mockClipboard); + +jest.mock("react-native-base64", () => ({ + decode: jest.fn(), + encode: jest.fn(), +})); + +jest.mock("react-native-vector-icons/MaterialCommunityIcons", () => "Icon"); +jest.mock("react-native-vector-icons/FontAwesome5", () => "Icon"); +jest.mock("react-native-vector-icons/FontAwesome", () => "Icon"); +jest.mock("react-native-vector-icons/MaterialIcons", () => "Icon"); +jest.mock("react-native-vector-icons/Feather", () => "Icon"); +jest.mock("react-native-vector-icons/AntDesign", () => "Icon"); +jest.mock("react-native-vector-icons/Entypo", () => "Icon"); diff --git a/src/components/PrivacyPolicesAndTerms/__tests__/PrivacyPolicesAndTerms.test.tsx b/src/components/PrivacyPolicesAndTerms/__tests__/PrivacyPolicesAndTerms.test.tsx index ce039bf..53ec6dc 100644 --- a/src/components/PrivacyPolicesAndTerms/__tests__/PrivacyPolicesAndTerms.test.tsx +++ b/src/components/PrivacyPolicesAndTerms/__tests__/PrivacyPolicesAndTerms.test.tsx @@ -18,8 +18,6 @@ const mockNavigation = { isFocused: jest.fn(), } as unknown as NavigationType; -jest.mock("react-native-vector-icons/MaterialCommunityIcons", () => "Icon"); - describe("PrivacyPolicesAndTerms", () => { it("should render correctly", () => { render(); diff --git a/src/pages/encodersAndDecoders/Base64/__tests__/Base64.test.tsx b/src/pages/encodersAndDecoders/Base64/__tests__/Base64.test.tsx new file mode 100644 index 0000000..9c0c064 --- /dev/null +++ b/src/pages/encodersAndDecoders/Base64/__tests__/Base64.test.tsx @@ -0,0 +1,233 @@ +import Clipboard from "@react-native-clipboard/clipboard"; +import { fireEvent, render, screen } from "@testing-library/react-native"; +import base64 from "react-native-base64"; + +import Base64Page from ".."; + +describe("Base64", () => { + it("should render correctly", () => { + render(); + + const text = screen.getByText(/Apagar apos gerar ?/); + + expect(text).toBeTruthy(); + }); + + describe("Checkbox", () => { + it("should change the value of the checkbox cleanAfterGenerate", async () => { + render(); + + const checkbox = await screen.findByTestId("checkbox-cleanAfterGenerate"); + + expect(checkbox.props.accessibilityState.checked).toBe(false); + + fireEvent(checkbox, "onValueChange", true); + + expect(checkbox.props.accessibilityState.checked).toBe(true); + }); + + it("should change the value of the checkbox considerSpace", async () => { + render(); + + const checkbox = await screen.findByTestId("checkbox-considerSpace"); + + expect(checkbox.props.accessibilityState.checked).toBe(false); + + fireEvent(checkbox, "onValueChange", true); + + expect(checkbox.props.accessibilityState.checked).toBe(true); + }); + }); + + describe("TextInputs", () => { + describe("TextInput to encode", () => { + it("should change the value of the text input", async () => { + render(); + + const textInput = await screen.findByTestId("inputText"); + + expect(textInput.props.value).toBe(""); + + fireEvent.changeText(textInput, "test"); + + expect(textInput.props.value).toBe("test"); + }); + }); + + describe("TextInput to decode", () => { + it("should change the value of the text input", async () => { + render(); + + const textInput = await screen.findByTestId("inputBase64"); + + expect(textInput.props.value).toBe(""); + + fireEvent.changeText(textInput, "test"); + + expect(textInput.props.value).toBe("test"); + }); + }); + }); + + describe("Buttons", () => { + describe("Button to encode", () => { + it("should encode the text", async () => { + jest.spyOn(base64, "encode").mockImplementation(() => "VGVzdGU="); + + render(); + + const textInput = await screen.findByTestId("inputText"); + + fireEvent.changeText(textInput, "test"); + + const button = await screen.findByTestId("encode-buttonDispatch"); + + fireEvent.press(button); + + const textInputBase64 = await screen.findByTestId("inputBase64"); + + expect(textInputBase64.props.value).toBe("VGVzdGU="); + }); + + it("should paste the encoded text in the clipboard", async () => { + const clipboardSpyOn = jest + .spyOn(Clipboard, "getString") + .mockResolvedValue("Teste"); + + render(); + + const button = await screen.findByTestId("encode-buttonPaste"); + + fireEvent.press(button); + + const textInput = await screen.findByTestId("inputText"); + + expect(clipboardSpyOn).toHaveBeenCalled(); + + expect(textInput.props.value).toBe("Teste"); + }); + + it("should copy the encoded text to the clipboard", async () => { + render(); + + const textInput = await screen.findByTestId("inputText"); + + fireEvent.changeText(textInput, "Teste"); + + const button = await screen.findByTestId("encode-buttonCopy"); + + fireEvent.press(button); + + expect(textInput.props.value).toBe("Teste"); + }); + + it("should cut the encoded text to the clipboard", async () => { + render(); + + const textInput = await screen.findByTestId("inputText"); + + fireEvent.changeText(textInput, "Teste"); + + const button = await screen.findByTestId("encode-buttonCut"); + + fireEvent.press(button); + + expect(textInput.props.value).toBe(""); + }); + + it("should clean the encoded text to the clipboard", async () => { + render(); + + const textInput = await screen.findByTestId("inputText"); + + fireEvent.changeText(textInput, "Teste"); + + const button = await screen.findByTestId("encode-buttonClean"); + + fireEvent.press(button); + + expect(textInput.props.value).toBe(""); + }); + }); + + describe("Button to decode", () => { + it("should decode the text", async () => { + jest.spyOn(base64, "decode").mockImplementation(() => "test"); + + render(); + + const textInput = await screen.findByTestId("inputBase64"); + + fireEvent.changeText(textInput, "VGVzdGU="); + + const button = await screen.findByTestId("decode-buttonDispatch"); + + fireEvent.press(button); + + const textInputText = await screen.findByTestId("inputText"); + + expect(textInputText.props.value).toBe("test"); + }); + + it("should paste the decoded text in the clipboard", async () => { + const clipboardSpyOn = jest + .spyOn(Clipboard, "getString") + .mockResolvedValue("Teste"); + + render(); + + const button = await screen.findByTestId("decode-buttonPaste"); + + fireEvent.press(button); + + const textInput = await screen.findByTestId("inputBase64"); + + expect(clipboardSpyOn).toHaveBeenCalled(); + + expect(textInput.props.value).toBe("Teste"); + }); + + it("should copy the decoded text to the clipboard", async () => { + render(); + + const textInput = await screen.findByTestId("inputBase64"); + + fireEvent.changeText(textInput, "Teste"); + + const button = await screen.findByTestId("decode-buttonCopy"); + + fireEvent.press(button); + + expect(textInput.props.value).toBe("Teste"); + }); + + it("should cut the decoded text to the clipboard", async () => { + render(); + + const textInput = await screen.findByTestId("inputBase64"); + + fireEvent.changeText(textInput, "Teste"); + + const button = await screen.findByTestId("decode-buttonCut"); + + fireEvent.press(button); + + expect(textInput.props.value).toBe(""); + }); + + it("should clean the decoded text to the clipboard", async () => { + render(); + + const textInput = await screen.findByTestId("inputBase64"); + + fireEvent.changeText(textInput, "Teste"); + + const button = await screen.findByTestId("decode-buttonClean"); + + fireEvent.press(button); + + expect(textInput.props.value).toBe(""); + }); + }); + }); +}); diff --git a/src/pages/encodersAndDecoders/Base64/index.tsx b/src/pages/encodersAndDecoders/Base64/index.tsx index 0746b83..f93492c 100644 --- a/src/pages/encodersAndDecoders/Base64/index.tsx +++ b/src/pages/encodersAndDecoders/Base64/index.tsx @@ -125,6 +125,7 @@ export default function Base64Page() { {t("Apagar apos gerar ?")} { @@ -141,6 +142,7 @@ export default function Base64Page() { {t("Considerar espaço ?")} { @@ -155,6 +157,7 @@ export default function Base64Page() { pasteToClipboard("text")} > copyToClipboard(inputText)} > cutToClipboard(inputText, "text")} > cleanToClipboard("text")} > @@ -190,6 +197,7 @@ export default function Base64Page() { @@ -199,6 +207,7 @@ export default function Base64Page() { pasteToClipboard("base64")} > copyToClipboard(base64Text)} > cutToClipboard(base64Text, "base64")} > cleanToClipboard("base64")} > @@ -234,6 +247,7 @@ export default function Base64Page() { diff --git a/src/pages/encodersAndDecoders/CodigoBinario/__tests__/CodigoBinario.test.tsx b/src/pages/encodersAndDecoders/CodigoBinario/__tests__/CodigoBinario.test.tsx new file mode 100644 index 0000000..e0e533f --- /dev/null +++ b/src/pages/encodersAndDecoders/CodigoBinario/__tests__/CodigoBinario.test.tsx @@ -0,0 +1,114 @@ +import { fireEvent, render, screen } from "@testing-library/react-native"; + +import BinaryCodePage from ".."; + +describe("BinaryCode", () => { + it("should render correctly", () => { + render(); + const text = screen.getByText(/Codificar para Binário/); + + expect(text).toBeTruthy(); + }); + + describe("buttons", () => { + describe("TextToBinary", () => { + it("should copy the text code to the clipboard", async () => { + render(); + + const button = screen.getByTestId("encode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the text code", async () => { + render(); + + const button = screen.getByTestId("encode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should encode the text to binary", async () => { + render(); + + const textInput = screen.getByPlaceholderText("Digite o texto aqui"); + + fireEvent.changeText(textInput, "Teste"); + + expect(textInput.props.value).toBe("Teste"); + + const button = screen.getByText("Codificar para Binário"); + + fireEvent.press(button); + + const binaryInput = screen.getByPlaceholderText( + "O código binário será exibido aqui" + ); + + expect(binaryInput.props.value).toBe( + "01010100 01100101 01110011 01110100 01100101 " + ); + }); + }); + + describe("BinaryToText", () => { + it("should copy the binary code to the clipboard", async () => { + render(); + + const binaryInput = screen.getByPlaceholderText( + "O código binário será exibido aqui" + ); + + fireEvent.changeText( + binaryInput, + "01010100 01100101 01110011 01110100 01100101" + ); + + const button = screen.getByTestId("decode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the binary code", async () => { + render(); + + const button = screen.getByTestId("decode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should decode the binary to text", async () => { + render(); + + const binaryInput = screen.getByPlaceholderText( + "O código binário será exibido aqui" + ); + + fireEvent.changeText( + binaryInput, + "01010100 01100101 01110011 01110100 01100101" + ); + + expect(binaryInput.props.value).toBe( + "01010100 01100101 01110011 01110100 01100101" + ); + + const button = screen.getByText("Decodificar para Texto"); + + fireEvent.press(button); + + const textInput = screen.getByPlaceholderText("Digite o texto aqui"); + + expect(textInput.props.value).toBe("Teste"); + }); + }); + }); +}); diff --git a/src/pages/encodersAndDecoders/CodigoBinario/index.tsx b/src/pages/encodersAndDecoders/CodigoBinario/index.tsx index 9b3811f..795881c 100644 --- a/src/pages/encodersAndDecoders/CodigoBinario/index.tsx +++ b/src/pages/encodersAndDecoders/CodigoBinario/index.tsx @@ -79,14 +79,16 @@ export default function BinaryCodePage() { /> copyToClipboard(binaryCode)} > cleanToClipboard("Binario")} + onPress={() => cleanToClipboard("text")} > @@ -105,12 +107,14 @@ export default function BinaryCodePage() { /> copyToClipboard(binaryCode)} > cleanToClipboard("Binario")} > diff --git a/src/pages/encodersAndDecoders/DataConverter/AsciiAndHex/index.tsx b/src/pages/encodersAndDecoders/DataConverter/AsciiAndHex/index.tsx index 95f7734..55fdee5 100644 --- a/src/pages/encodersAndDecoders/DataConverter/AsciiAndHex/index.tsx +++ b/src/pages/encodersAndDecoders/DataConverter/AsciiAndHex/index.tsx @@ -102,24 +102,28 @@ export default function AsciiAndHexPage() { /> pasteToClipboard("text")} > copyToClipboard(asciiText)} > cutToClipboard(asciiText, "text")} > cleanToClipboard("text")} > @@ -127,7 +131,11 @@ export default function AsciiAndHexPage() { - + {t("Codificar para Hex")} @@ -144,24 +152,28 @@ export default function AsciiAndHexPage() { /> pasteToClipboard("hex")} > copyToClipboard(hexText)} > cutToClipboard(hexText, "hex")} > cleanToClipboard("hex")} > @@ -170,6 +182,7 @@ export default function AsciiAndHexPage() { diff --git a/src/pages/encodersAndDecoders/DataConverter/BinAndDecimal/index.tsx b/src/pages/encodersAndDecoders/DataConverter/BinAndDecimal/index.tsx index be729e5..d31a740 100644 --- a/src/pages/encodersAndDecoders/DataConverter/BinAndDecimal/index.tsx +++ b/src/pages/encodersAndDecoders/DataConverter/BinAndDecimal/index.tsx @@ -95,24 +95,28 @@ export default function BinAndDecimal() { /> pasteToClipboard("decimal")} > copyToClipboard(decimalText)} > cutToClipboard(decimalText, "decimal")} > cleanToClipboard("decimal")} > @@ -121,6 +125,7 @@ export default function BinAndDecimal() { @@ -140,24 +145,28 @@ export default function BinAndDecimal() { /> pasteToClipboard("binary")} > copyToClipboard(binaryText)} > cutToClipboard(binaryText, "binary")} > cleanToClipboard("binary")} > @@ -166,6 +175,7 @@ export default function BinAndDecimal() { diff --git a/src/pages/encodersAndDecoders/DataConverter/HexAndDecimal/index.tsx b/src/pages/encodersAndDecoders/DataConverter/HexAndDecimal/index.tsx index 8a9333c..c412a49 100644 --- a/src/pages/encodersAndDecoders/DataConverter/HexAndDecimal/index.tsx +++ b/src/pages/encodersAndDecoders/DataConverter/HexAndDecimal/index.tsx @@ -95,24 +95,28 @@ export default function HexAndDecimal() { /> pasteToClipboard("decimal")} > copyToClipboard(decimalText)} > cutToClipboard(decimalText, "decimal")} > cleanToClipboard("decimal")} > @@ -137,24 +141,28 @@ export default function HexAndDecimal() { /> pasteToClipboard("hex")} > copyToClipboard(hexText)} > cutToClipboard(hexText, "hex")} > cleanToClipboard("hex")} > diff --git a/src/pages/encodersAndDecoders/DataConverter/__tests__/AsciiAndHex.test.tsx b/src/pages/encodersAndDecoders/DataConverter/__tests__/AsciiAndHex.test.tsx new file mode 100644 index 0000000..f735174 --- /dev/null +++ b/src/pages/encodersAndDecoders/DataConverter/__tests__/AsciiAndHex.test.tsx @@ -0,0 +1,226 @@ +import Clipboard from "@react-native-clipboard/clipboard"; +import { fireEvent, render, screen } from "@testing-library/react-native"; + +import AsciiAndHexPage from "../AsciiAndHex"; + +describe("DataConverter", () => { + it("should render correctly", () => { + render(); + + const text = screen.getByText(/Codificar para Hex/i); + + expect(text).toBeTruthy(); + }); + + describe("buttons", () => { + describe("TextToHex", () => { + it("should copy the text code to the clipboard", async () => { + render(); + + const button = screen.getByTestId("encode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the text code", async () => { + render(); + + const button = screen.getByTestId("encode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should cut the text code to the clipboard", async () => { + const spyClipboard = jest + .spyOn(Clipboard, "setString") + .mockImplementation(() => "teste"); + + render(); + + const inputText = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + fireEvent.changeText(inputText, "Teste"); + + expect(inputText.props.value).toBe("Teste"); + + const button = screen.getByTestId("encode-buttonCut"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + + expect(spyClipboard).toHaveBeenCalled(); + + expect(inputText.props.value).toBe(""); + }); + + it("should paste the text code to the clipboard", async () => { + jest.spyOn(Clipboard, "getString").mockResolvedValue("Teste"); + + render(); + + const button = await screen.findByTestId("encode-buttonPaste"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const textInput = await screen.findByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + expect(textInput.props.value).toBe("Teste"); + }); + + it("should encode the text to hex", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + fireEvent.changeText(textInput, "Teste"); + + expect(textInput.props.value).toBe("Teste"); + + const button = screen.getByTestId("encode-buttonDispatch"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe("54 65 73 74 65"); + }); + + it("should not encode the text to hex", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + expect(textInput.props.value).toBe(""); + + const button = screen.getByTestId("encode-buttonDispatch"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe(""); + }); + }); + + describe("HexToText", () => { + it("should copy the hex code to the clipboard", async () => { + render(); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + fireEvent.changeText(hexInput, "54 65 73 74 65"); + + const button = screen.getByTestId("decode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the hex code", async () => { + render(); + + const button = screen.getByTestId("decode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should cut the hex code to the clipboard", async () => { + render(); + + const button = screen.getByTestId("decode-buttonCut"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should paste the hex code to the clipboard", async () => { + jest.spyOn(Clipboard, "getString").mockResolvedValue("54 65 73 74 65"); + + render(); + + const button = await screen.findByTestId("decode-buttonPaste"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const hexInput = await screen.findByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe("54 65 73 74 65"); + }); + + it("should decode the hex to text", async () => { + render(); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + fireEvent.changeText(hexInput, "54 65 73 74 65"); + + expect(hexInput.props.value).toBe("54 65 73 74 65"); + + const button = screen.getByText("Decodificar para Texto"); + + fireEvent.press(button); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + expect(textInput.props.value).toBe("Teste"); + }); + + it("should not decode the hex to text", async () => { + render(); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe(""); + + const button = screen.getByText("Decodificar para Texto"); + + fireEvent.press(button); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + expect(textInput.props.value).toBe(""); + }); + }); + }); +}); diff --git a/src/pages/encodersAndDecoders/DataConverter/__tests__/BinAndDecimal.test.tsx b/src/pages/encodersAndDecoders/DataConverter/__tests__/BinAndDecimal.test.tsx new file mode 100644 index 0000000..22840cf --- /dev/null +++ b/src/pages/encodersAndDecoders/DataConverter/__tests__/BinAndDecimal.test.tsx @@ -0,0 +1,222 @@ +import Clipboard from "@react-native-clipboard/clipboard"; +import { fireEvent, render, screen } from "@testing-library/react-native"; + +import BinAndDecimal from "../BinAndDecimal"; + +describe("DataConverter", () => { + it("should render correctly", () => { + render(); + }); + + describe("buttons", () => { + describe("BinToDecimal", () => { + it("should copy the binary code to the clipboard", async () => { + render(); + + const button = screen.getByTestId("encode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the binary code", async () => { + render(); + + const button = screen.getByTestId("encode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should cut the binary code to the clipboard", async () => { + const spyClipboard = jest + .spyOn(Clipboard, "setString") + .mockImplementation(() => "teste"); + + render(); + + const inputText = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + fireEvent.changeText(inputText, "Teste"); + + expect(inputText.props.value).toBe("Teste"); + + const button = screen.getByTestId("encode-buttonCut"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + + expect(spyClipboard).toHaveBeenCalled(); + + expect(inputText.props.value).toBe(""); + }); + + it("should paste the binary code to the clipboard", async () => { + jest.spyOn(Clipboard, "getString").mockResolvedValue("Teste"); + + render(); + + const button = await screen.findByTestId("encode-buttonPaste"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const binaryInput = await screen.findByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(binaryInput.props.value).toBe("Teste"); + }); + + it("should encode the binary to decimal", async () => { + render(); + + const binaryInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + fireEvent.changeText(binaryInput, "010001100"); + + expect(binaryInput.props.value).toBe("010001100"); + + const button = screen.getByTestId("encode-buttonDispatch"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const decimalInput = screen.getByPlaceholderText( + "Cole ou digite o código binario aqui" + ); + + expect(decimalInput.props.value).toBe("989ACC"); // NOT CORRECT + }); + + it("should not encode the binary to decimal", async () => { + render(); + + const binaryInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(binaryInput.props.value).toBe(""); + + const button = screen.getByTestId("encode-buttonDispatch"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const decimalInput = screen.getByPlaceholderText( + "Cole ou digite o código binario aqui" + ); + + expect(decimalInput.props.value).toBe(""); + }); + }); + + describe("HexToText", () => { + it("should copy the decimal code to the clipboard", async () => { + render(); + + const decimalInput = screen.getByPlaceholderText( + "Cole ou digite o código binario aqui" + ); + + fireEvent.changeText(decimalInput, "46"); + + const button = screen.getByTestId("decode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the decimal code", async () => { + render(); + + const button = screen.getByTestId("decode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should cut the decimal code to the clipboard", async () => { + render(); + + const button = screen.getByTestId("decode-buttonCut"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should paste the decimal code to the clipboard", async () => { + jest.spyOn(Clipboard, "getString").mockResolvedValue("54 65 73 74 65"); + + render(); + + const button = await screen.findByTestId("decode-buttonPaste"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const decimalInput = await screen.findByPlaceholderText( + "Cole ou digite o código binario aqui" + ); + + expect(decimalInput.props.value).toBe("54 65 73 74 65"); + }); + + it("should decode the decimal to binary", async () => { + render(); + + const decimalInput = screen.getByPlaceholderText( + "Cole ou digite o código binario aqui" + ); + + fireEvent.changeText(decimalInput, "46"); + + expect(decimalInput.props.value).toBe("46"); + + const button = screen.getByText("Decodificar para decimal"); + + fireEvent.press(button); + + const binaryInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(binaryInput.props.value).toBe("70"); // NOT CORRECT + }); + + it("should not decode the decimal to binary", async () => { + render(); + + const decimalInput = screen.getByPlaceholderText( + "Cole ou digite o código binario aqui" + ); + + expect(decimalInput.props.value).toBe(""); + + const button = screen.getByText("Decodificar para decimal"); + + fireEvent.press(button); + + const binaryInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(binaryInput.props.value).toBe(""); + }); + }); + }); +}); diff --git a/src/pages/encodersAndDecoders/DataConverter/__tests__/DataConverter.test.tsx b/src/pages/encodersAndDecoders/DataConverter/__tests__/DataConverter.test.tsx new file mode 100644 index 0000000..f84d4c6 --- /dev/null +++ b/src/pages/encodersAndDecoders/DataConverter/__tests__/DataConverter.test.tsx @@ -0,0 +1,13 @@ +import { render, screen } from "@testing-library/react-native"; + +import DataConverterPage from ".."; + +describe("DataConverter", () => { + it("should render correctly", () => { + render(); + + const text = screen.getByText(/Pagina para conversoes/i); + + expect(text).toBeTruthy(); + }); +}); diff --git a/src/pages/encodersAndDecoders/DataConverter/__tests__/HexAndDecimal.test.tsx b/src/pages/encodersAndDecoders/DataConverter/__tests__/HexAndDecimal.test.tsx new file mode 100644 index 0000000..5fbe399 --- /dev/null +++ b/src/pages/encodersAndDecoders/DataConverter/__tests__/HexAndDecimal.test.tsx @@ -0,0 +1,288 @@ +import Clipboard from "@react-native-clipboard/clipboard"; +import { fireEvent, render, screen } from "@testing-library/react-native"; +import { Alert } from "react-native"; + +import HexAndDecimal from "../HexAndDecimal"; + +describe("DataConverter", () => { + it("should render correctly", () => { + render(); + + const decimal = screen.getByText(/Codificar para Hex/i); + + expect(decimal).toBeTruthy(); + }); + + describe("buttons", () => { + describe("TextToHex", () => { + it("should copy the decimal code to the clipboard", async () => { + render(); + + const button = screen.getByTestId("encode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the decimal code", async () => { + render(); + + const button = screen.getByTestId("encode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should cut the decimal code to the clipboard", async () => { + const spyClipboard = jest + .spyOn(Clipboard, "setString") + .mockImplementation(() => "teste"); + + render(); + + const inputText = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + fireEvent.changeText(inputText, "Teste"); + + expect(inputText.props.value).toBe("Teste"); + + const button = screen.getByTestId("encode-buttonCut"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + + expect(spyClipboard).toHaveBeenCalled(); + + expect(inputText.props.value).toBe(""); + }); + + it("should paste the decimal code to the clipboard", async () => { + jest.spyOn(Clipboard, "getString").mockResolvedValue("Teste"); + + render(); + + const button = await screen.findByTestId("encode-buttonPaste"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const textInput = await screen.findByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(textInput.props.value).toBe("Teste"); + }); + + it("should encode the decimal to hex", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + fireEvent.changeText(textInput, "123"); + + expect(textInput.props.value).toBe("123"); + + const button = screen.getByText("Codificar para Hex"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe("7B"); + }); + + it("should not encode the decimal to hex", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(textInput.props.value).toBe(""); + + const button = screen.getByText("Codificar para Hex"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe(""); + }); + + it("should not encode and Alert.alert when decimalTextNumber is not a number", async () => { + const spyAlert = jest.spyOn(Alert, "alert"); + + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + fireEvent.changeText(textInput, "Teste"); + + expect(textInput.props.value).toBe("Teste"); + + const button = screen.getByText("Codificar para Hex"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe(""); + + expect(spyAlert).toHaveBeenCalled(); + + expect(spyAlert).toHaveBeenCalledWith("Erro", "Digite apenas numeros"); + }); + }); + + describe("HexToText", () => { + it("should copy the hex code to the clipboard", async () => { + render(); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + fireEvent.changeText(hexInput, "54 65 73 74 65"); + + const button = screen.getByTestId("decode-buttonCopy"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should clean the hex code", async () => { + render(); + + const button = screen.getByTestId("decode-buttonClean"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should cut the hex code to the clipboard", async () => { + render(); + + const button = screen.getByTestId("decode-buttonCut"); + + fireEvent.press(button); + + expect(button).toBeTruthy(); + }); + + it("should paste the hex code to the clipboard", async () => { + jest.spyOn(Clipboard, "getString").mockResolvedValue("54 65 73 74 65"); + + render(); + + const button = await screen.findByTestId("decode-buttonPaste"); + + expect(button).toBeTruthy(); + + fireEvent.press(button); + + const hexInput = await screen.findByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe("54 65 73 74 65"); + }); + + it("should decode the hex to decimal", async () => { + render(); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + fireEvent.changeText(hexInput, "7B"); + + expect(hexInput.props.value).toBe("7B"); + + const button = screen.getByText("Decodificar para decimal"); + + fireEvent.press(button); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(textInput.props.value).toBe("123"); + }); + + it("should not decode the hex to decimal", async () => { + render(); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + expect(hexInput.props.value).toBe(""); + + const button = screen.getByText("Decodificar para decimal"); + + fireEvent.press(button); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(textInput.props.value).toBe(""); + }); + + it("should not decode and Alert.alert when !/^[0-9A-Fa-f]+$/.test(cleanedHex) is true", async () => { + const spyAlert = jest.spyOn(Alert, "alert"); + + render(); + + const hexInput = screen.getByPlaceholderText( + "Cole ou digite o código Hex aqui" + ); + + fireEvent.changeText(hexInput, "7B??7B"); + + expect(hexInput.props.value).toBe("7B??7B"); + + const button = screen.getByText("Decodificar para decimal"); + + fireEvent.press(button); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o numero decimal aqui" + ); + + expect(textInput.props.value).toBe(""); + + expect(spyAlert).toHaveBeenCalled(); + + expect(spyAlert).toHaveBeenCalledWith( + "Erro", + "O valor de entrada não é valido" + ); + }); + }); + }); +}); diff --git a/src/pages/encodersAndDecoders/DataConverter/index.tsx b/src/pages/encodersAndDecoders/DataConverter/index.tsx index 28eb016..8a7ad54 100644 --- a/src/pages/encodersAndDecoders/DataConverter/index.tsx +++ b/src/pages/encodersAndDecoders/DataConverter/index.tsx @@ -1,4 +1,4 @@ -import { t } from "i18next"; +import { useTranslation } from "react-i18next"; import { View, Text, StyleSheet } from "react-native"; import { useTheme } from "../../../components/ThemeContext"; @@ -8,6 +8,7 @@ import type { Theme } from "../../../types/themeProps"; export default function DataConverterPage() { const { theme } = useTheme(); const stylesWithTheme = styles(theme); + const { t } = useTranslation(); return ( {t("Pagina para conversoes")} diff --git a/src/pages/encodersAndDecoders/Md5/__tests__/Md5.test.tsx b/src/pages/encodersAndDecoders/Md5/__tests__/Md5.test.tsx new file mode 100644 index 0000000..b158f01 --- /dev/null +++ b/src/pages/encodersAndDecoders/Md5/__tests__/Md5.test.tsx @@ -0,0 +1,186 @@ +import AsyncStorage from "@react-native-async-storage/async-storage"; +import Clipboard from "@react-native-clipboard/clipboard"; +import { fireEvent, render, screen } from "@testing-library/react-native"; + +import Md5Page from ".."; + +describe("Md5", () => { + beforeEach(() => { + jest.clearAllMocks(); + AsyncStorage.clear(); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it("should render correctly", () => { + render(); + + const md5Text = screen.getByText(/MD5 HASH:/i); + + expect(md5Text).toBeTruthy(); + }); + + it("should change checkbox value", () => { + render(); + + const checkbox = screen.getByRole("checkbox"); + + expect(checkbox).toBeTruthy(); + + expect(checkbox.props.accessibilityState.checked).toBe(false); + + fireEvent(checkbox, "onValueChange", true); + + expect(checkbox.props.accessibilityState.checked).toBe(true); + + fireEvent(checkbox, "onValueChange", false); + + expect(checkbox.props.accessibilityState.checked).toBe(false); + }); + + it("should change the value of the text input", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + expect(textInput).toBeTruthy(); + + expect(textInput.props.value).toBe(""); + + fireEvent.changeText(textInput, "test"); + + expect(textInput.props.value).toBe("test"); + }); + + it("should copy the md5 hash to the clipboard", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + fireEvent.changeText(textInput, "test"); + + const button = await screen.findByText(/Codificar para Md5/i); + + fireEvent.press(button); + + const copyButton = await screen.findByTestId("buttonCopy"); + + fireEvent.press(copyButton); + }); + + it("should cut the md5 hash to the clipboard", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + fireEvent.changeText(textInput, "test"); + + const button = await screen.findByText(/Codificar para Md5/i); + + fireEvent.press(button); + + const cutButton = await screen.findByTestId("buttonCut"); + + fireEvent.press(cutButton); + }); + + it("should clean the md5 hash to the clipboard", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + fireEvent.changeText(textInput, "test"); + + const button = await screen.findByText(/Codificar para Md5/i); + + fireEvent.press(button); + + const cleanButton = await screen.findByTestId("buttonClean"); + + fireEvent.press(cleanButton); + }); + + it("should paste the clipboard value to the text input", async () => { + jest.spyOn(Clipboard, "getString").mockResolvedValue("test"); + + render(); + + const pasteButton = await screen.findByTestId("buttonPaste"); + + fireEvent.press(pasteButton); + + const textInput = await screen.findByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + expect(textInput.props.value).toBe("test"); + + jest.clearAllMocks(); + }); + + it("should encode the text to md5", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + fireEvent.changeText(textInput, "test"); + + const button = await screen.findByText(/Codificar para Md5/i); + + fireEvent.press(button); + + const md5Text = await screen.findByText( + /098f6bcd4621d373cade4e832627b4f6/i + ); + + expect(md5Text).toBeTruthy(); + }); + + it("should clean the text input always after generate", async () => { + render(); + + const textInput = screen.getByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + fireEvent.changeText(textInput, "test"); + + const checkbox = screen.getByRole("checkbox"); + + fireEvent(checkbox, "onValueChange", true); + + const button = await screen.findByText(/Codificar para Md5/i); + + fireEvent.press(button); + + const newTextInput = await screen.findByPlaceholderText( + "Cole ou digite o texto aqui" + ); + + expect(newTextInput.props.value).toBe(""); + }); + + it("should set the value of the checkbox from AsyncStorage", async () => { + jest.spyOn(AsyncStorage, "getItem").mockResolvedValue("true"); + + render(); + + const checkbox = await screen.findByRole("checkbox"); + + expect(checkbox.props.accessibilityState.checked).toBe(true); + + jest.clearAllMocks(); + }); +}); diff --git a/src/pages/encodersAndDecoders/Md5/index.tsx b/src/pages/encodersAndDecoders/Md5/index.tsx index a4af5a3..6f7b3c5 100644 --- a/src/pages/encodersAndDecoders/Md5/index.tsx +++ b/src/pages/encodersAndDecoders/Md5/index.tsx @@ -109,24 +109,28 @@ export default function Md5Page() { pasteToClipboard()} > copyToClipboard()} > cutToClipboard()} > cleanToClipboard()} > diff --git a/src/utils/translations/i18n.ts b/src/utils/translations/i18n.ts index 28011cd..144db02 100644 --- a/src/utils/translations/i18n.ts +++ b/src/utils/translations/i18n.ts @@ -57,9 +57,15 @@ import zhHant from "./zh-Hant/zh-Hant.json"; // Chinês Tradicional // (tip move them in a JSON file and import them, // or even better, manage them separated from your code: https://react.i18next.com/guides/multiple-translation-files) -const languageWithoutHyphen = getLocales()[0].languageTag.replace("-", ""); +const locales = getLocales(); -const { languageCode } = getLocales()[0]; +if (!Array.isArray(locales) || locales.length === 0) { + throw new Error("No locales found"); +} + +const languageWithoutHyphen = locales[0].languageTag.replace("-", ""); + +const { languageCode } = locales[0]; const resources = { af,