Skip to content

Commit 41d0f97

Browse files
WIP: toBeEmpty function
1 parent 05e2863 commit 41d0f97

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)