From 41d0f97fc51a080ed60576c335e8927a02378ff6 Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Mon, 17 Mar 2025 10:47:13 -0500 Subject: [PATCH 1/8] WIP: toBeEmpty function --- .../src/lib/ToBeEmptyElementAssertion.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 packages/native/src/lib/ToBeEmptyElementAssertion.ts diff --git a/packages/native/src/lib/ToBeEmptyElementAssertion.ts b/packages/native/src/lib/ToBeEmptyElementAssertion.ts new file mode 100644 index 00000000..3b59632c --- /dev/null +++ b/packages/native/src/lib/ToBeEmptyElementAssertion.ts @@ -0,0 +1,51 @@ +import { Assertion, AssertionError } from "@assertive-ts/core"; +import { ReactTestInstance } from "react-test-renderer"; + +export class ToBeEmptyElementAssertion extends Assertion { + public constructor(actual: ReactTestInstance) { + super(actual); + } + + public override toString = (): string => { + if (this.actual === null) { + return "null"; + } + + return `<${this.actual.type.toString()} ... />`; + }; + + /** + * Check if the element is empty. + * + * @example + * ``` + * expect(element).toBeEmptyElement(); + * ``` + * + * @returns the assertion instance + */ + public toBeEmptyElement(): this { + const error = new AssertionError({ + actual: this.actual, + message: `Expected element ${this.toString()} to be empty.`, + }); + const invertedError = new AssertionError({ + actual: this.actual, + message: `Expected element ${this.toString()} to NOT be empty.`, + }); + + return this.execute({ + assertWhen: this.isEmpty(this.actual), + error, + invertedError, + }); + } + + private isEmpty(element: ReactTestInstance): boolean { + if(!element?.props?.children) { + return true; + } + + return element?.props?.children.length === 0; + } +} \ No newline at end of file From 6dfdd21db7f766971de9024499f662ca890d4443 Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Mon, 17 Mar 2025 10:47:33 -0500 Subject: [PATCH 2/8] toBeEmpty tests --- .../lib/ToBeEmptyElementAssertion.test.tsx | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx diff --git a/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx b/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx new file mode 100644 index 00000000..e28a680d --- /dev/null +++ b/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx @@ -0,0 +1,37 @@ +import { AssertionError, expect } from "@assertive-ts/core"; +import { render } from "@testing-library/react-native"; +import { View, Text } from "react-native"; + +import { ToBeEmptyElementAssertion } from "../../src/lib/ToBeEmptyElementAssertion"; + +describe("[Unit] ToBeEmptyElementAssertion.test.ts", () => { + describe(".toBeEmptyElement", () => { + context("when the element is empty", () => { + it("returns the assertion instance", () => { + const element = render(); + const test = new ToBeEmptyElementAssertion(element.getByTestId("id")); + + expect(test.toBeEmptyElement()).toBe(test); + expect(() => test.not.toBeEmptyElement()) + .toThrowError(AssertionError) + .toHaveMessage("Expected element to NOT be empty."); + }); + }); + + context("when the element is NOT empty", () => { + it("throws an error", () => { + const element = render( + + {"Not empty"} + , + ); + const test = new ToBeEmptyElementAssertion(element.getByTestId("id")); + + expect(test.not.toBeEmptyElement()).toBeEqual(test); + expect(() => test.toBeEmptyElement()) + .toThrowError(AssertionError) + .toHaveMessage("Expected element to be empty."); + }); + }); + }); +}); From debfd8ae11f4eb9277c0626354a4274d697539f6 Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Mon, 17 Mar 2025 13:01:15 -0500 Subject: [PATCH 3/8] Fix: use element 'children' --- packages/native/src/lib/ToBeEmptyElementAssertion.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/native/src/lib/ToBeEmptyElementAssertion.ts b/packages/native/src/lib/ToBeEmptyElementAssertion.ts index 3b59632c..ab0f2fd4 100644 --- a/packages/native/src/lib/ToBeEmptyElementAssertion.ts +++ b/packages/native/src/lib/ToBeEmptyElementAssertion.ts @@ -42,10 +42,16 @@ export class ToBeEmptyElementAssertion extends Assertion { } private isEmpty(element: ReactTestInstance): boolean { - if(!element?.props?.children) { + const children = element?.children; + + if (!children) { return true; } - return element?.props?.children.length === 0; + if (Array.isArray(children)) { + return children.length === 0; + } + + return false; } -} \ No newline at end of file +} From 496edc59a1a507808cb2f6429cc6542dad361354 Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Mon, 17 Mar 2025 15:45:03 -0500 Subject: [PATCH 4/8] CR: Move isEmpty to a helper file --- .../src/lib/ToBeEmptyElementAssertion.ts | 21 ++++++------------- packages/native/src/lib/helpers/helpers.ts | 15 +++++++++++++ 2 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 packages/native/src/lib/helpers/helpers.ts diff --git a/packages/native/src/lib/ToBeEmptyElementAssertion.ts b/packages/native/src/lib/ToBeEmptyElementAssertion.ts index ab0f2fd4..b6bede40 100644 --- a/packages/native/src/lib/ToBeEmptyElementAssertion.ts +++ b/packages/native/src/lib/ToBeEmptyElementAssertion.ts @@ -1,6 +1,11 @@ import { Assertion, AssertionError } from "@assertive-ts/core"; import { ReactTestInstance } from "react-test-renderer"; +import { isEmpty } from "./helpers/helpers"; + +/** + * Assertion for checking if a React element is empty. + */ export class ToBeEmptyElementAssertion extends Assertion { public constructor(actual: ReactTestInstance) { super(actual); @@ -35,23 +40,9 @@ export class ToBeEmptyElementAssertion extends Assertion { }); return this.execute({ - assertWhen: this.isEmpty(this.actual), + assertWhen: isEmpty(this.actual), error, invertedError, }); } - - private isEmpty(element: ReactTestInstance): boolean { - const children = element?.children; - - if (!children) { - return true; - } - - if (Array.isArray(children)) { - return children.length === 0; - } - - return false; - } } diff --git a/packages/native/src/lib/helpers/helpers.ts b/packages/native/src/lib/helpers/helpers.ts new file mode 100644 index 00000000..afaf4d05 --- /dev/null +++ b/packages/native/src/lib/helpers/helpers.ts @@ -0,0 +1,15 @@ +import { ReactTestInstance } from "react-test-renderer"; + +export function isEmpty(element: ReactTestInstance): boolean { + const children = element?.children; + + if (!children) { + return true; + } + + if (Array.isArray(children)) { + return children.length === 0; + } + + return false; +} From 1f782720dc31584f1393299a46601fc39da39a9a Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Mon, 17 Mar 2025 15:55:33 -0500 Subject: [PATCH 5/8] Add isEmpty doc. --- .../src/lib/ToBeEmptyElementAssertion.ts | 2 +- packages/native/src/lib/helpers/helpers.ts | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/native/src/lib/ToBeEmptyElementAssertion.ts b/packages/native/src/lib/ToBeEmptyElementAssertion.ts index b6bede40..43d71b22 100644 --- a/packages/native/src/lib/ToBeEmptyElementAssertion.ts +++ b/packages/native/src/lib/ToBeEmptyElementAssertion.ts @@ -40,7 +40,7 @@ export class ToBeEmptyElementAssertion extends Assertion { }); return this.execute({ - assertWhen: isEmpty(this.actual), + assertWhen: isEmpty(this.actual.children), error, invertedError, }); diff --git a/packages/native/src/lib/helpers/helpers.ts b/packages/native/src/lib/helpers/helpers.ts index afaf4d05..76e6f2a6 100644 --- a/packages/native/src/lib/helpers/helpers.ts +++ b/packages/native/src/lib/helpers/helpers.ts @@ -1,14 +1,16 @@ -import { ReactTestInstance } from "react-test-renderer"; - -export function isEmpty(element: ReactTestInstance): boolean { - const children = element?.children; - - if (!children) { +/** + * Checks if a value is empty. + * + * @param value - The value to check. + * @returns `true` if the value is empty, `false` otherwise. + */ +export function isEmpty(value: unknown): boolean { + if (!value) { return true; } - if (Array.isArray(children)) { - return children.length === 0; + if (Array.isArray(value)) { + return value.length === 0; } return false; From f305bb4cf72237a2e321a472fc620fec508b37db Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Tue, 18 Mar 2025 13:01:54 -0500 Subject: [PATCH 6/8] Update: CR comments --- packages/native/src/lib/ElementAssertion.ts | 12 +++++------ ...mentAssertion.ts => ToBeEmptyAssertion.ts} | 16 ++++++--------- packages/native/src/lib/helpers/helpers.ts | 16 +++++++++++++++ .../native/test/lib/ElementAssertion.test.tsx | 12 +++++------ .../lib/ToBeEmptyElementAssertion.test.tsx | 20 +++++++++---------- 5 files changed, 43 insertions(+), 33 deletions(-) rename packages/native/src/lib/{ToBeEmptyElementAssertion.ts => ToBeEmptyAssertion.ts} (67%) diff --git a/packages/native/src/lib/ElementAssertion.ts b/packages/native/src/lib/ElementAssertion.ts index d1350b95..aa9d6804 100644 --- a/packages/native/src/lib/ElementAssertion.ts +++ b/packages/native/src/lib/ElementAssertion.ts @@ -2,17 +2,15 @@ import { Assertion, AssertionError } from "@assertive-ts/core"; import { get } from "dot-prop-immutable"; import { ReactTestInstance } from "react-test-renderer"; +import { instanceToString } from "./helpers/helpers"; + export class ElementAssertion extends Assertion { public constructor(actual: ReactTestInstance) { super(actual); } public override toString = (): string => { - if (this.actual === null) { - return "null"; - } - - return `<${this.actual.type.toString()} ... />`; + return instanceToString(this.actual); }; /** @@ -32,7 +30,7 @@ export class ElementAssertion extends Assertion { }); const invertedError = new AssertionError({ actual: this.actual, - message: `Expected element ${this.toString()} to NOT be disabled.`, + message: `Expected element ${this.toString()} NOT to be disabled.`, }); return this.execute({ @@ -58,7 +56,7 @@ export class ElementAssertion extends Assertion { }); const invertedError = new AssertionError({ actual: this.actual, - message: `Expected element ${this.toString()} to NOT be enabled.`, + message: `Expected element ${this.toString()} NOT to be enabled.`, }); return this.execute({ diff --git a/packages/native/src/lib/ToBeEmptyElementAssertion.ts b/packages/native/src/lib/ToBeEmptyAssertion.ts similarity index 67% rename from packages/native/src/lib/ToBeEmptyElementAssertion.ts rename to packages/native/src/lib/ToBeEmptyAssertion.ts index 43d71b22..b6bd6030 100644 --- a/packages/native/src/lib/ToBeEmptyElementAssertion.ts +++ b/packages/native/src/lib/ToBeEmptyAssertion.ts @@ -1,22 +1,18 @@ import { Assertion, AssertionError } from "@assertive-ts/core"; import { ReactTestInstance } from "react-test-renderer"; -import { isEmpty } from "./helpers/helpers"; +import { instanceToString, isEmpty } from "./helpers/helpers"; /** * Assertion for checking if a React element is empty. */ -export class ToBeEmptyElementAssertion extends Assertion { +export class ToBeEmptyAssertion extends Assertion { public constructor(actual: ReactTestInstance) { super(actual); } public override toString = (): string => { - if (this.actual === null) { - return "null"; - } - - return `<${this.actual.type.toString()} ... />`; + return instanceToString(this.actual); }; /** @@ -24,19 +20,19 @@ export class ToBeEmptyElementAssertion extends Assertion { * * @example * ``` - * expect(element).toBeEmptyElement(); + * expect(element).toBeEmpty(); * ``` * * @returns the assertion instance */ - public toBeEmptyElement(): this { + public toBeEmpty(): this { const error = new AssertionError({ actual: this.actual, message: `Expected element ${this.toString()} to be empty.`, }); const invertedError = new AssertionError({ actual: this.actual, - message: `Expected element ${this.toString()} to NOT be empty.`, + message: `Expected element ${this.toString()} NOT to be empty.`, }); return this.execute({ diff --git a/packages/native/src/lib/helpers/helpers.ts b/packages/native/src/lib/helpers/helpers.ts index 76e6f2a6..6ab4db1e 100644 --- a/packages/native/src/lib/helpers/helpers.ts +++ b/packages/native/src/lib/helpers/helpers.ts @@ -1,3 +1,5 @@ +import { ReactTestInstance } from "react-test-renderer"; + /** * Checks if a value is empty. * @@ -15,3 +17,17 @@ export function isEmpty(value: unknown): boolean { return false; } + +/** + * Converts a ReactTestInstance to a string representation. + * + * @param instance - The ReactTestInstance to convert. + * @returns A string representation of the instance. + */ +export function instanceToString(instance: ReactTestInstance | null): string { + if (instance === null) { + return "null"; + } + + return `<${instance.type.toString()} ... />`; +} diff --git a/packages/native/test/lib/ElementAssertion.test.tsx b/packages/native/test/lib/ElementAssertion.test.tsx index 6db8aba7..bdaabb40 100644 --- a/packages/native/test/lib/ElementAssertion.test.tsx +++ b/packages/native/test/lib/ElementAssertion.test.tsx @@ -34,7 +34,7 @@ describe("[Unit] ElementAssertion.test.ts", () => { .toHaveMessage("Expected element to be disabled."); expect(() => test.not.toBeEnabled()) .toThrowError(AssertionError) - .toHaveMessage("Expected element to NOT be enabled."); + .toHaveMessage("Expected element NOT to be enabled."); }); }); }); @@ -59,7 +59,7 @@ describe("[Unit] ElementAssertion.test.ts", () => { .toHaveMessage("Expected element to be enabled."); expect(() => parent.not.toBeDisabled()) .toThrowError(AssertionError) - .toHaveMessage("Expected element to NOT be disabled."); + .toHaveMessage("Expected element NOT to be disabled."); }); }); @@ -83,13 +83,13 @@ describe("[Unit] ElementAssertion.test.ts", () => { .toHaveMessage("Expected element to be disabled."); expect(() => parent.not.toBeEnabled()) .toThrowError(AssertionError) - .toHaveMessage("Expected element to NOT be enabled."); + .toHaveMessage("Expected element NOT to be enabled."); expect(() => child.toBeDisabled()) .toThrowError(AssertionError) .toHaveMessage("Expected element to be disabled."); expect(() => child.not.toBeEnabled()) .toThrowError(AssertionError) - .toHaveMessage("Expected element to NOT be enabled."); + .toHaveMessage("Expected element NOT to be enabled."); }); }); }); @@ -114,7 +114,7 @@ describe("[Unit] ElementAssertion.test.ts", () => { .toHaveMessage("Expected element to be enabled."); expect(() => child.not.toBeDisabled()) .toThrowError(AssertionError) - .toHaveMessage("Expected element to NOT be disabled."); + .toHaveMessage("Expected element NOT to be disabled."); }); it("returns error for parent element", () => { @@ -124,7 +124,7 @@ describe("[Unit] ElementAssertion.test.ts", () => { .toHaveMessage("Expected element to be disabled."); expect(() => parent.not.toBeEnabled()) .toThrowError(AssertionError) - .toHaveMessage("Expected element to NOT be enabled."); + .toHaveMessage("Expected element NOT to be enabled."); }); }); }); diff --git a/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx b/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx index e28a680d..86fcb218 100644 --- a/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx +++ b/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx @@ -2,19 +2,19 @@ import { AssertionError, expect } from "@assertive-ts/core"; import { render } from "@testing-library/react-native"; import { View, Text } from "react-native"; -import { ToBeEmptyElementAssertion } from "../../src/lib/ToBeEmptyElementAssertion"; +import { ToBeEmptyAssertion } from "../../src/lib/ToBeEmptyAssertion"; -describe("[Unit] ToBeEmptyElementAssertion.test.ts", () => { - describe(".toBeEmptyElement", () => { +describe("[Unit] toBeEmptyAssertion.test.ts", () => { + describe(".toBeEmpty", () => { context("when the element is empty", () => { it("returns the assertion instance", () => { const element = render(); - const test = new ToBeEmptyElementAssertion(element.getByTestId("id")); + const test = new ToBeEmptyAssertion(element.getByTestId("id")); - expect(test.toBeEmptyElement()).toBe(test); - expect(() => test.not.toBeEmptyElement()) + expect(test.toBeEmpty()).toBe(test); + expect(() => test.not.toBeEmpty()) .toThrowError(AssertionError) - .toHaveMessage("Expected element to NOT be empty."); + .toHaveMessage("Expected element NOT to be empty."); }); }); @@ -25,10 +25,10 @@ describe("[Unit] ToBeEmptyElementAssertion.test.ts", () => { {"Not empty"} , ); - const test = new ToBeEmptyElementAssertion(element.getByTestId("id")); + const test = new ToBeEmptyAssertion(element.getByTestId("id")); - expect(test.not.toBeEmptyElement()).toBeEqual(test); - expect(() => test.toBeEmptyElement()) + expect(test.not.toBeEmpty()).toBeEqual(test); + expect(() => test.toBeEmpty()) .toThrowError(AssertionError) .toHaveMessage("Expected element to be empty."); }); From aed8344bb10a1626210ab13fbff4808067322f13 Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Thu, 20 Mar 2025 12:53:04 -0500 Subject: [PATCH 7/8] Move toBeEmptyAssertion to the element assertions --- packages/native/src/lib/ElementAssertion.ts | 29 +++++++++++- packages/native/src/lib/ToBeEmptyAssertion.ts | 44 ------------------- .../native/test/lib/ElementAssertion.test.tsx | 31 +++++++++++++ .../lib/ToBeEmptyElementAssertion.test.tsx | 37 ---------------- 4 files changed, 59 insertions(+), 82 deletions(-) delete mode 100644 packages/native/src/lib/ToBeEmptyAssertion.ts delete mode 100644 packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx diff --git a/packages/native/src/lib/ElementAssertion.ts b/packages/native/src/lib/ElementAssertion.ts index aa9d6804..283516c8 100644 --- a/packages/native/src/lib/ElementAssertion.ts +++ b/packages/native/src/lib/ElementAssertion.ts @@ -2,7 +2,7 @@ import { Assertion, AssertionError } from "@assertive-ts/core"; import { get } from "dot-prop-immutable"; import { ReactTestInstance } from "react-test-renderer"; -import { instanceToString } from "./helpers/helpers"; +import { instanceToString, isEmpty } from "./helpers/helpers"; export class ElementAssertion extends Assertion { public constructor(actual: ReactTestInstance) { @@ -66,6 +66,33 @@ export class ElementAssertion extends Assertion { }); } + /** + * Check if the element is empty. + * + * @example + * ``` + * expect(element).toBeEmpty(); + * ``` + * + * @returns the assertion instance + */ + public toBeEmpty(): this { + const error = new AssertionError({ + actual: this.actual, + message: `Expected element ${this.toString()} to be empty.`, + }); + const invertedError = new AssertionError({ + actual: this.actual, + message: `Expected element ${this.toString()} NOT to be empty.`, + }); + + return this.execute({ + assertWhen: isEmpty(this.actual.children), + error, + invertedError, + }); + } + private isElementDisabled(element: ReactTestInstance): boolean { const { type } = element; const elementType = type.toString(); diff --git a/packages/native/src/lib/ToBeEmptyAssertion.ts b/packages/native/src/lib/ToBeEmptyAssertion.ts deleted file mode 100644 index b6bd6030..00000000 --- a/packages/native/src/lib/ToBeEmptyAssertion.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Assertion, AssertionError } from "@assertive-ts/core"; -import { ReactTestInstance } from "react-test-renderer"; - -import { instanceToString, isEmpty } from "./helpers/helpers"; - -/** - * Assertion for checking if a React element is empty. - */ -export class ToBeEmptyAssertion extends Assertion { - public constructor(actual: ReactTestInstance) { - super(actual); - } - - public override toString = (): string => { - return instanceToString(this.actual); - }; - - /** - * Check if the element is empty. - * - * @example - * ``` - * expect(element).toBeEmpty(); - * ``` - * - * @returns the assertion instance - */ - public toBeEmpty(): this { - const error = new AssertionError({ - actual: this.actual, - message: `Expected element ${this.toString()} to be empty.`, - }); - const invertedError = new AssertionError({ - actual: this.actual, - message: `Expected element ${this.toString()} NOT to be empty.`, - }); - - return this.execute({ - assertWhen: isEmpty(this.actual.children), - error, - invertedError, - }); - } -} diff --git a/packages/native/test/lib/ElementAssertion.test.tsx b/packages/native/test/lib/ElementAssertion.test.tsx index bdaabb40..ce3b4419 100644 --- a/packages/native/test/lib/ElementAssertion.test.tsx +++ b/packages/native/test/lib/ElementAssertion.test.tsx @@ -3,6 +3,7 @@ import { render } from "@testing-library/react-native"; import { View, TextInput, + Text, } from "react-native"; import { ElementAssertion } from "../../src/lib/ElementAssertion"; @@ -129,4 +130,34 @@ describe("[Unit] ElementAssertion.test.ts", () => { }); }); }); + + describe(".toBeEmpty", () => { + context("when the element is empty", () => { + it("returns the assertion instance", () => { + const element = render(); + const test = new ElementAssertion(element.getByTestId("id")); + + expect(test.toBeEmpty()).toBe(test); + expect(() => test.not.toBeEmpty()) + .toThrowError(AssertionError) + .toHaveMessage("Expected element NOT to be empty."); + }); + }); + + context("when the element is NOT empty", () => { + it("throws an error", () => { + const element = render( + + {"Not empty"} + , + ); + const test = new ElementAssertion(element.getByTestId("id")); + + expect(test.not.toBeEmpty()).toBeEqual(test); + expect(() => test.toBeEmpty()) + .toThrowError(AssertionError) + .toHaveMessage("Expected element to be empty."); + }); + }); + }); }); diff --git a/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx b/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx deleted file mode 100644 index 86fcb218..00000000 --- a/packages/native/test/lib/ToBeEmptyElementAssertion.test.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import { AssertionError, expect } from "@assertive-ts/core"; -import { render } from "@testing-library/react-native"; -import { View, Text } from "react-native"; - -import { ToBeEmptyAssertion } from "../../src/lib/ToBeEmptyAssertion"; - -describe("[Unit] toBeEmptyAssertion.test.ts", () => { - describe(".toBeEmpty", () => { - context("when the element is empty", () => { - it("returns the assertion instance", () => { - const element = render(); - const test = new ToBeEmptyAssertion(element.getByTestId("id")); - - expect(test.toBeEmpty()).toBe(test); - expect(() => test.not.toBeEmpty()) - .toThrowError(AssertionError) - .toHaveMessage("Expected element NOT to be empty."); - }); - }); - - context("when the element is NOT empty", () => { - it("throws an error", () => { - const element = render( - - {"Not empty"} - , - ); - const test = new ToBeEmptyAssertion(element.getByTestId("id")); - - expect(test.not.toBeEmpty()).toBeEqual(test); - expect(() => test.toBeEmpty()) - .toThrowError(AssertionError) - .toHaveMessage("Expected element to be empty."); - }); - }); - }); -}); From 4676d93edd634fbdf77ccaced23edf62b700566f Mon Sep 17 00:00:00 2001 From: Karla Quistanchala Date: Fri, 25 Apr 2025 11:54:56 -0500 Subject: [PATCH 8/8] (refactor): Address CR comments --- packages/native/src/lib/ElementAssertion.ts | 5 +++-- packages/native/src/lib/helpers/helpers.ts | 20 +------------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/packages/native/src/lib/ElementAssertion.ts b/packages/native/src/lib/ElementAssertion.ts index 283516c8..4dbabd63 100644 --- a/packages/native/src/lib/ElementAssertion.ts +++ b/packages/native/src/lib/ElementAssertion.ts @@ -1,8 +1,9 @@ import { Assertion, AssertionError } from "@assertive-ts/core"; import { get } from "dot-prop-immutable"; +import { Children } from "react"; import { ReactTestInstance } from "react-test-renderer"; -import { instanceToString, isEmpty } from "./helpers/helpers"; +import { instanceToString } from "./helpers/helpers"; export class ElementAssertion extends Assertion { public constructor(actual: ReactTestInstance) { @@ -87,7 +88,7 @@ export class ElementAssertion extends Assertion { }); return this.execute({ - assertWhen: isEmpty(this.actual.children), + assertWhen: Children.count(this.actual.props.children) === 0, error, invertedError, }); diff --git a/packages/native/src/lib/helpers/helpers.ts b/packages/native/src/lib/helpers/helpers.ts index 6ab4db1e..631395c9 100644 --- a/packages/native/src/lib/helpers/helpers.ts +++ b/packages/native/src/lib/helpers/helpers.ts @@ -1,27 +1,9 @@ import { ReactTestInstance } from "react-test-renderer"; -/** - * Checks if a value is empty. - * - * @param value - The value to check. - * @returns `true` if the value is empty, `false` otherwise. - */ -export function isEmpty(value: unknown): boolean { - if (!value) { - return true; - } - - if (Array.isArray(value)) { - return value.length === 0; - } - - return false; -} - /** * Converts a ReactTestInstance to a string representation. * - * @param instance - The ReactTestInstance to convert. + * @param instance The ReactTestInstance to convert. * @returns A string representation of the instance. */ export function instanceToString(instance: ReactTestInstance | null): string {