Skip to content

Commit c57fe5a

Browse files
committed
add unit test with test-library and jest/native
1 parent 23bc9b0 commit c57fe5a

File tree

7 files changed

+2172
-64
lines changed

7 files changed

+2172
-64
lines changed

hackathon/spacecraft/jest.setup.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable @typescript-eslint/no-empty-function */
2+
/* eslint-disable @typescript-eslint/no-var-requires */
3+
/* eslint-disable no-undef */
4+
import "react-native-gesture-handler/jestSetup";
5+
6+
jest.mock("react-native-reanimated", () => {
7+
const Reanimated = require("react-native-reanimated/mock");
8+
9+
// The mock for `call` immediately calls the callback which is incorrect
10+
// So we override it with a no-op
11+
Reanimated.default.call = () => {};
12+
13+
return Reanimated;
14+
});
15+
16+
// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
17+
jest.mock("react-native/Libraries/Animated/NativeAnimatedHelper");

hackathon/spacecraft/package.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,30 @@
66
"ios": "expo start --ios",
77
"web": "expo start --web",
88
"eject": "expo eject",
9-
"lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx ."
9+
"lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
10+
"test": "jest",
11+
"test:unit:watch": "jest --watch"
12+
},
13+
"jest": {
14+
"preset": "jest-expo",
15+
"setupFiles": [
16+
"<rootDir>/jest.setup.js"
17+
]
1018
},
1119
"dependencies": {
1220
"@expo/vector-icons": "^12.0.0",
1321
"@react-native-community/masked-view": "0.1.10",
1422
"@react-navigation/material-bottom-tabs": "^6.0.9",
1523
"@react-navigation/native": "^6.0.6",
1624
"@react-navigation/stack": "^6.0.11",
25+
"@testing-library/jest-native": "^4.0.4",
26+
"@types/jest": "^27.4.0",
27+
"babel-core": "^7.0.0-bridge.0",
28+
"babel-jest": "^27.4.6",
1729
"expo": "^44.0.0",
1830
"expo-status-bar": "~1.2.0",
31+
"jest": "26",
32+
"jest-expo": "^44.0.1",
1933
"react": "17.0.1",
2034
"react-dom": "17.0.1",
2135
"react-native": "0.64.3",
@@ -30,6 +44,8 @@
3044
},
3145
"devDependencies": {
3246
"@babel/core": "^7.12.9",
47+
"@babel/preset-typescript": "^7.16.7",
48+
"@testing-library/react-native": "^9.0.0",
3349
"@types/react": "~17.0.21",
3450
"@types/react-dom": "~17.0.9",
3551
"@types/react-native": "~0.64.12",

hackathon/spacecraft/src/navigation/BottomTabNavigator.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { FontAwesome5 } from "@expo/vector-icons";
55
import { createStackNavigator } from "@react-navigation/stack";
66

77
import StarshipFeedScreen from "../screens/StarshipFeedScreen";
8-
import StarshipFeedExampleScreen from "../screens/exercice/FeedScreen";
98
import StarshipDetailsScreen from "../screens/StarshipDetailsScreen";
109

1110
import { AppRoutes } from "./AppRoutes";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from "react";
2+
import { fireEvent, render } from "@testing-library/react-native";
3+
4+
import { FormInput } from "~/components/FromInput";
5+
6+
describe("StarshipCard", () => {
7+
const mock = jest.fn();
8+
it("renders correctly", () => {
9+
const { getAllByText } = render(
10+
<FormInput label="your-car" value="tesla" onChangeText={mock} />
11+
);
12+
getAllByText("your-car");
13+
fireEvent.changeText(getAllByText("your-car")[0], "tesla");
14+
expect(mock).toHaveBeenCalled();
15+
});
16+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from "react";
2+
import { render } from "@testing-library/react-native";
3+
import { NavigationContainer } from "@react-navigation/native";
4+
5+
import StarshipCard from "../components/StarshipCard";
6+
7+
const shipFixture = {
8+
name: "Millennium Falcon",
9+
model: "YT-1300 light freighter",
10+
};
11+
12+
describe("StarshipCard", () => {
13+
it("renders correctly", () => {
14+
const { getByText } = render(
15+
<NavigationContainer>
16+
<StarshipCard ship={shipFixture} />
17+
</NavigationContainer>
18+
);
19+
getByText("Millennium Falcon");
20+
});
21+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("works", () => {
2+
expect(1).toBe(1);
3+
});

0 commit comments

Comments
 (0)