Skip to content

Commit 0fb9d67

Browse files
authored
Infra/test renderer (#1470)
* adding react-native part of the testing library * export from our own wrapper * added hint tests * PR Comments * remove unused props in tests and in demo screen
1 parent 54dc3a0 commit 0fb9d67

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"@react-native-community/netinfo": "^5.6.2",
7171
"@react-native-picker/picker": "^1.9.4",
7272
"@testing-library/react-hooks": "^3.4.2",
73+
"@testing-library/react-native": "^7.2.0",
7374
"@types/hoist-non-react-statics": "^3.3.1",
7475
"@types/lodash": "^4.0.0",
7576
"@types/prop-types": "^15.5.3",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react';
2+
import {Hint} from 'react-native-ui-lib';
3+
import {Colors} from '../../../style';
4+
import {render, findStyle} from '../../../uilib-test-renderer';
5+
6+
const HintTestComponent = ({showHint}) => {
7+
return (
8+
<Hint
9+
visible={showHint}
10+
message={'Hint message to hint things'}
11+
position={Hint.positions.TOP}
12+
useSideTip
13+
key={'1'}
14+
targetFrame={{x: 1, y: 1, height: 1, width: 1}}
15+
onBackgroundPress={() => {}}
16+
color={Colors.white}
17+
removePaddings
18+
enableShadow
19+
testID={'Hint'}
20+
/>
21+
);
22+
};
23+
24+
describe('Hint Screen component test', () => {
25+
it('Test Hint component background color', async () => {
26+
const hintTestId = 'Hint.message';
27+
28+
const expectedColor = Colors.primary;
29+
const element = <HintTestComponent showHint/>;
30+
31+
const {getByTestId} = render(element);
32+
33+
const wrapper = getByTestId('Hint');
34+
expect(wrapper).toBeTruthy();
35+
36+
const hint = getByTestId(hintTestId);
37+
const color = findStyle('backgroundColor', hint);
38+
39+
expect(color).toBe(expectedColor);
40+
expect(hint).toBeTruthy();
41+
});
42+
43+
it('Test Hint modal is not visible when showHint is false', async () => {
44+
const element = <HintTestComponent showHint={false}/>;
45+
46+
const {queryByTestId} = render(element);
47+
48+
const modal = queryByTestId('Hint.modal');
49+
50+
expect(modal).toBeNull();
51+
});
52+
53+
it('Test Hint modal is visible when showHint is true', async () => {
54+
const element = <HintTestComponent showHint/>;
55+
56+
const {getByTestId, queryAllByTestId} = render(element);
57+
const hint = getByTestId('Hint');
58+
expect(hint).toBeTruthy();
59+
60+
expect(queryAllByTestId('Hint.modal')).toBeTruthy();
61+
});
62+
});

src/uilib-test-renderer/helper.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import _ from 'lodash';
2+
3+
interface Props {
4+
style: any;
5+
}
6+
7+
interface Component {
8+
props: Props;
9+
}
10+
11+
const findStyle = <T>(key: string, component: Component): T => {
12+
const flat = _.flatMap(component.props.style) as Array<any | undefined>;
13+
const color = _.find(flat, key)[key];
14+
return color;
15+
};
16+
17+
export {findStyle};

src/uilib-test-renderer/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export * from '@testing-library/react-native';
2+
import {findStyle} from './helper';
3+
4+
export {
5+
findStyle
6+
};
7+
8+
/* Docs: https://callstack.github.io/react-native-testing-library/docs/api/ */

0 commit comments

Comments
 (0)