Skip to content

Commit aed8344

Browse files
Move toBeEmptyAssertion to the element assertions
1 parent f305bb4 commit aed8344

File tree

4 files changed

+59
-82
lines changed

4 files changed

+59
-82
lines changed

packages/native/src/lib/ElementAssertion.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Assertion, AssertionError } from "@assertive-ts/core";
22
import { get } from "dot-prop-immutable";
33
import { ReactTestInstance } from "react-test-renderer";
44

5-
import { instanceToString } from "./helpers/helpers";
5+
import { instanceToString, isEmpty } from "./helpers/helpers";
66

77
export class ElementAssertion extends Assertion<ReactTestInstance> {
88
public constructor(actual: ReactTestInstance) {
@@ -66,6 +66,33 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
6666
});
6767
}
6868

69+
/**
70+
* Check if the element is empty.
71+
*
72+
* @example
73+
* ```
74+
* expect(element).toBeEmpty();
75+
* ```
76+
*
77+
* @returns the assertion instance
78+
*/
79+
public toBeEmpty(): this {
80+
const error = new AssertionError({
81+
actual: this.actual,
82+
message: `Expected element ${this.toString()} to be empty.`,
83+
});
84+
const invertedError = new AssertionError({
85+
actual: this.actual,
86+
message: `Expected element ${this.toString()} NOT to be empty.`,
87+
});
88+
89+
return this.execute({
90+
assertWhen: isEmpty(this.actual.children),
91+
error,
92+
invertedError,
93+
});
94+
}
95+
6996
private isElementDisabled(element: ReactTestInstance): boolean {
7097
const { type } = element;
7198
const elementType = type.toString();

packages/native/src/lib/ToBeEmptyAssertion.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/native/test/lib/ElementAssertion.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { render } from "@testing-library/react-native";
33
import {
44
View,
55
TextInput,
6+
Text,
67
} from "react-native";
78

89
import { ElementAssertion } from "../../src/lib/ElementAssertion";
@@ -129,4 +130,34 @@ describe("[Unit] ElementAssertion.test.ts", () => {
129130
});
130131
});
131132
});
133+
134+
describe(".toBeEmpty", () => {
135+
context("when the element is empty", () => {
136+
it("returns the assertion instance", () => {
137+
const element = render(<View testID="id" />);
138+
const test = new ElementAssertion(element.getByTestId("id"));
139+
140+
expect(test.toBeEmpty()).toBe(test);
141+
expect(() => test.not.toBeEmpty())
142+
.toThrowError(AssertionError)
143+
.toHaveMessage("Expected element <View ... /> NOT to be empty.");
144+
});
145+
});
146+
147+
context("when the element is NOT empty", () => {
148+
it("throws an error", () => {
149+
const element = render(
150+
<View testID="id">
151+
<Text>{"Not empty"}</Text>
152+
</View>,
153+
);
154+
const test = new ElementAssertion(element.getByTestId("id"));
155+
156+
expect(test.not.toBeEmpty()).toBeEqual(test);
157+
expect(() => test.toBeEmpty())
158+
.toThrowError(AssertionError)
159+
.toHaveMessage("Expected element <View ... /> to be empty.");
160+
});
161+
});
162+
});
132163
});

packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)