File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -121,6 +121,50 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
121121 } ) ;
122122 }
123123
124+ /**
125+ * Check if an element is contained within another element.
126+ *
127+ * @example
128+ * ```
129+ * expect(parent).toContainElement(child);
130+ * ```
131+ *
132+ * @param element - The element to check for.
133+ * @returns the assertion instance
134+ */
135+ public toContainElement ( element : ReactTestInstance ) : this {
136+ const error = new AssertionError ( {
137+ actual : this . actual ,
138+ message : `Expected element ${ this . toString ( ) } to contain element ${ instanceToString ( element ) } .` ,
139+ } ) ;
140+ const invertedError = new AssertionError ( {
141+ actual : this . actual ,
142+ message : `Expected element ${ this . toString ( ) } NOT to contain element ${ instanceToString ( element ) } .` ,
143+ } ) ;
144+
145+ const isElementContained = (
146+ parentElement : ReactTestInstance ,
147+ childElement : ReactTestInstance ,
148+ ) : boolean => {
149+ if ( parentElement === null || childElement === null ) {
150+ return false ;
151+ }
152+
153+ return (
154+ parentElement . findAll (
155+ node =>
156+ node . type === childElement . type && node . props === childElement . props ,
157+ ) . length > 0
158+ ) ;
159+ } ;
160+
161+ return this . execute ( {
162+ assertWhen : isElementContained ( this . actual , element ) ,
163+ error,
164+ invertedError,
165+ } ) ;
166+ }
167+
124168 private isElementDisabled ( element : ReactTestInstance ) : boolean {
125169 const { type } = element ;
126170 const elementType = type . toString ( ) ;
You can’t perform that action at this time.
0 commit comments