Skip to content

Commit

Permalink
Add toContainElement assertion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matycarolina committed Jul 23, 2024
1 parent a5c5cc3 commit ddc1682
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions packages/dom/test/unit/lib/ElementAssertion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ function TestComponent(): ReactElement {
);
}

function ContainsTestComponent(): ReactElement {
return (
<span data-testid="grandparent">
<span data-testid="parent">
<span data-testid="child"></span>
</span>
<svg data-testid="svg-element"></svg>
</span>
);
}

describe("[Unit] ElementAssertion.test.ts", () => {
describe(".toBeInTheDocument", () => {
context("when the element is in the document", () => {
Expand Down Expand Up @@ -42,4 +53,81 @@ describe("[Unit] ElementAssertion.test.ts", () => {
});
});
});

describe(".toContainElement", () => {
context("when the ancestor element contains the descendant element", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(<ContainsTestComponent/>);

const grandparent = getByTestId("grandparent");
const parent = getByTestId("parent");
const child = getByTestId("child");
const svgElement = getByTestId("svg-element");

const grandparentTest = new ElementAssertion(grandparent);
const parentTest = new ElementAssertion(parent);

expect(grandparentTest.toContainElement(parent));
expect(grandparentTest.toContainElement(child));
expect(grandparentTest.toContainElement(svgElement));
expect(parentTest.toContainElement(child));

expect(() => grandparentTest.not.toContainElement(parent))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");

expect(() => grandparentTest.not.toContainElement(child))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");

expect(() => grandparentTest.not.toContainElement(svgElement))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");

expect(() => parentTest.not.toContainElement(child))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to NOT contain the element");
});
});

context("when the descendant element does not contain the ancestor element", () => {
it("returns the assertion instance", () => {
const { getByTestId } = render(<ContainsTestComponent/>);

const grandparent = getByTestId("grandparent");
const parent = getByTestId("parent");
const child = getByTestId("child");
const svgElement = getByTestId("svg-element");

const parentTest = new ElementAssertion(parent);
const childTest = new ElementAssertion(child);

expect(parentTest.not.toContainElement(grandparent));
expect(parentTest.not.toContainElement(svgElement));
expect(childTest.not.toContainElement(grandparent));
expect(childTest.not.toContainElement(parent));
expect(childTest.not.toContainElement(svgElement));

expect(() => parentTest.toContainElement(grandparent))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to contain the element");

expect(() => parentTest.toContainElement(svgElement))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to contain the element");

expect(() => childTest.toContainElement(grandparent))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to contain the element");

expect(() => childTest.toContainElement(parent))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to contain the element");

expect(() => childTest.toContainElement(svgElement))
.toThrowError(AssertionError)
.toHaveMessage("Expected the container to contain the element");
});
});
});
});

0 comments on commit ddc1682

Please sign in to comment.