Skip to content

Commit a0ee8a7

Browse files
committed
refactor: tweaks and cleanup
1 parent 4ece01b commit a0ee8a7

File tree

5 files changed

+185
-142
lines changed

5 files changed

+185
-142
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"flow": "flow",
8383
"copy-flowtypes": "cp typings/index.flow.js build",
8484
"lint": "eslint src --cache",
85+
"validate": "yarn lint && yarn typecheck && yarn test",
8586
"prepublish": "yarn build",
8687
"build:js": "babel src --out-dir build --extensions \".js,.ts,.jsx,.tsx\" --source-maps --ignore \"**/__tests__/**\"",
8788
"build:js:watch": "yarn build:js --watch",

src/matchers/__tests__/__snapshots__/to-have-style.test.tsx.snap

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 123 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React from 'react';
2-
import { View, Text, StyleSheet, Pressable } from 'react-native';
2+
import { StyleSheet, View, Pressable } from 'react-native';
33
import { render } from '../..';
44
import '../extend-expect';
55

6-
test('handles positive test cases', () => {
7-
const styles = StyleSheet.create({
8-
container: { borderBottomColor: 'white' },
9-
});
10-
const { getByTestId } = render(
6+
const styles = StyleSheet.create({
7+
container: { borderBottomColor: 'white' },
8+
});
9+
10+
test('toHaveStyle() handles basic cases', () => {
11+
const screen = render(
1112
<View
12-
testID="container"
13+
testID="view"
1314
style={[
1415
{
1516
backgroundColor: 'blue',
@@ -20,92 +21,155 @@ test('handles positive test cases', () => {
2021
[[{ width: '50%' }]],
2122
styles.container,
2223
]}
23-
>
24-
<Text>Hello World</Text>
25-
</View>
24+
/>
2625
);
2726

28-
const container = getByTestId('container');
27+
const view = screen.getByTestId('view');
28+
expect(view).toHaveStyle({ backgroundColor: 'blue' });
29+
expect(view).toHaveStyle({ height: '100%' });
30+
expect(view).toHaveStyle({ backgroundColor: 'blue', height: '100%' });
31+
expect(view).toHaveStyle([{ backgroundColor: 'blue' }, { height: '100%' }]);
32+
33+
expect(view).toHaveStyle({ borderBottomColor: 'white' });
34+
expect(view).toHaveStyle({ width: '50%' });
35+
expect(view).toHaveStyle([[{ width: '50%' }]]);
36+
expect(view).toHaveStyle({
37+
transform: [{ scale: 2 }, { rotate: '45deg' }],
38+
});
2939

30-
expect(container).toHaveStyle({ backgroundColor: 'blue', height: '100%' });
31-
expect(container).toHaveStyle([
40+
expect(view).not.toHaveStyle({ backgroundColor: 'red' });
41+
expect(view).not.toHaveStyle({ height: '50%' });
42+
expect(view).not.toHaveStyle({ backgroundColor: 'blue', height: '50%' });
43+
expect(view).not.toHaveStyle([
3244
{ backgroundColor: 'blue' },
33-
{ height: '100%' },
45+
{ height: '50%' },
3446
]);
35-
expect(container).toHaveStyle({ backgroundColor: 'blue' });
36-
expect(container).toHaveStyle({ height: '100%' });
37-
expect(container).toHaveStyle({ borderBottomColor: 'white' });
38-
expect(container).toHaveStyle({ width: '50%' });
39-
expect(container).toHaveStyle([[{ width: '50%' }]]);
40-
expect(container).toHaveStyle({
41-
transform: [{ scale: 2 }, { rotate: '45deg' }],
47+
expect(view).not.toHaveStyle({
48+
transform: [{ scale: 2 }],
49+
});
50+
expect(view).not.toHaveStyle({
51+
transform: [{ rotate: '45deg' }, { scale: 2 }],
4252
});
4353
});
4454

45-
test('handles negative test cases', () => {
46-
const { getByTestId } = render(
55+
test('toHaveStyle error messages', () => {
56+
const screen = render(
4757
<View
48-
testID="container"
58+
testID="view"
4959
style={{
5060
backgroundColor: 'blue',
5161
borderBottomColor: 'black',
5262
height: '100%',
5363
transform: [{ scale: 2 }, { rotate: '45deg' }],
5464
}}
55-
>
56-
<Text>Hello World</Text>
57-
</View>
65+
/>
5866
);
5967

60-
const container = getByTestId('container');
68+
const view = screen.getByTestId('view');
69+
expect(() => expect(view).toHaveStyle({ backgroundColor: 'red' }))
70+
.toThrowErrorMatchingInlineSnapshot(`
71+
"expect(element).toHaveStyle()
72+
73+
- Expected
74+
+ Received
75+
76+
- backgroundColor: red;
77+
+ backgroundColor: blue;"
78+
`);
79+
6180
expect(() =>
62-
expect(container).toHaveStyle({
81+
expect(view).toHaveStyle({
6382
backgroundColor: 'blue',
6483
transform: [{ scale: 1 }],
6584
})
66-
).toThrowErrorMatchingSnapshot();
67-
expect(container).not.toHaveStyle({ fontWeight: 'bold' });
68-
expect(container).not.toHaveStyle({ color: 'black' });
69-
expect(container).not.toHaveStyle({
70-
transform: [{ rotate: '45deg' }, { scale: 2 }],
71-
});
72-
expect(container).not.toHaveStyle({ transform: [{ rotate: '45deg' }] });
73-
});
85+
).toThrowErrorMatchingInlineSnapshot(`
86+
"expect(element).toHaveStyle()
7487
75-
test('handles when the style prop is undefined', () => {
76-
const { getByTestId } = render(
77-
<View testID="container">
78-
<Text>Hello World</Text>
79-
</View>
80-
);
88+
- Expected
89+
+ Received
90+
91+
backgroundColor: blue;
92+
transform: [
93+
{
94+
- "scale": 1
95+
+ "scale": 2
96+
+ },
97+
+ {
98+
+ "rotate": "45deg"
99+
}
100+
];"
101+
`);
102+
103+
expect(() => expect(view).not.toHaveStyle({ backgroundColor: 'blue' }))
104+
.toThrowErrorMatchingInlineSnapshot(`
105+
"expect(element).not.toHaveStyle()
106+
107+
Expected element not to have style:
108+
backgroundColor: blue;
109+
Received:
110+
backgroundColor: blue;"
111+
`);
112+
113+
expect(() => expect(view).toHaveStyle({ fontWeight: 'bold' }))
114+
.toThrowErrorMatchingInlineSnapshot(`
115+
"expect(element).toHaveStyle()
116+
117+
- Expected
118+
+ Received
119+
120+
- fontWeight: bold;"
121+
`);
81122

82-
const container = getByTestId('container');
123+
expect(() => expect(view).not.toHaveStyle({ backgroundColor: 'blue' }))
124+
.toThrowErrorMatchingInlineSnapshot(`
125+
"expect(element).not.toHaveStyle()
83126
84-
expect(container).not.toHaveStyle({ fontWeight: 'bold' });
127+
Expected element not to have style:
128+
backgroundColor: blue;
129+
Received:
130+
backgroundColor: blue;"
131+
`);
85132
});
86133

87-
test('handles transform when transform undefined', () => {
88-
const { getByTestId } = render(
134+
test('toHaveStyle() supports missing "style" prop', () => {
135+
const screen = render(<View testID="view" />);
136+
137+
const view = screen.getByTestId('view');
138+
expect(view).not.toHaveStyle({ fontWeight: 'bold' });
139+
});
140+
141+
test('toHaveStyle() supports undefined "transform" style', () => {
142+
const screen = render(
89143
<View
90-
testID="container"
144+
testID="view"
91145
style={{
92146
backgroundColor: 'blue',
93147
transform: undefined,
94148
}}
95-
>
96-
<Text>Hello World</Text>
97-
</View>
149+
/>
98150
);
99151

100-
const container = getByTestId('container');
101-
expect(() =>
102-
expect(container).toHaveStyle({ transform: [{ scale: 1 }] })
103-
).toThrowErrorMatchingSnapshot();
152+
const view = screen.getByTestId('view');
153+
expect(() => expect(view).toHaveStyle({ transform: [{ scale: 1 }] }))
154+
.toThrowErrorMatchingInlineSnapshot(`
155+
"expect(element).toHaveStyle()
156+
157+
- Expected
158+
+ Received
159+
160+
- transform: [
161+
- {
162+
- "scale": 1
163+
- }
164+
- ];
165+
+ transform: undefined;"
166+
`);
104167
});
105168

106-
test('handles Pressable with function style prop', () => {
107-
const { getByTestId } = render(
108-
<Pressable testID="test" style={() => ({ backgroundColor: 'blue' })} />
169+
test('toHaveStyle() supports Pressable with function "style" prop', () => {
170+
const screen = render(
171+
<Pressable testID="view" style={() => ({ backgroundColor: 'blue' })} />
109172
);
110-
expect(getByTestId('test')).toHaveStyle({ backgroundColor: 'blue' });
173+
174+
expect(screen.getByTestId('view')).toHaveStyle({ backgroundColor: 'blue' });
111175
});

src/matchers/extend-expect.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { StyleProp } from 'react-native';
12
import type { TextMatch, TextMatchOptions } from '../matches';
3+
import type { Style } from './to-have-style';
24

35
export interface JestNativeMatchers<R> {
46
toBeOnTheScreen(): R;

0 commit comments

Comments
 (0)