Skip to content

Commit

Permalink
test: all components
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Sep 4, 2024
1 parent 5363e27 commit f0a423c
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 9 deletions.
2 changes: 2 additions & 0 deletions __mocks__/react-native-localize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// __mocks__/react-native-localize.ts
export * from "react-native-localize/mock"; // or "react-native-localize/mock/jest"
14 changes: 7 additions & 7 deletions jest/setupFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ jest.mock("react-i18next", () => ({
}));

jest.mock("react-native-responsive-fontsize", () => ({
RFValue: jest.fn(),
RFPercentage: jest.fn(),
fixedRFValue: jest.fn(),
RFValue: jest.fn().mockImplementation(() => 10),
RFPercentage: jest.fn().mockImplementation(() => 10),
fixedRFValue: jest.fn().mockImplementation(() => 10),
}));

jest.mock("react-native-responsive-screen", () => ({
widthPercentageToDP: jest.fn(),
heightPercentageToDP: jest.fn(),
width: jest.fn(),
height: jest.fn(),
widthPercentageToDP: jest.fn().mockImplementation(() => 10),
heightPercentageToDP: jest.fn().mockImplementation(() => 10),
width: jest.fn().mockImplementation(() => 10),
height: jest.fn().mockImplementation(() => 10),
}));
53 changes: 52 additions & 1 deletion src/components/DrawerContent/__tests__/DrawerContent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
import { render } from "@testing-library/react-native";
import { fireEvent, render, screen } from "@testing-library/react-native";

import CustomDrawerContent from "..";

