@@ -5,21 +5,97 @@ import {render} from './helpers/test-utils'
55expect . addSnapshotSerializer ( plugins . ConvertAnsi )
66
77test ( '.toBeInTheDOM' , ( ) => {
8- const { queryByTestId} = render ( `<span data-testid="count-value">2</span>` )
8+ const { queryByTestId} = render ( `
9+ <span data-testid="count-container">
10+ <span data-testid="count-value"></span>
11+ </span>` )
12+
13+ const containerElement = queryByTestId ( 'count-container' )
14+ const valueElement = queryByTestId ( 'count-value' )
15+ const nonExistantElement = queryByTestId ( 'not-exists' )
16+ const fakeElement = { thisIsNot : 'an html element' }
17+
18+ // Testing toBeInTheDOM without container
19+ expect ( valueElement ) . toBeInTheDOM ( )
20+ expect ( nonExistantElement ) . not . toBeInTheDOM ( )
21+
22+ // negative test cases wrapped in throwError assertions for coverage.
23+ expect ( ( ) => expect ( valueElement ) . not . toBeInTheDOM ( ) ) . toThrowError ( )
24+
25+ expect ( ( ) => expect ( nonExistantElement ) . toBeInTheDOM ( ) ) . toThrowError ( )
26+
27+ expect ( ( ) => expect ( fakeElement ) . toBeInTheDOM ( ) ) . toThrowError ( )
28+
29+ // Testing toBeInTheDOM with container
30+ expect ( valueElement ) . toBeInTheDOM ( containerElement )
31+ expect ( containerElement ) . not . toBeInTheDOM ( valueElement )
32+
33+ expect ( ( ) =>
34+ expect ( valueElement ) . not . toBeInTheDOM ( containerElement ) ,
35+ ) . toThrowError ( )
36+
37+ expect ( ( ) =>
38+ expect ( nonExistantElement ) . toBeInTheDOM ( containerElement ) ,
39+ ) . toThrowError ( )
40+
41+ expect ( ( ) =>
42+ expect ( fakeElement ) . toBeInTheDOM ( containerElement ) ,
43+ ) . toThrowError ( )
44+
45+ expect ( ( ) => {
46+ expect ( valueElement ) . toBeInTheDOM ( fakeElement )
47+ } ) . toThrowError ( )
48+ } )
49+
50+ test ( '.toContainElement' , ( ) => {
51+ const { queryByTestId} = render ( `
52+ <span data-testid="grandparent">
53+ <span data-testid="parent">
54+ <span data-testid="child"></span>
55+ </span>
56+ </span>
57+ ` )
958
10- expect ( queryByTestId ( 'count-value' ) ) . toBeInTheDOM ( )
11- expect ( queryByTestId ( 'count-value1' ) ) . not . toBeInTheDOM ( )
59+ const grandparent = queryByTestId ( 'grandparent' )
60+ const parent = queryByTestId ( 'parent' )
61+ const child = queryByTestId ( 'child' )
62+ const nonExistantElement = queryByTestId ( 'not-exists' )
63+ const fakeElement = { thisIsNot : 'an html element' }
64+
65+ expect ( grandparent ) . toContainElement ( parent )
66+ expect ( grandparent ) . toContainElement ( child )
67+ expect ( parent ) . toContainElement ( child )
68+ expect ( parent ) . not . toContainElement ( grandparent )
69+ expect ( child ) . not . toContainElement ( parent )
70+ expect ( child ) . not . toContainElement ( grandparent )
1271
1372 // negative test cases wrapped in throwError assertions for coverage.
1473 expect ( ( ) =>
15- expect ( queryByTestId ( 'count-value' ) ) . not . toBeInTheDOM ( ) ,
74+ expect ( nonExistantElement ) . not . toContainElement ( child ) ,
75+ ) . toThrowError ( )
76+ expect ( ( ) => expect ( parent ) . toContainElement ( grandparent ) ) . toThrowError ( )
77+ expect ( ( ) =>
78+ expect ( nonExistantElement ) . toContainElement ( grandparent ) ,
79+ ) . toThrowError ( )
80+ expect ( ( ) =>
81+ expect ( grandparent ) . toContainElement ( nonExistantElement ) ,
82+ ) . toThrowError ( )
83+ expect ( ( ) =>
84+ expect ( nonExistantElement ) . toContainElement ( nonExistantElement ) ,
85+ ) . toThrowError ( )
86+ expect ( ( ) =>
87+ expect ( nonExistantElement ) . toContainElement ( fakeElement ) ,
1688 ) . toThrowError ( )
1789 expect ( ( ) =>
18- expect ( queryByTestId ( 'count-value1' ) ) . toBeInTheDOM ( ) ,
90+ expect ( fakeElement ) . toContainElement ( nonExistantElement ) ,
1991 ) . toThrowError ( )
2092 expect ( ( ) =>
21- expect ( { thisIsNot : 'an html element' } ) . toBeInTheDOM ( ) ,
93+ expect ( fakeElement ) . not . toContainElement ( nonExistantElement ) ,
2294 ) . toThrowError ( )
95+ expect ( ( ) => expect ( fakeElement ) . toContainElement ( grandparent ) ) . toThrowError ( )
96+ expect ( ( ) => expect ( grandparent ) . toContainElement ( fakeElement ) ) . toThrowError ( )
97+ expect ( ( ) => expect ( fakeElement ) . toContainElement ( fakeElement ) ) . toThrowError ( )
98+ expect ( ( ) => expect ( grandparent ) . not . toContainElement ( child ) ) . toThrowError ( )
2399} )
24100
25101test ( '.toBeEmpty' , ( ) => {
0 commit comments