Skip to content

Commit 0567117

Browse files
tests: setup jest, testing library and add tests (#239)
Co-authored-by: Daniel Merrill <[email protected]>
1 parent b885715 commit 0567117

File tree

5 files changed

+2313
-36
lines changed

5 files changed

+2313
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/**/*
22
/lib
3+
coverage/

babel.config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["module:metro-react-native-babel-preset"]
3+
}

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1",
8+
"test": "jest",
99
"build": "tsc"
1010
},
1111
"husky": {
@@ -32,22 +32,35 @@
3232
"bugs": {
3333
"url": "https://github.com/computerjazz/react-native-draggable-flatlist/issues"
3434
},
35+
"jest": {
36+
"preset": "react-native",
37+
"setupFiles": [
38+
"./node_modules/react-native-gesture-handler/jestSetup.js"
39+
],
40+
"transform": {
41+
"^.+\\.[t|j]sx?$": "babel-jest"
42+
}
43+
},
3544
"homepage": "https://github.com/computerjazz/react-native-draggable-flatlist#readme",
3645
"peerDependencies": {
3746
"react-native": ">=0.59.0",
3847
"react-native-gesture-handler": "^1.0.0",
3948
"react-native-reanimated": "^1.0.0"
4049
},
4150
"devDependencies": {
51+
"@testing-library/react-native": "^7.1.0",
4252
"@types/react": "^16.8.8",
4353
"@types/react-native": "^0.60.8",
54+
"babel-jest": "^26.6.1",
4455
"husky": "^4.2.0",
56+
"jest": "^26.6.1",
4557
"prettier": "1.19.1",
4658
"pretty-quick": "^2.0.1",
47-
"react": "^16.12.0",
59+
"react": "^16.13.1",
4860
"react-native": "^0.61.5",
4961
"react-native-gesture-handler": "^1.5.3",
5062
"react-native-reanimated": "^1.13.0",
63+
"react-test-renderer": "~16.11.0",
5164
"typescript": "^3.7.3"
5265
}
5366
}

tests/index.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { useState } from "react";
2+
import { Text, View } from "react-native";
3+
import { fireEvent, render } from "@testing-library/react-native";
4+
import DraggableFlatList from "../src/index";
5+
6+
jest.mock("react-native-reanimated", () =>
7+
require("react-native-reanimated/mock")
8+
);
9+
10+
const DummyFlatList = props => {
11+
const [data, setData] = useState([
12+
{ id: "1", name: "item 1" },
13+
{ id: "2", name: "item 2" }
14+
]);
15+
16+
return (
17+
<DraggableFlatList
18+
keyExtractor={item => item.id}
19+
renderItem={({ item, drag }) => (
20+
<View onLongPress={drag}>
21+
<Text>{item.name}</Text>
22+
</View>
23+
)}
24+
testID="draggable-flat-list"
25+
data={data}
26+
{...props}
27+
/>
28+
);
29+
};
30+
31+
describe("DraggableFlatList", () => {
32+
const setup = propOverrides => {
33+
const defaultProps = {
34+
...propOverrides
35+
};
36+
37+
return render(<DummyFlatList {...defaultProps} />);
38+
};
39+
40+
it("calls onDragBegin with the index of the element when the drag starts", () => {
41+
const mockOnDragBegin = jest.fn();
42+
const { getByText } = setup({ onDragBegin: mockOnDragBegin });
43+
44+
fireEvent(getByText("item 1"), "longPress");
45+
46+
expect(mockOnDragBegin).toHaveBeenCalledWith(0);
47+
});
48+
49+
it("renders a placeholder when renderPlaceholder is defined", () => {
50+
const renderPlaceholder = () => <View testID="some-placeholder" />;
51+
const { getByText, getByTestId } = setup({
52+
renderPlaceholder: renderPlaceholder
53+
});
54+
55+
fireEvent(getByText("item 1"), "longPress");
56+
57+
expect(getByTestId("some-placeholder")).toBeDefined();
58+
});
59+
});

0 commit comments

Comments
 (0)