Skip to content

Commit 9f18dfb

Browse files
kdquistanchalalopenchi
authored andcommitted
Add tests for toContainElement()
1 parent c9c349e commit 9f18dfb

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
@@ -271,4 +271,50 @@ describe("[Unit] ElementAssertion.test.ts", () => {
271271
});
272272
});
273273
});
274+
275+
describe (".toContainElement", () => {
276+
const element = render(
277+
<View testID="grandParentId">
278+
<View testID="parentId">
279+
<View testID="childId" />
280+
</View>
281+
<Text testID="textId" />
282+
</View>,
283+
);
284+
285+
const container = element.getByTestId("grandParentId");
286+
const containerElementAssertion = new ElementAssertion(container);
287+
const parent = element.getByTestId("parentId");
288+
const parentElementAssertion = new ElementAssertion(parent);
289+
const child = element.getByTestId("childId");
290+
const text = element.getByTestId("textId");
291+
292+
context("when the container element contains the target element", () => {
293+
it("returns the assertion instance", () => {
294+
expect(containerElementAssertion.toContainElement(parent)).toBe(containerElementAssertion);
295+
expect(containerElementAssertion.toContainElement(child)).toBe(containerElementAssertion);
296+
expect(containerElementAssertion.toContainElement(text)).toBe(containerElementAssertion);
297+
expect(parentElementAssertion.toContainElement(child)).toBe(parentElementAssertion);
298+
});
299+
300+
it("returns the assertion instance for negated assertions when the target element is not contained", () => {
301+
expect(parentElementAssertion.not.toContainElement(text)).toBe(parentElementAssertion);
302+
expect(parentElementAssertion.not.toContainElement(container)).toBe(parentElementAssertion);
303+
});
304+
});
305+
306+
context("when the container element does NOT contain the target element", () => {
307+
it("throws an error", () => {
308+
expect(() => containerElementAssertion.not.toContainElement(parent))
309+
.toThrowError(AssertionError)
310+
.toHaveMessage("Expected element <View ... /> NOT to contain element <View ... />.");
311+
expect(() => containerElementAssertion.not.toContainElement(text))
312+
.toThrowError(AssertionError)
313+
.toHaveMessage("Expected element <View ... /> NOT to contain element <Text ... />.");
314+
expect(() => parentElementAssertion.toContainElement(text))
315+
.toThrowError(AssertionError)
316+
.toHaveMessage("Expected element <View ... /> to contain element <Text ... />.");
317+
});
318+
});
319+
});
274320
});

0 commit comments

Comments
 (0)