Skip to content

Commit 786fb07

Browse files
AntoineThibimdjastrzebski
authored andcommitted
refactor: use screen in the test
1 parent 015b96e commit 786fb07

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed
Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
22
import { Button, Text, View } from 'react-native';
3-
import { render } from '../..';
3+
import { render, screen } from '../..';
44
import '../extend-expect';
55

66
test('.toHaveProp', () => {
7-
const { queryByTestId } = render(
7+
render(
88
<View style={null} testID="view">
99
<Text allowFontScaling={false} testID="text" ellipsizeMode="head">
1010
text
@@ -13,31 +13,67 @@ test('.toHaveProp', () => {
1313
</View>
1414
);
1515

16-
expect(queryByTestId('button')).toHaveProp('accessibilityState', {
16+
const text = screen.getByTestId('text');
17+
const button = screen.getByTestId('button');
18+
const view = screen.getByTestId('view');
19+
20+
expect(button).toHaveProp('accessibilityState', {
1721
disabled: true,
1822
});
19-
expect(queryByTestId('text')).toHaveProp('ellipsizeMode', 'head');
20-
expect(queryByTestId('text')).toHaveProp('allowFontScaling', false);
23+
expect(text).toHaveProp('ellipsizeMode', 'head');
24+
expect(text).toHaveProp('allowFontScaling', false);
2125

22-
expect(queryByTestId('button')).not.toHaveProp('accessibilityStates');
23-
expect(queryByTestId('button')).not.toHaveProp('ellipsizeMode', undefined);
24-
expect(queryByTestId('button')).not.toHaveProp('allowFontScaling', false);
25-
expect(queryByTestId('text')).not.toHaveProp('style');
26+
expect(button).not.toHaveProp('accessibilityStates');
27+
expect(button).not.toHaveProp('ellipsizeMode', undefined);
28+
expect(button).not.toHaveProp('allowFontScaling', false);
29+
expect(text).not.toHaveProp('style');
2630

2731
// title is no longer findable as it is a React child
28-
expect(() =>
29-
expect(queryByTestId('button')).toHaveProp('title', 'ok')
30-
).toThrow();
31-
expect(() =>
32-
expect(queryByTestId('button')).toHaveProp('disabled')
33-
).toThrow();
34-
expect(() =>
35-
expect(queryByTestId('text')).not.toHaveProp('allowFontScaling', false)
36-
).toThrow();
37-
expect(() => expect(queryByTestId('text')).toHaveProp('style')).toThrow();
38-
expect(() =>
39-
expect(queryByTestId('text')).toHaveProp('allowFontScaling', 'wrongValue')
40-
).toThrow();
41-
42-
expect(queryByTestId('view')).toHaveProp('style', null);
32+
expect(() => expect(button).toHaveProp('title', 'ok'))
33+
.toThrowErrorMatchingInlineSnapshot(`
34+
"expect(element).toHaveProp("title", "ok") // Element should have prop title with value "ok"
35+
36+
Expected the element to have prop:
37+
title="ok"
38+
Received:
39+
null"
40+
`);
41+
expect(() => expect(button).toHaveProp('disabled'))
42+
.toThrowErrorMatchingInlineSnapshot(`
43+
"expect(element).toHaveProp("disabled") // Element should have prop disabled
44+
45+
Expected the element to have prop:
46+
disabled
47+
Received:
48+
null"
49+
`);
50+
expect(() => expect(text).not.toHaveProp('allowFontScaling', false))
51+
.toThrowErrorMatchingInlineSnapshot(`
52+
"expect(element).not.toHaveProp("allowFontScaling", false) // Element should have prop allowFontScaling with value false
53+
54+
Expected the element not to have prop:
55+
allowFontScaling=false
56+
Received:
57+
allowFontScaling=false"
58+
`);
59+
expect(() => expect(text).toHaveProp('style'))
60+
.toThrowErrorMatchingInlineSnapshot(`
61+
"expect(element).toHaveProp("style") // Element should have prop style
62+
63+
Expected the element to have prop:
64+
style
65+
Received:
66+
null"
67+
`);
68+
expect(() => expect(text).toHaveProp('allowFontScaling', 'wrongValue'))
69+
.toThrowErrorMatchingInlineSnapshot(`
70+
"expect(element).toHaveProp("allowFontScaling", "wrongValue") // Element should have prop allowFontScaling with value "wrongValue"
71+
72+
Expected the element to have prop:
73+
allowFontScaling="wrongValue"
74+
Received:
75+
allowFontScaling=false"
76+
`);
77+
78+
expect(view).toHaveProp('style', null);
4379
});

0 commit comments

Comments
 (0)