Skip to content

Commit f305bb4

Browse files
Update: CR comments
1 parent 1f78272 commit f305bb4

File tree

5 files changed

+43
-33
lines changed

5 files changed

+43
-33
lines changed

packages/native/src/lib/ElementAssertion.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ 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";
6+
57
export class ElementAssertion extends Assertion<ReactTestInstance> {
68
public constructor(actual: ReactTestInstance) {
79
super(actual);
810
}
911

1012
public override toString = (): string => {
11-
if (this.actual === null) {
12-
return "null";
13-
}
14-
15-
return `<${this.actual.type.toString()} ... />`;
13+
return instanceToString(this.actual);
1614
};
1715

1816
/**
@@ -32,7 +30,7 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
3230
});
3331
const invertedError = new AssertionError({
3432
actual: this.actual,
35-
message: `Expected element ${this.toString()} to NOT be disabled.`,
33+
message: `Expected element ${this.toString()} NOT to be disabled.`,
3634
});
3735

3836
return this.execute({
@@ -58,7 +56,7 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
5856
});
5957
const invertedError = new AssertionError({
6058
actual: this.actual,
61-
message: `Expected element ${this.toString()} to NOT be enabled.`,
59+
message: `Expected element ${this.toString()} NOT to be enabled.`,
6260
});
6361

6462
return this.execute({

packages/native/src/lib/ToBeEmptyElementAssertion.ts renamed to packages/native/src/lib/ToBeEmptyAssertion.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
11
import { Assertion, AssertionError } from "@assertive-ts/core";
22
import { ReactTestInstance } from "react-test-renderer";
33

4-
import { isEmpty } from "./helpers/helpers";
4+
import { instanceToString, isEmpty } from "./helpers/helpers";
55

66
/**
77
* Assertion for checking if a React element is empty.
88
*/
9-
export class ToBeEmptyElementAssertion extends Assertion<ReactTestInstance> {
9+
export class ToBeEmptyAssertion extends Assertion<ReactTestInstance> {
1010
public constructor(actual: ReactTestInstance) {
1111
super(actual);
1212
}
1313

1414
public override toString = (): string => {
15-
if (this.actual === null) {
16-
return "null";
17-
}
18-
19-
return `<${this.actual.type.toString()} ... />`;
15+
return instanceToString(this.actual);
2016
};
2117

2218
/**
2319
* Check if the element is empty.
2420
*
2521
* @example
2622
* ```
27-
* expect(element).toBeEmptyElement();
23+
* expect(element).toBeEmpty();
2824
* ```
2925
*
3026
* @returns the assertion instance
3127
*/
32-
public toBeEmptyElement(): this {
28+
public toBeEmpty(): this {
3329
const error = new AssertionError({
3430
actual: this.actual,
3531
message: `Expected element ${this.toString()} to be empty.`,
3632
});
3733
const invertedError = new AssertionError({
3834
actual: this.actual,
39-
message: `Expected element ${this.toString()} to NOT be empty.`,
35+
message: `Expected element ${this.toString()} NOT to be empty.`,
4036
});
4137

4238
return this.execute({

packages/native/src/lib/helpers/helpers.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ReactTestInstance } from "react-test-renderer";
2+
13
/**
24
* Checks if a value is empty.
35
*
@@ -15,3 +17,17 @@ export function isEmpty(value: unknown): boolean {
1517

1618
return false;
1719
}
20+
21+
/**
22+
* Converts a ReactTestInstance to a string representation.
23+
*
24+
* @param instance - The ReactTestInstance to convert.
25+
* @returns A string representation of the instance.
26+
*/
27+
export function instanceToString(instance: ReactTestInstance | null): string {
28+
if (instance === null) {
29+
return "null";
30+
}
31+
32+
return `<${instance.type.toString()} ... />`;
33+
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
3434
.toHaveMessage("Expected element <TextInput ... /> to be disabled.");
3535
expect(() => test.not.toBeEnabled())
3636
.toThrowError(AssertionError)
37-
.toHaveMessage("Expected element <TextInput ... /> to NOT be enabled.");
37+
.toHaveMessage("Expected element <TextInput ... /> NOT to be enabled.");
3838
});
3939
});
4040
});
@@ -59,7 +59,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
5959
.toHaveMessage("Expected element <View ... /> to be enabled.");
6060
expect(() => parent.not.toBeDisabled())
6161
.toThrowError(AssertionError)
62-
.toHaveMessage("Expected element <View ... /> to NOT be disabled.");
62+
.toHaveMessage("Expected element <View ... /> NOT to be disabled.");
6363
});
6464
});
6565

