File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { Assertion , AssertionError } from "@assertive-ts/core" ;
2
+ import { ReactTestInstance } from "react-test-renderer" ;
3
+
4
+ export class ToBeEmptyElementAssertion extends Assertion < ReactTestInstance > {
5
+ public constructor ( actual : ReactTestInstance ) {
6
+ super ( actual ) ;
7
+ }
8
+
9
+ public override toString = ( ) : string => {
10
+ if ( this . actual === null ) {
11
+ return "null" ;
12
+ }
13
+
14
+ return `<${ this . actual . type . toString ( ) } ... />` ;
15
+ } ;
16
+
17
+ /**
18
+ * Check if the element is empty.
19
+ *
20
+ * @example
21
+ * ```
22
+ * expect(element).toBeEmptyElement();
23
+ * ```
24
+ *
25
+ * @returns the assertion instance
26
+ */
27
+ public toBeEmptyElement ( ) : this {
28
+ const error = new AssertionError ( {
29
+ actual : this . actual ,
30
+ message : `Expected element ${ this . toString ( ) } to be empty.` ,
31
+ } ) ;
32
+ const invertedError = new AssertionError ( {
33
+ actual : this . actual ,
34
+ message : `Expected element ${ this . toString ( ) } to NOT be empty.` ,
35
+ } ) ;
36
+
37
+ return this . execute ( {
38
+ assertWhen : this . isEmpty ( this . actual ) ,
39
+ error,
40
+ invertedError,
41
+ } ) ;
42
+ }
43
+
44
+ private isEmpty ( element : ReactTestInstance ) : boolean {
45
+ if ( ! element ?. props ?. children ) {
46
+ return true ;
47
+ }
48
+
49
+ return element ?. props ?. children . length === 0 ;
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments