Skip to content

Commit 3e77a88

Browse files
committed
Refactor: test component added to containElement tests and rendered after every it case
1 parent d757a9b commit 3e77a88

1 file changed

Lines changed: 42 additions & 17 deletions

File tree

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

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99

1010
import { ElementAssertion } from "../../src/lib/ElementAssertion";
1111

12+
import { ContainElementTestComponent } from "./fixtures/containElementTestComponent";
13+
1214
describe("[Unit] ElementAssertion.test.ts", () => {
1315
describe(".toBeDisabled", () => {
1416
context("when the element is TextInput", () => {
@@ -413,33 +415,34 @@ describe("[Unit] ElementAssertion.test.ts", () => {
413415
});
414416

415417
describe (".toContainElement", () => {
416-
const element = render(
417-
<View testID="grandParentId">
418-
<View testID="parentId">
419-
<View testID="childId" />
420-
</View>
421-
<Text testID="textId" />
422-
</View>,
423-
);
424-
425-
const container = element.getByTestId("grandParentId");
426-
const containerElementAssertion = new ElementAssertion(container);
427-
const parent = element.getByTestId("parentId");
428-
const parentElementAssertion = new ElementAssertion(parent);
429-
const child = element.getByTestId("childId");
430-
const text = element.getByTestId("textId");
431-
const textElementAssertion = new ElementAssertion(text);
432-
433418
context("when the element has children", () => {
434419
context("and the target element is found in the children's element", () => {
435420
it("returns the assertion instance", () => {
421+
const { getByText, getByTestId } = render(<ContainElementTestComponent />);
422+
423+
const container = getByTestId("grandParent");
424+
const parent = getByTestId("parent");
425+
const child = getByText("child");
426+
const text = getByText("text");
427+
428+
const containerElementAssertion = new ElementAssertion(container);
429+
const parentElementAssertion = new ElementAssertion(parent);
430+
436431
expect(containerElementAssertion.toContainElement(parent)).toBe(containerElementAssertion);
437432
expect(containerElementAssertion.toContainElement(child)).toBe(containerElementAssertion);
438433
expect(containerElementAssertion.toContainElement(text)).toBe(containerElementAssertion);
439434
expect(parentElementAssertion.toContainElement(child)).toBe(parentElementAssertion);
440435
});
441436

442437
it("throws an error for negative assertion", () => {
438+
const { getByText, getByTestId } = render(<ContainElementTestComponent />);
439+
440+
const container = getByTestId("grandParent");
441+
const parent = getByTestId("parent");
442+
const text = getByText("text");
443+
444+
const containerElementAssertion = new ElementAssertion(container);
445+
443446
expect(() => containerElementAssertion.not.toContainElement(parent))
444447
.toThrowError(AssertionError)
445448
.toHaveMessage("Expected element <View ... /> NOT to contain element <View ... />.");
@@ -451,20 +454,42 @@ describe("[Unit] ElementAssertion.test.ts", () => {
451454

452455
context("and the target element is NOT found in the children's element", () => {
453456
it("throws an error", () => {
457+
const { getByText, getByTestId } = render(<ContainElementTestComponent />);
458+
459+
const parent = getByTestId("parent");
460+
const text = getByText("text");
461+
462+
const parentElementAssertion = new ElementAssertion(parent);
463+
454464
expect(() => parentElementAssertion.toContainElement(text))
455465
.toThrowError(AssertionError)
456466
.toHaveMessage("Expected element <View ... /> to contain element <Text ... />.");
457467
});
458468

459469
it("returns the assertion instance for negative assertion", () => {
470+
const { getByText, getByTestId } = render(<ContainElementTestComponent />);
471+
472+
const container = getByTestId("grandParent");
473+
const parent = getByTestId("parent");
474+
const text = getByText("text");
475+
476+
const parentElementAssertion = new ElementAssertion(parent);
477+
460478
expect(parentElementAssertion.not.toContainElement(text)).toBeEqual(parentElementAssertion);
461479
expect(parentElementAssertion.not.toContainElement(container)).toBeEqual(parentElementAssertion);
462480
});
463481
});
464482
});
465483

466484
context("when the element does NOT have children", () => {
485+
467486
it("throws an error", () => {
487+
const { getByText, getByTestId } = render(<ContainElementTestComponent />);
488+
489+
const parent = getByTestId("parent");
490+
const text = getByText("text");
491+
492+
const textElementAssertion = new ElementAssertion(text);
468493
expect(() => textElementAssertion.toContainElement(parent))
469494
.toThrowError(AssertionError)
470495
.toHaveMessage("Expected element <Text ... /> to contain element <View ... />.");

0 commit comments

Comments
 (0)