Skip to content

Commit 8328f48

Browse files
kdquistanchalalopenchi
authored andcommitted
Add tests for toContainElement()
1 parent b8d0042 commit 8328f48

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
@@ -311,4 +311,50 @@ describe("[Unit] ElementAssertion.test.ts", () => {
311311
});
312312
});
313313
});
314+
315+
describe (".toContainElement", () => {
316+
const element = render(
317+
<View testID="grandParentId">
318+
<View testID="parentId">
319+
<View testID="childId" />
320+
</View>
321+
<Text testID="textId" />
322+
</View>,
323+
);
324+
325+
const container = element.getByTestId("grandParentId");
326+
const containerElementAssertion = new ElementAssertion(container);
327+
const parent = element.getByTestId("parentId");
328+
const parentElementAssertion = new ElementAssertion(parent);
329+
const child = element.getByTestId("childId");
330+
const text = element.getByTestId("textId");
331+
332+
context("when the container element contains the target element", () => {
333+
it("returns the assertion instance", () => {
334+
expect(containerElementAssertion.toContainElement(parent)).toBe(containerElementAssertion);
335+
expect(containerElementAssertion.toContainElement(child)).toBe(containerElementAssertion);
336+
expect(containerElementAssertion.toContainElement(text)).toBe(containerElementAssertion);
337+
expect(parentElementAssertion.toContainElement(child)).toBe(parentElementAssertion);
338+
});
339+
340+
it("returns the assertion instance for negated assertions when the target element is not contained", () => {
341+
expect(parentElementAssertion.not.toContainElement(text)).toBe(parentElementAssertion);
342+
expect(parentElementAssertion.not.toContainElement(container)).toBe(parentElementAssertion);
343+
});
344+
});
345+
346+
context("when the container element does NOT contain the target element", () => {
347+
it("throws an error", () => {
348+
expect(() => containerElementAssertion.not.toContainElement(parent))
349+
.toThrowError(AssertionError)
350+
.toHaveMessage("Expected element <View ... /> NOT to contain element <View ... />.");
351+
expect(() => containerElementAssertion.not.toContainElement(text))
352+
.toThrowError(AssertionError)
353+
.toHaveMessage("Expected element <View ... /> NOT to contain element <Text ... />.");
354+
expect(() => parentElementAssertion.toContainElement(text))
355+
.toThrowError(AssertionError)
356+
.toHaveMessage("Expected element <View ... /> to contain element <Text ... />.");
357+
});
358+
});
359+
});
314360
});

0 commit comments

Comments
 (0)