describe("DrawerContent", () => {
it("should render correctly", () => {
render(<CustomDrawerContent />);

expect(screen.getByText(/Principal/i)).toBeTruthy();
});

describe("Main menu", () => {
it("should navigate to the correct screen", async () => {
render(<CustomDrawerContent />);

const drawerItem = await screen.findAllByTestId("drawerItem");

drawerItem.forEach((item) => {
fireEvent.press(item);
});

expect(screen.getByText(/Principal/i)).toBeTruthy();
});

it("should open and close the sub menu", async () => {
render(<CustomDrawerContent />);

const drawerItem = await screen.findAllByTestId("drawerItem");

fireEvent.press(drawerItem[2]);

const drawerItemSub = await screen.findAllByTestId("drawerItemSub");

expect(drawerItemSub).toHaveLength(4);

fireEvent.press(drawerItem[2]);

expect(screen.queryByTestId("drawerItemSub")).toBeNull();
});
});

describe("Sub menu", () => {
it("should navigate to the correct screen", async () => {
render(<CustomDrawerContent />);

const drawerItem = await screen.findAllByTestId("drawerItem");

fireEvent.press(drawerItem[2]);
fireEvent.press(drawerItem[3]);

const drawerItemSub = await screen.findAllByTestId("drawerItemSub");

drawerItemSub.forEach((item) => {
fireEvent.press(item);
});

expect(screen.getByText(/Principal/i)).toBeTruthy();
});
});
});
2 changes: 2 additions & 0 deletions src/components/DrawerContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const CustomDrawerContent = () => {
{drawerMenu.map((item, index) => (
<View key={index} style={stylesWithTheme.menuItem}>
<TouchableOpacity
testID="drawerItem"
onPress={() => {
LayoutAnimation.configureNext(
LayoutAnimation.create(200, "easeInEaseOut", "opacity")
Expand All @@ -59,6 +60,7 @@ const CustomDrawerContent = () => {
<>
{item.menuList?.map((subItem, subIndex) => (
<TouchableOpacity
testID="drawerItemSub"
key={subIndex}
onPress={() => navigation.navigate(subItem.route)}
>
Expand Down
15 changes: 15 additions & 0 deletions src/components/LogoComponent/__tests__/LogoComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { render } from "@testing-library/react-native";

import LogoComponent from "..";

describe("LogoComponent", () => {
it("should render correctly", () => {
render(
<LogoComponent
width={20}
height={20}
style={{ backgroundColor: "black" }}
/>
);
});
});
4 changes: 3 additions & 1 deletion src/components/LogoComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ interface LogoComponentProps {
const LogoComponent = ({ style, width, height }: LogoComponentProps) => {
const { theme } = useTheme();
const Logo = theme === "light" ? Marca : MarcaLight;
return <Logo style={style} width={width} height={height} />;
return (
<Logo testID="LogoComponent" style={style} width={width} height={height} />
);
};

export default LogoComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import {
fireEvent,
render,
screen,
waitFor,
} from "@testing-library/react-native";
import { Alert } from "react-native";

import PrivacyPolicesAndTerms from "..";
import type { NavigationType } from "../../../types/navigationProps";

const mockNavigation = {
navigate: jest.fn(),
dispatch: jest.fn(),
reset: jest.fn(),
goBack: jest.fn(),
isFocused: jest.fn(),
} as unknown as NavigationType;

jest.mock("react-native-vector-icons/MaterialCommunityIcons", () => "Icon");

describe("PrivacyPolicesAndTerms", () => {
it("should render correctly", () => {
render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);
});

describe("open links", () => {
it("should open the terms link", () => {
render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);

const button = screen.getByText("Ler termos de uso");

fireEvent.press(button);
});

it("should open the privacy link", () => {
render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);

const button = screen.getByText("Ler politicas de privacidade");

fireEvent.press(button);
});
});

describe("checkbox", () => {
it("should apear activeSpan if checkbox is not checked", async () => {
render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);

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

fireEvent.press(button);

const textActiveSpan = await screen.findByText(
"Para continuar você precisa aceitar os termos"
);

expect(textActiveSpan).toBeTruthy();
});

it("should not apear activeSpan if checkbox is checked", async () => {
render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);

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

fireEvent.press(checkbox);

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

fireEvent.press(button);

const textActiveSpan = screen.queryByText(
"Para continuar você precisa aceitar os termos"
);

await waitFor(() => {
expect(textActiveSpan).toBeNull();
});
});

it("should check the checkbox", () => {
render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);

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

fireEvent.press(checkbox);
});
});

describe("error handling", () => {
it("should show alert on AsyncStorage.setItem error", async () => {
jest.spyOn(Alert, "alert");

(AsyncStorage.setItem as jest.Mock).mockRejectedValueOnce(
new Error("AsyncStorage error")
);

render(<PrivacyPolicesAndTerms navigation={mockNavigation} />);

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

fireEvent.press(checkbox);

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

fireEvent.press(button);

await waitFor(() => {
expect(Alert.alert).toHaveBeenCalledWith(
"Alguma coisa errada aconteceu, contate o desenvolvedor"
);
});
});
});
});
1 change: 1 addition & 0 deletions src/components/PrivacyPolicesAndTerms/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default function PrivacyPolicesAndTerms({
style={styles.checkboxContainer}
onPress={handleCheckboxChange}
activeOpacity={0.8}
testID="checkbox"
>
{checkboxIcon}
<Text style={styles.checkboxText}>
Expand Down
35 changes: 35 additions & 0 deletions src/components/Responsive/__tests__/Responsive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
height,
RFPercentage,
RFValue,
RFValueWithFixedSecondParam,
width,
} from "..";

describe("Responsive", () => {
it("should calculate width correctly", () => {
const calculatedWidth = width("100%");
expect(calculatedWidth).toBeGreaterThan(0);
});

it("should calculate height correctly", () => {
const calculatedHeight = height(50);
expect(calculatedHeight).toBeGreaterThan(0);
});

it("should calculate RFPercentage correctly", () => {
const calculatedRFPercentage = RFPercentage(50);
expect(calculatedRFPercentage).toBeGreaterThan(0);
});

it("should calculate RFValue correctly", () => {
const calculatedRFValue = RFValue(16);
expect(calculatedRFValue).toBeGreaterThan(0);
});

it("should calculate RFValueWithFixedSecondParam correctly", () => {
const calculatedRFValueWithFixedSecondParam =
RFValueWithFixedSecondParam(16);
expect(calculatedRFValueWithFixedSecondParam).toBeGreaterThan(0);
});
});
19 changes: 19 additions & 0 deletions src/components/Urls/__tests__/Urls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { termsURL, privacyURL, buyMeACoffeeURL, repoGithubURL } from "..";

describe("Urls", () => {
it("should have the correct terms URL", () => {
expect(termsURL).toBe("https://2devs.tech/terms");
});

it("should have the correct privacy URL", () => {
expect(privacyURL).toBe("https://2devs.tech/PrivacyPolicy");
});

it("should have the correct buy me a coffee URL", () => {
expect(buyMeACoffeeURL).toBe("https://www.buymeacoffee.com/gabriellogan");
});

it("should have the correct GitHub repository URL", () => {
expect(repoGithubURL).toBe("https://github.com/gabriel-logan/2Devs-Mobile");
});
});

0 comments on commit f0a423c

Please sign in to comment.