Skip to content

Commit f261e19

Browse files
kdquistanchalalopenchi
authored andcommitted
Add tests for toContainElement()
1 parent f61b150 commit f261e19

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,50 @@ describe("[Unit] ElementAssertion.test.ts", () => {
313313
});
314314
});
315315
});
316+
317+
describe (".toContainElement", () => {
318+
const element = render(
319+
<View testID="grandParentId">
320+
<View testID="parentId">
321+
<View testID="childId" />
322+
</View>
323+
<Text testID="textId" />
324+
</View>,
325+
);
326+
327+
const container = element.getByTestId("grandParentId");
328+
const containerElementAssertion = new ElementAssertion(container);
329+
const parent = element.getByTestId("parentId");
330+
const parentElementAssertion = new ElementAssertion(parent);
331+
const child = element.getByTestId("childId");
332+
const text = element.getByTestId("textId");
333+
334+
context("when the container element contains the target element", () => {
335+
it("returns the assertion instance", () => {
336+
expect(containerElementAssertion.toContainElement(parent)).toBe(containerElementAssertion);
337+
expect(containerElementAssertion.toContainElement(child)).toBe(containerElementAssertion);
338+
expect(containerElementAssertion.toContainElement(text)).toBe(containerElementAssertion);
339+
expect(parentElementAssertion.toContainElement(child)).toBe(parentElementAssertion);
340+
});
341+
342+
it("returns the assertion instance for negated assertions when the target element is not contained", () => {
343+
expect(parentElementAssertion.not.toContainElement(text)).toBe(parentElementAssertion);
344+
expect(parentElementAssertion.not.toContainElement(container)).toBe(parentElementAssertion);
345+
});
346+
});
347+
348+
context("when the container element does NOT contain the target element", () => {
349+
it("throws an error", () => {
350+
expect(() => containerElementAssertion.not.toContainElement(parent))
351+
.toThrowError(AssertionError)
352+
.toHaveMessage("Expected element <View ... /> NOT to contain element <View ... />.");
353+
expect(() => containerElementAssertion.not.toContainElement(text))
354+
.toThrowError(AssertionError)
355+
.toHaveMessage("Expected element <View ... /> NOT to contain element <Text ... />.");
356+
expect(() => parentElementAssertion.toContainElement(text))
357+
.toThrowError(AssertionError)
358+
.toHaveMessage("Expected element <View ... /> to contain element <Text ... />.");
359+
});
360+
});
361+
});
316362
});

0 commit comments

Comments
 (0)