Skip to content

Commit 45ecb87

Browse files
Merge pull request #6 from filipemerker/add-tests
Add tests
2 parents f2b5427 + 60a876b commit 45ecb87

File tree

8 files changed

+1745
-77
lines changed

8 files changed

+1745
-77
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
env: {
33
browser: true,
44
es6: true,
5+
jest: true,
56
},
67
extends: ['plugin:react/recommended', 'airbnb'],
78
globals: {

.prettierrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module.exports = {
55
printWidth: 100,
66
singleQuote: true,
77
tabWidth: 2,
8-
trailingComma: 'always',
8+
trailingComma: 'all',
99
useTabs: false,
1010
};

package.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@
1616
"binary search"
1717
],
1818
"scripts": {
19-
"commit": "git-cz"
19+
"commit": "git-cz",
20+
"test": "jest"
21+
},
22+
"jest": {
23+
"testEnvironment": "jsdom",
24+
"preset": "@testing-library/react-native",
25+
"transform": {
26+
"^.+\\.(js)$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
27+
},
28+
"transformIgnorePatterns": [
29+
"node_modules/(?!(jest-)?react-native)"
30+
]
2031
},
2132
"dependencies": {
2233
"lodash": "^4.17.19",
2334
"prop-types": "^15.7.2"
2435
},
2536
"devDependencies": {
37+
"@testing-library/react-native": "^5.0.3",
2638
"babel-eslint": "^10.1.0",
2739
"commitizen": "^4.0.3",
2840
"cz-conventional-changelog": "^3.1.0",
@@ -34,11 +46,12 @@
3446
"eslint-plugin-react": "^7.18.3",
3547
"eslint-plugin-react-hooks": "^1.7.0",
3648
"husky": "^4.2.3",
37-
"metro-react-native-babel-preset": "^0.58.0",
49+
"jest": "^26.1.0",
3850
"prettier-eslint": "^9.0.1",
3951
"react": "^16.9.0",
4052
"react-native": "^0.61.5",
41-
"react-native-text-size": "^4.0.0-rc.1"
53+
"react-native-text-size": "^4.0.0-rc.1",
54+
"react-test-renderer": "^16.13.1"
4255
},
4356
"peerDependencies": {
4457
"react": "*",
@@ -52,7 +65,7 @@
5265
},
5366
"husky": {
5467
"hooks": {
55-
"pre-commit": "npx eslint ."
68+
"pre-commit": "npx eslint . && yarn test"
5669
}
5770
}
5871
}

src/SeeMore/SeeMore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class SeeMore extends React.Component {
136136

137137
return (
138138
<Text
139+
testID="SeeMore"
139140
onLayout={isShowingMore ? undefined : this.onLayout}
140141
numberOfLines={isShowingMore ? undefined : numberOfLines}
141142
{...this.panResponder.panHandlers}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const measure = () => new Promise((a) => a(4305));
2+
3+
export default {
4+
measure,
5+
};

src/__tests__/SeeMore.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import {
3+
render, cleanup, fireEvent, wait,
4+
} from '@testing-library/react-native';
5+
import SeeMore from '../SeeMore';
6+
import SeeMoreUtil from '../SeeMore/SeeMoreUtil';
7+
8+
afterEach(cleanup);
9+
10+
describe('SeeMore', () => {
11+
it('renders small text correctly', () => {
12+
const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
13+
const { baseElement } = render(<SeeMore numberOfLines={2}>{text}</SeeMore>);
14+
15+
expect(baseElement).toMatchSnapshot();
16+
});
17+
18+
it('renders big text correctly', async () => {
19+
const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum';
20+
const truncationIndex = 222;
21+
22+
jest
23+
.spyOn(SeeMoreUtil, 'getTruncationIndex')
24+
.mockImplementation(() => new Promise((a) => a(truncationIndex)));
25+
26+
const { getByTestId, getByText } = render(
27+
<SeeMore numberOfLines={2}>{text}</SeeMore>,
28+
);
29+
30+
fireEvent.layout(getByTestId('SeeMore'), {
31+
nativeEvent: {
32+
layout: {
33+
width: 200,
34+
},
35+
},
36+
persist: Function,
37+
});
38+
39+
await wait(() => {
40+
expect(getByText(/see more/)).toBeTruthy();
41+
expect(getByText(text.slice(0, truncationIndex))).toBeTruthy();
42+
});
43+
});
44+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`SeeMore renders small text correctly 1`] = `
4+
<View
5+
pointerEvents="box-none"
6+
style={
7+
Object {
8+
"flex": 1,
9+
}
10+
}
11+
>
12+
<View
13+
collapsable={true}
14+
pointerEvents="box-none"
15+
style={
16+
Object {
17+
"flex": 1,
18+
}
19+
}
20+
>
21+
<Text
22+
numberOfLines={2}
23+
testID="SeeMore"
24+
>
25+
<Text
26+
linkColor="#2E75F0"
27+
linkPressedColor="#163772"
28+
numberOfLines={2}
29+
seeLessText="see less"
30+
seeMoreText="see more"
31+
style={
32+
Object {
33+
"fontFamily": undefined,
34+
"fontSize": 14,
35+
"fontWeight": "300",
36+
}
37+
}
38+
>
39+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
40+
</Text>
41+
</Text>
42+
</View>
43+
</View>
44+
`;

0 commit comments

Comments
 (0)