Skip to content

Commit

Permalink
test: covering all pages and components
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Sep 5, 2024
1 parent b0a2154 commit 0cc796c
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"eslint": "^8",
"eslint-config-universe": "^13.0.0",
"eslint-plugin-testing-library": "^6.3.0",
"jest": "^29.6.3",
"jest": "^29.7.0",
"metro-react-native-babel-preset": "0.76.8",
"prettier": "2.8.8",
"react-native-svg-transformer": "^1.1.0",
Expand Down
7 changes: 6 additions & 1 deletion src/Routes/DataConverterNavigator/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Picker } from "@react-native-picker/picker";
import { useNavigation } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { t } from "i18next";
import type { Dispatch, SetStateAction } from "react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { View, StyleSheet } from "react-native";

import { useTheme } from "../../components/ThemeContext";
Expand All @@ -23,6 +23,8 @@ function DataConverterHeader({
selectedLanguage: RoutesStringsProps;
setSelectedLanguage: Dispatch<SetStateAction<RoutesStringsProps>>;
}) {
const { t } = useTranslation();

const { theme } = useTheme();

const styles = StyleSheet.create({
Expand All @@ -47,6 +49,7 @@ function DataConverterHeader({
return (
<View style={styles.container}>
<Picker
testID="picker"
selectedValue={selectedLanguage}
onValueChange={(itemValue) => {
navigation.navigate(itemValue);
Expand Down Expand Up @@ -79,6 +82,8 @@ function DataConverterHeader({
const Stack = createNativeStackNavigator();

export default function DataConverterNavigator() {
const { t } = useTranslation();

const [selectedLanguage, setSelectedLanguage] =
useState<RoutesStringsProps>("DataConverterMain");

Expand Down
20 changes: 19 additions & 1 deletion src/Routes/__tests__/DataConverterNavigator.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { NavigationContainer } from "@react-navigation/native";
import { fireEvent, render, screen } from "@testing-library/react-native";

import DataConverterNavigator from "../DataConverterNavigator";

describe("DataConverterNavigator", () => {
it("should render correctly", async () => {
expect(true).toBe(true);
render(
<NavigationContainer>
<DataConverterNavigator />
</NavigationContainer>
);

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

// Simulate changing the value of the Picker
fireEvent(picker, "onValueChange", "AsciiAndHex");

// Add your assertions here to check if the navigation and state update occurred
// For example, you can check if the navigation.navigate was called with the correct argument
// and if the selectedLanguage state was updated correctly. });
});
});
5 changes: 0 additions & 5 deletions src/Routes/__tests__/Routes.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { NavigationContainer } from "@react-navigation/native";
import { render } from "@testing-library/react-native";

import Routes from "..";

describe("Routes", () => {
it("should render correctly", async () => {
expect(true).toBe(true);
Expand Down
15 changes: 14 additions & 1 deletion src/components/Loading/__tests__/Loading.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { render, screen } from "@testing-library/react-native";

import Loading from "..";
import { useTheme } from "../../ThemeContext";

jest.mock("../../ThemeContext", () => ({
useTheme: jest.fn(),
}));

describe("Loading Component", () => {
it("should render correctly", () => {
(useTheme as jest.Mock).mockReturnValue({ theme: "light" });
render(<Loading />);
expect(screen.getByTestId("loading-indicator")).toBeTruthy();
});

expect(screen.getByTestId(/loading-indicator/)).toBeTruthy();
it("should render using dark theme", () => {
(useTheme as jest.Mock).mockReturnValue({ theme: "dark" });
render(<Loading />);
expect(screen.getByTestId("loading-indicator").props.style).toContainEqual({
backgroundColor: "black",
});
});
});
125 changes: 125 additions & 0 deletions src/pages/encodersAndDecoders/Base64/__tests__/Base64.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
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 base64 from "react-native-base64";

import Base64Page from "..";

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

afterEach(() => {
jest.resetAllMocks();
});
it("should render correctly", () => {
render(<Base64Page />);

Expand Down Expand Up @@ -89,6 +98,48 @@ describe("Base64", () => {
expect(textInputBase64.props.value).toBe("VGVzdGU=");
});

it("should encode the text considering space", async () => {
jest.spyOn(base64, "encode").mockImplementation(() => "dGVzdAo=");

render(<Base64Page />);

const textInput = await screen.findByTestId("inputText");

fireEvent.changeText(textInput, "test");

const checkbox = await screen.findByTestId("checkbox-considerSpace");

fireEvent(checkbox, "onValueChange", true);

const button = await screen.findByTestId("encode-buttonDispatch");

fireEvent.press(button);

const textInputBase64 = await screen.findByTestId("inputBase64");

expect(textInputBase64.props.value).toBe("dGVzdAo=");
});

it("should encode the text and clean the text input", async () => {
jest.spyOn(base64, "encode").mockImplementation(() => "VGVzdGU=");

render(<Base64Page />);

const textInput = screen.getByTestId("inputText");

const button = screen.getByTestId("encode-buttonDispatch");

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

fireEvent.changeText(textInput, "test");

fireEvent(checkbox, "onValueChange", true);

fireEvent.press(button);

expect(textInput.props.value).toBe("");
});

it("should paste the encoded text in the clipboard", async () => {
const clipboardSpyOn = jest
.spyOn(Clipboard, "getString")
Expand Down Expand Up @@ -169,6 +220,58 @@ describe("Base64", () => {
expect(textInputText.props.value).toBe("test");
});

it("should decode and clean the text input", async () => {
jest.spyOn(base64, "decode").mockImplementation(() => "test");

render(<Base64Page />);

const textInput = screen.getByTestId("inputBase64");

const button = screen.getByTestId("decode-buttonDispatch");

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

fireEvent.changeText(textInput, "VGVzdGU=");

fireEvent(checkbox, "onValueChange", true);

fireEvent.press(button);

expect(textInput.props.value).toBe("");
});

it("should Alert.alert if the text is not a valid base64", async () => {
jest.spyOn(base64, "decode").mockImplementation(() => {
throw new Error("Invalid base64");
});

render(<Base64Page />);

const textInput = await screen.findByTestId("inputBase64");

fireEvent.changeText(textInput, "VGVzdGU");

const button = await screen.findByTestId("decode-buttonDispatch");

fireEvent.press(button);

expect(AsyncStorage.setItem).not.toHaveBeenCalled();
});

it("should Alert.alert if the base64 input is empty string", async () => {
render(<Base64Page />);

const textInput = await screen.findByTestId("inputBase64");

fireEvent.changeText(textInput, "");

const button = await screen.findByTestId("decode-buttonDispatch");

fireEvent.press(button);

expect(AsyncStorage.setItem).not.toHaveBeenCalled();
});

it("should paste the decoded text in the clipboard", async () => {
const clipboardSpyOn = jest
.spyOn(Clipboard, "getString")
Expand Down Expand Up @@ -230,4 +333,26 @@ describe("Base64", () => {
});
});
});

describe("useEffect", () => {
it("should load base64AlwaysClean from AsyncStorage", async () => {
jest.spyOn(AsyncStorage, "getItem").mockResolvedValue("true");

render(<Base64Page />);

const checkbox = await screen.findByTestId("checkbox-cleanAfterGenerate");

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

it("should load base64ConsiderSpace from AsyncStorage", async () => {
jest.spyOn(AsyncStorage, "getItem").mockResolvedValue("true");

render(<Base64Page />);

const checkbox = await screen.findByTestId("checkbox-considerSpace");

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

import BinaryCodePage from "..";
import { useTheme } from "../../../../components/ThemeContext";

jest.mock("../../../../components/ThemeContext", () => ({
useTheme: jest.fn().mockReturnValue({ theme: "light" }),
}));

describe("BinaryCode", () => {
it("should render correctly", () => {
Expand All @@ -10,6 +15,22 @@ describe("BinaryCode", () => {
expect(text).toBeTruthy();
});

describe("text inputs", () => {
it("should render palceholder textColor dark if theme is dark", () => {
(useTheme as jest.Mock).mockReturnValue({ theme: "dark" });

render(<BinaryCodePage />);

const textInput1 = screen.getByPlaceholderText("Digite o texto aqui");
const textInput2 = screen.getByPlaceholderText(
"O código binário será exibido aqui"
);

expect(textInput1.props.placeholderTextColor).toBe("#ccc");
expect(textInput2.props.placeholderTextColor).toBe("#ccc");
});
});

describe("buttons", () => {
describe("TextToBinary", () => {
it("should copy the text code to the clipboard", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Clipboard from "@react-native-clipboard/clipboard";
import { fireEvent, render, screen } from "@testing-library/react-native";
import {
fireEvent,
render,
screen,
waitFor,
} from "@testing-library/react-native";
import { Alert } from "react-native";

import BinAndDecimal from "../BinAndDecimal";

Expand Down Expand Up @@ -98,6 +104,33 @@ describe("DataConverter", () => {
expect(decimalInput.props.value).toBe("989ACC"); // NOT CORRECT
});

it("should Alert.alert if decimalTextNumber is not a number", async () => {
jest.spyOn(Alert, "alert");

render(<BinAndDecimal />);

const binaryInput = screen.getByPlaceholderText(
"Cole ou digite o numero decimal aqui"
);

fireEvent.changeText(binaryInput, "teste");

expect(binaryInput.props.value).toBe("teste");

const button = screen.getByTestId("encode-buttonDispatch");

fireEvent.press(button);

expect(binaryInput.props.value).toBe("teste");

await waitFor(() => {
expect(Alert.alert).toHaveBeenCalledWith(
"Erro",
"Digite apenas numeros"
);
});
});

it("should not encode the binary to decimal", async () => {
render(<BinAndDecimal />);

Expand All @@ -121,7 +154,7 @@ describe("DataConverter", () => {
});
});

describe("HexToText", () => {
describe("decimalToBinary", () => {
it("should copy the decimal code to the clipboard", async () => {
render(<BinAndDecimal />);

Expand Down Expand Up @@ -198,6 +231,33 @@ describe("DataConverter", () => {
expect(binaryInput.props.value).toBe("70"); // NOT CORRECT
});

it("should Alert.alert if !/^[0-9A-Fa-f]+$/.test(cleanedBinary) match", async () => {
jest.spyOn(Alert, "alert");

render(<BinAndDecimal />);

const decimalInput = screen.getByPlaceholderText(
"Cole ou digite o código binario aqui"
);

fireEvent.changeText(decimalInput, "teste");

expect(decimalInput.props.value).toBe("teste");

const button = screen.getByText("Decodificar para decimal");

fireEvent.press(button);

expect(decimalInput.props.value).toBe("teste");

await waitFor(() => {
expect(Alert.alert).toHaveBeenCalledWith(
"Erro",
"O valor de entrada não é valido"
);
});
});

it("should not decode the decimal to binary", async () => {
render(<BinAndDecimal />);

Expand Down
Loading

0 comments on commit 0cc796c

Please sign in to comment.