@@ -83,13 +83,13 @@ describe("[Unit] ElementAssertion.test.ts", () => {
8383
.toHaveMessage("Expected element <View ... /> to be disabled.");
8484
expect(() => parent.not.toBeEnabled())
8585
.toThrowError(AssertionError)
86-
.toHaveMessage("Expected element <View ... /> to NOT be enabled.");
86+
.toHaveMessage("Expected element <View ... /> NOT to be enabled.");
8787
expect(() => child.toBeDisabled())
8888
.toThrowError(AssertionError)
8989
.toHaveMessage("Expected element <View ... /> to be disabled.");
9090
expect(() => child.not.toBeEnabled())
9191
.toThrowError(AssertionError)
92-
.toHaveMessage("Expected element <View ... /> to NOT be enabled.");
92+
.toHaveMessage("Expected element <View ... /> NOT to be enabled.");
9393
});
9494
});
9595
});
@@ -114,7 +114,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
114114
.toHaveMessage("Expected element <View ... /> to be enabled.");
115115
expect(() => child.not.toBeDisabled())
116116
.toThrowError(AssertionError)
117-
.toHaveMessage("Expected element <View ... /> to NOT be disabled.");
117+
.toHaveMessage("Expected element <View ... /> NOT to be disabled.");
118118
});
119119

120120
it("returns error for parent element", () => {
@@ -124,7 +124,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
124124
.toHaveMessage("Expected element <View ... /> to be disabled.");
125125
expect(() => parent.not.toBeEnabled())
126126
.toThrowError(AssertionError)
127-
.toHaveMessage("Expected element <View ... /> to NOT be enabled.");
127+
.toHaveMessage("Expected element <View ... /> NOT to be enabled.");
128128
});
129129
});
130130
});

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import { AssertionError, expect } from "@assertive-ts/core";
22
import { render } from "@testing-library/react-native";
33
import { View, Text } from "react-native";
44

5-
import { ToBeEmptyElementAssertion } from "../../src/lib/ToBeEmptyElementAssertion";
5+
import { ToBeEmptyAssertion } from "../../src/lib/ToBeEmptyAssertion";
66

7-
describe("[Unit] ToBeEmptyElementAssertion.test.ts", () => {
8-
describe(".toBeEmptyElement", () => {
7+
describe("[Unit] toBeEmptyAssertion.test.ts", () => {
8+
describe(".toBeEmpty", () => {
99
context("when the element is empty", () => {
1010
it("returns the assertion instance", () => {
1111
const element = render(<View testID="id" />);
12-
const test = new ToBeEmptyElementAssertion(element.getByTestId("id"));
12+
const test = new ToBeEmptyAssertion(element.getByTestId("id"));
1313

14-
expect(test.toBeEmptyElement()).toBe(test);
15-
expect(() => test.not.toBeEmptyElement())
14+
expect(test.toBeEmpty()).toBe(test);
15+
expect(() => test.not.toBeEmpty())
1616
.toThrowError(AssertionError)
17-
.toHaveMessage("Expected element <View ... /> to NOT be empty.");
17+
.toHaveMessage("Expected element <View ... /> NOT to be empty.");
1818
});
1919
});
2020

@@ -25,10 +25,10 @@ describe("[Unit] ToBeEmptyElementAssertion.test.ts", () => {
2525
<Text>{"Not empty"}</Text>
2626
</View>,
2727
);
28-
const test = new ToBeEmptyElementAssertion(element.getByTestId("id"));
28+
const test = new ToBeEmptyAssertion(element.getByTestId("id"));
2929

30-
expect(test.not.toBeEmptyElement()).toBeEqual(test);
31-
expect(() => test.toBeEmptyElement())
30+
expect(test.not.toBeEmpty()).toBeEqual(test);
31+
expect(() => test.toBeEmpty())
3232
.toThrowError(AssertionError)
3333
.toHaveMessage("Expected element <View ... /> to be empty.");
3434
});

0 commit comments

Comments
 (0)