Skip to content

Commit f1858fe

Browse files
authored
Merge pull request #4 from lawnstarter/task/add-unit-tests
Task/add unit tests
2 parents 855acc2 + ddadf60 commit f1858fe

File tree

7 files changed

+1387
-35
lines changed

7 files changed

+1387
-35
lines changed

.babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"react-native"
4+
]
5+
}

.circleci/config.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Javascript Node CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# specify the version you desire here
10+
- image: circleci/node:8.9.4-browsers
11+
12+
# Specify service dependencies here if necessary
13+
# CircleCI maintains a library of pre-built images
14+
# documented at https://circleci.com/docs/2.0/circleci-images/
15+
# - image: circleci/mongo:3.4.4
16+
17+
working_directory: ~/repo
18+
19+
steps:
20+
- checkout
21+
22+
# Download and cache dependencies
23+
- restore_cache:
24+
keys:
25+
- v1-dependencies-{{ checksum "package.json" }}
26+
# fallback to using the latest cache if no exact match is found
27+
- v1-dependencies-
28+
29+
- run: yarn install
30+
31+
- save_cache:
32+
paths:
33+
- node_modules
34+
key: v1-dependencies-{{ checksum "package.json" }}
35+
36+
# run tests!
37+
- run: yarn test
38+

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
.DS_Store
2+
node_modules/
3+
coverage/

package.json

+24-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,36 @@
2020
"lodash.isequal": "^4.5.0"
2121
},
2222
"devDependencies": {
23+
"babel-jest": "^22.2.2",
24+
"babel-preset-react-native": "^4.0.0",
25+
"enzyme": "^3.3.0",
26+
"enzyme-adapter-react-16": "^1.1.1",
27+
"enzyme-to-json": "^3.3.1",
2328
"eslint": "^4.17.0",
2429
"eslint-config-airbnb": "^16.1.0",
2530
"eslint-plugin-import": "^2.8.0",
2631
"eslint-plugin-jsx-a11y": "^6.0.3",
2732
"eslint-plugin-react": "^7.6.1",
33+
"jest": "^22.3.0",
2834
"prop-types": "^15.6.0",
2935
"react": "16.0.0",
30-
"react-native": "0.51.0"
36+
"react-dom": "^16.2.0",
37+
"react-native": "0.51.0",
38+
"react-test-renderer": "^16.2.0"
39+
},
40+
"scripts": {
41+
"test": "jest",
42+
"test:watch": "jest --watch",
43+
"test:coverage": "jest --coverage",
44+
"open:coverage": "open ./coverage/lcov-report/index.html"
45+
},
46+
"jest": {
47+
"preset": "react-native",
48+
"setupFiles": [
49+
"./test/setup.js"
50+
],
51+
"snapshotSerializers": [
52+
"enzyme-to-json/serializer"
53+
]
3154
}
3255
}

test/setup.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Enzyme, { shallow, render, mount } from 'enzyme';
2+
import Adapter from 'enzyme-adapter-react-16';
3+
4+
// React 16 Enzyme adapter
5+
Enzyme.configure({ adapter: new Adapter() });
6+
7+
// Make Enzyme functions available in all test files without importing
8+
global.shallow = shallow;
9+
global.render = render;
10+
global.mount = mount;

test/test.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React from 'react';
2+
import { Platform } from 'react-native';
3+
import RNPickerSelect from '../src/';
4+
5+
const selectItems = [
6+
{
7+
label: 'Orange',
8+
value: 'orange',
9+
},
10+
{
11+
label: 'Red',
12+
value: 'red',
13+
},
14+
{
15+
label: 'Green',
16+
value: 'green',
17+
},
18+
{
19+
label: 'Purple',
20+
value: 'purple',
21+
},
22+
{
23+
label: 'Yellow',
24+
value: 'yellow',
25+
},
26+
];
27+
28+
const placeholder = {
29+
label: 'Select a color...',
30+
value: null,
31+
};
32+
33+
describe('RNPickerSelect', () => {
34+
it('should set the picked value to state', () => {
35+
const wrapper = shallow(<RNPickerSelect
36+
items={selectItems}
37+
placeholder={placeholder}
38+
onSelect={() => {}}
39+
/>);
40+
41+
wrapper.find('[testId="RNPickerSelectIOS"]').props().onValueChange('orange', 1);
42+
expect(wrapper.state().selectedItem.value).toEqual('orange');
43+
});
44+
45+
it('should return the expected option to a callback passed into onSelect', () => {
46+
const onSelectSpy = jest.fn();
47+
const wrapper = shallow(<RNPickerSelect
48+
items={selectItems}
49+
placeholder={placeholder}
50+
onSelect={onSelectSpy}
51+
/>);
52+
53+
wrapper.find('[testId="RNPickerSelectIOS"]').props().onValueChange('orange', 1);
54+
expect(onSelectSpy).toHaveBeenCalledWith({ index: 1, value: 'orange' });
55+
});
56+
57+
it('should show the picker when pressed', () => {
58+
const wrapper = shallow(<RNPickerSelect
59+
items={selectItems}
60+
placeholder={placeholder}
61+
onSelect={() => {}}
62+
/>);
63+
64+
const touchable = wrapper.find('TouchableWithoutFeedback').at(1);
65+
touchable.simulate('press');
66+
expect(wrapper.state().showPicker).toEqual(true);
67+
});
68+
69+
it('should not show the picker when pressed if disabled', () => {
70+
const wrapper = shallow(<RNPickerSelect
71+
items={selectItems}
72+
placeholder={placeholder}
73+
onSelect={() => {}}
74+
disabled
75+
/>);
76+
77+
const touchable = wrapper.find('TouchableWithoutFeedback').at(1);
78+
touchable.simulate('press');
79+
expect(wrapper.state().showPicker).toEqual(false);
80+
});
81+
82+
it('should update the picked value when the parent updates', () => {
83+
const wrapper = shallow(<RNPickerSelect
84+
items={selectItems}
85+
placeholder={placeholder}
86+
onSelect={() => {}}
87+
value="red"
88+
/>);
89+
90+
expect(wrapper.state().selectedItem.value).toEqual('red');
91+
wrapper.setProps({ value: 'orange' });
92+
expect(wrapper.state().selectedItem.value).toEqual('orange');
93+
});
94+
95+
it('should set the picked value to state (Android)', () => {
96+
Platform.OS = 'android';
97+
const wrapper = shallow(<RNPickerSelect
98+
items={selectItems}
99+
placeholder={placeholder}
100+
onSelect={() => {}}
101+
/>);
102+
103+
wrapper.find('[testId="RNPickerSelectAndroid"]').props().onValueChange('orange', 1);
104+
expect(wrapper.state().selectedItem.value).toEqual('orange');
105+
});
106+
});

0 commit comments

Comments
 (0)