Skip to content

Commit 38b14c1

Browse files
committed
fix: add a check to ensure Jest doesn't throw error. closes #644
1 parent 2e23161 commit 38b14c1

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

src/components/Switch.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import setColor from 'color';
77
import { withTheme } from '../core/theming';
88
import type { Theme } from '../types';
99

10-
const version = NativeModules.PlatformConstants.reactNativeVersion;
10+
const version = NativeModules.PlatformConstants
11+
? NativeModules.PlatformConstants.reactNativeVersion
12+
: undefined;
1113

1214
type Props = React.ElementProps<NativeSwitch> & {
1315
/**
@@ -124,7 +126,7 @@ class Switch extends React.Component<Props> {
124126
: grey50;
125127

126128
const props =
127-
version.major === 0 && version.minor <= 56
129+
version && version.major === 0 && version.minor <= 56
128130
? {
129131
onTintColor,
130132
thumbTintColor,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* @flow */
2+
3+
import * as React from 'react';
4+
import renderer from 'react-test-renderer';
5+
import Switch from '../Switch';
6+
import { pink500 } from '../../styles/colors';
7+
8+
it('renders on switch', () => {
9+
const tree = renderer.create(<Switch value />).toJSON();
10+
11+
expect(tree).toMatchSnapshot();
12+
});
13+
14+
it('renders off switch', () => {
15+
const tree = renderer.create(<Switch value={false} />).toJSON();
16+
17+
expect(tree).toMatchSnapshot();
18+
});
19+
20+
it('renders disabled switch', () => {
21+
const tree = renderer.create(<Switch disabled value />).toJSON();
22+
23+
expect(tree).toMatchSnapshot();
24+
});
25+
26+
it('renders switch with color', () => {
27+
const tree = renderer.create(<Switch value color={pink500} />).toJSON();
28+
29+
expect(tree).toMatchSnapshot();
30+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`renders disabled switch 1`] = `
4+
<RCTSwitch
5+
disabled={true}
6+
onChange={[Function]}
7+
onResponderTerminationRequest={[Function]}
8+
onStartShouldSetResponder={[Function]}
9+
onTintColor="#03dac4"
10+
style={
11+
Object {
12+
"height": 31,
13+
"width": 51,
14+
}
15+
}
16+
value={true}
17+
/>
18+
`;
19+
20+
exports[`renders off switch 1`] = `
21+
<RCTSwitch
22+
onChange={[Function]}
23+
onResponderTerminationRequest={[Function]}
24+
onStartShouldSetResponder={[Function]}
25+
onTintColor="#03dac4"
26+
style={
27+
Object {
28+
"height": 31,
29+
"width": 51,
30+
}
31+
}
32+
value={false}
33+
/>
34+
`;
35+
36+
exports[`renders on switch 1`] = `
37+
<RCTSwitch
38+
onChange={[Function]}
39+
onResponderTerminationRequest={[Function]}
40+
onStartShouldSetResponder={[Function]}
41+
onTintColor="#03dac4"
42+
style={
43+
Object {
44+
"height": 31,
45+
"width": 51,
46+
}
47+
}
48+
value={true}
49+
/>
50+
`;
51+
52+
exports[`renders switch with color 1`] = `
53+
<RCTSwitch
54+
onChange={[Function]}
55+
onResponderTerminationRequest={[Function]}
56+
onStartShouldSetResponder={[Function]}
57+
onTintColor="#e91e63"
58+
style={
59+
Object {
60+
"height": 31,
61+
"width": 51,
62+
}
63+
}
64+
value={true}
65+
/>
66+
`;

0 commit comments

Comments
 (0)