1
1
import { AssertionError , expect } from "@assertive-ts/core" ;
2
2
import { render } from "@testing-library/react" ;
3
- import { ReactElement } from "react" ;
4
3
5
4
import { ElementAssertion } from "../../../src/lib/ElementAssertion" ;
6
5
7
- function TestComponent ( ) : ReactElement {
8
- return (
9
- < div >
10
- < button > click me</ button >
11
- </ div >
12
- ) ;
13
- }
14
-
15
- function TestComponentElement ( ) : ReactElement {
16
- return (
17
- < button role = "button" type = "submit" className = "btn primary" disabled >
18
- click me
19
- </ button >
20
- ) ;
21
- }
6
+ import { NestedElementsTestComponent } from "./fixtures/nestedElementsTestComponent" ;
7
+ import { SimpleTestComponent } from "./fixtures/simpleTestComponent" ;
8
+ import { WithAttributesTestComponent } from "./fixtures/withAttributesTestComponent" ;
22
9
23
10
describe ( "[Unit] ElementAssertion.test.ts" , ( ) => {
24
11
describe ( ".toBeInTheDocument" , ( ) => {
25
12
context ( "when the element is in the document" , ( ) => {
26
13
it ( "returns the assertion instance" , async ( ) => {
27
- const { findByRole } = render ( < TestComponent /> ) ;
14
+ const { findByRole } = render ( < SimpleTestComponent /> ) ;
28
15
const button = await findByRole ( "button" , { name : "click me" } ) ;
29
16
const test = new ElementAssertion ( button ) ;
30
17
@@ -39,7 +26,6 @@ describe("[Unit] ElementAssertion.test.ts", () => {
39
26
context ( "when the element is not in the document" , ( ) => {
40
27
it ( "throws an assertion error" , ( ) => {
41
28
const detachedElement = document . createElement ( "div" ) ;
42
-
43
29
const test = new ElementAssertion ( detachedElement ) ;
44
30
45
31
expect ( ( ) => test . toBeInTheDocument ( ) )
@@ -51,10 +37,87 @@ describe("[Unit] ElementAssertion.test.ts", () => {
51
37
} ) ;
52
38
} ) ;
53
39
40
+ describe ( ".toContainElement" , ( ) => {
41
+ context ( "when the descendant element is contained in the ancestor element" , ( ) => {
42
+ context ( "and it is a direct child" , ( ) => {
43
+ it ( "returns the assertion instance" , async ( ) => {
44
+ const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
45
+ const grandparent = await findByTestId ( "grandparent" ) ;
46
+ const parent = await findByTestId ( "parent" ) ;
47
+ const child = await findByTestId ( "child" ) ;
48
+ const svgElement = await findByTestId ( "svg-element" ) ;
49
+ const grandparentTest = new ElementAssertion ( grandparent ) ;
50
+ const parentTest = new ElementAssertion ( parent ) ;
51
+
52
+ expect ( grandparentTest . toContainElement ( parent ) ) ;
53
+ expect ( grandparentTest . toContainElement ( svgElement ) ) ;
54
+ expect ( parentTest . toContainElement ( child ) ) ;
55
+
56
+ expect ( ( ) => grandparentTest . not . toContainElement ( parent ) )
57
+ . toThrowError ( AssertionError )
58
+ . toHaveMessage ( "Expected the container to NOT contain the element" ) ;
59
+
60
+ expect ( ( ) => grandparentTest . not . toContainElement ( svgElement ) )
61
+ . toThrowError ( AssertionError )
62
+ . toHaveMessage ( "Expected the container to NOT contain the element" ) ;
63
+
64
+ expect ( ( ) => parentTest . not . toContainElement ( child ) )
65
+ . toThrowError ( AssertionError )
66
+ . toHaveMessage ( "Expected the container to NOT contain the element" ) ;
67
+ } ) ;
68
+ } ) ;
69
+
70
+ context ( "and it is an indirect child" , ( ) => {
71
+ it ( "returns the assertion instance" , async ( ) => {
72
+ const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
73
+ const grandparent = await findByTestId ( "grandparent" ) ;
74
+ const child = await findByTestId ( "child" ) ;
75
+ const grandparentTest = new ElementAssertion ( grandparent ) ;
76
+
77
+ expect ( grandparentTest . toContainElement ( child ) ) ;
78
+
79
+ expect ( ( ) => grandparentTest . not . toContainElement ( child ) )
80
+ . toThrowError ( AssertionError )
81
+ . toHaveMessage ( "Expected the container to NOT contain the element" ) ;
82
+ } ) ;
83
+ } ) ;
84
+
85
+ context ( "and it is a deeply nested child" , ( ) => {
86
+ it ( "returns the assertion instance" , async ( ) => {
87
+ const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
88
+ const grandparent = await findByTestId ( "grandparent" ) ;
89
+ const deepChild = await findByTestId ( "deep-child" ) ;
90
+ const grandparentTest = new ElementAssertion ( grandparent ) ;
91
+
92
+ expect ( grandparentTest . toContainElement ( deepChild ) ) ;
93
+
94
+ expect ( ( ) => grandparentTest . not . toContainElement ( deepChild ) )
95
+ . toThrowError ( AssertionError )
96
+ . toHaveMessage ( "Expected the container to NOT contain the element" ) ;
97
+ } ) ;
98
+ } ) ;
99
+ } ) ;
100
+
101
+ context ( "when element is NOT contained in ancestor element" , ( ) => {
102
+ it ( "throws an assertion error" , async ( ) => {
103
+ const notChildElement = document . createElement ( "span" ) ;
104
+ const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
105
+ const grandparent = await findByTestId ( "grandparent" ) ;
106
+ const grandparentTest = new ElementAssertion ( grandparent ) ;
107
+
108
+ expect ( ( ) => grandparentTest . toContainElement ( notChildElement ) )
109
+ . toThrowError ( AssertionError )
110
+ . toHaveMessage ( "Expected the container to contain the element" ) ;
111
+
112
+ expect ( grandparentTest . not . toContainElement ( notChildElement ) ) . toBeEqual ( grandparentTest ) ;
113
+ } ) ;
114
+ } ) ;
115
+ } ) ;
116
+
54
117
describe ( ".toHaveAttribute" , ( ) => {
55
118
context ( "when the element has the attribute with the expected value" , ( ) => {
56
119
it ( "returns the assertion instance" , async ( ) => {
57
- const { findByRole } = render ( < TestComponentElement /> ) ;
120
+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
58
121
const button = await findByRole ( "button" , { name : "click me" } ) ;
59
122
const test = new ElementAssertion ( button ) ;
60
123
@@ -68,7 +131,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
68
131
69
132
context ( "when the element has the attribute with a not expected value" , ( ) => {
70
133
it ( "throws an assertion error" , async ( ) => {
71
- const { findByRole } = render ( < TestComponentElement /> ) ;
134
+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
72
135
const button = await findByRole ( "button" , { name : "click me" } ) ;
73
136
const test = new ElementAssertion ( button ) ;
74
137
@@ -83,7 +146,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
83
146
84
147
context ( "when the element has the attribute without checking value" , ( ) => {
85
148
it ( "returns the assertion instance" , async ( ) => {
86
- const { findByRole } = render ( < TestComponentElement /> ) ;
149
+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
87
150
const button = await findByRole ( "button" , { name : "click me" } ) ;
88
151
const test = new ElementAssertion ( button ) ;
89
152
@@ -97,7 +160,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
97
160
98
161
context ( "when the element does not have the attribute" , ( ) => {
99
162
it ( "throws an assertion error" , async ( ) => {
100
- const { findByRole } = render ( < TestComponentElement /> ) ;
163
+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
101
164
const button = await findByRole ( "button" , { name : "click me" } ) ;
102
165
const test = new ElementAssertion ( button ) ;
103
166
0 commit comments