11import { AssertionError , expect } from "@assertive-ts/core" ;
22import { render } from "@testing-library/react" ;
3- import { ReactElement } from "react" ;
43
54import { ElementAssertion } from "../../../src/lib/ElementAssertion" ;
65
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" ;
229
2310describe ( "[Unit] ElementAssertion.test.ts" , ( ) => {
2411 describe ( ".toBeInTheDocument" , ( ) => {
2512 context ( "when the element is in the document" , ( ) => {
2613 it ( "returns the assertion instance" , async ( ) => {
27- const { findByRole } = render ( < TestComponent /> ) ;
14+ const { findByRole } = render ( < SimpleTestComponent /> ) ;
2815 const button = await findByRole ( "button" , { name : "click me" } ) ;
2916 const test = new ElementAssertion ( button ) ;
3017
@@ -39,7 +26,6 @@ describe("[Unit] ElementAssertion.test.ts", () => {
3926 context ( "when the element is not in the document" , ( ) => {
4027 it ( "throws an assertion error" , ( ) => {
4128 const detachedElement = document . createElement ( "div" ) ;
42-
4329 const test = new ElementAssertion ( detachedElement ) ;
4430
4531 expect ( ( ) => test . toBeInTheDocument ( ) )
@@ -51,10 +37,87 @@ describe("[Unit] ElementAssertion.test.ts", () => {
5137 } ) ;
5238 } ) ;
5339
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+
54117 describe ( ".toHaveAttribute" , ( ) => {
55118 context ( "when the element has the attribute with the expected value" , ( ) => {
56119 it ( "returns the assertion instance" , async ( ) => {
57- const { findByRole } = render ( < TestComponentElement /> ) ;
120+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
58121 const button = await findByRole ( "button" , { name : "click me" } ) ;
59122 const test = new ElementAssertion ( button ) ;
60123
@@ -68,7 +131,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
68131
69132 context ( "when the element has the attribute with a not expected value" , ( ) => {
70133 it ( "throws an assertion error" , async ( ) => {
71- const { findByRole } = render ( < TestComponentElement /> ) ;
134+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
72135 const button = await findByRole ( "button" , { name : "click me" } ) ;
73136 const test = new ElementAssertion ( button ) ;
74137
@@ -83,7 +146,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
83146
84147 context ( "when the element has the attribute without checking value" , ( ) => {
85148 it ( "returns the assertion instance" , async ( ) => {
86- const { findByRole } = render ( < TestComponentElement /> ) ;
149+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
87150 const button = await findByRole ( "button" , { name : "click me" } ) ;
88151 const test = new ElementAssertion ( button ) ;
89152
@@ -97,7 +160,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
97160
98161 context ( "when the element does not have the attribute" , ( ) => {
99162 it ( "throws an assertion error" , async ( ) => {
100- const { findByRole } = render ( < TestComponentElement /> ) ;
163+ const { findByRole } = render ( < WithAttributesTestComponent /> ) ;
101164 const button = await findByRole ( "button" , { name : "click me" } ) ;
102165 const test = new ElementAssertion ( button ) ;
103166
0 commit comments