Skip to content

Commit 279a50c

Browse files
Add tests for toHaveProp
1 parent ee0261b commit 279a50c

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,62 @@ describe("[Unit] ElementAssertion.test.ts", () => {
317317
});
318318
});
319319
});
320+
321+
describe(".toHaveProp", () => {
322+
context("when the element contains the target prop", () => {
323+
it("returns the assertion instance", () => {
324+
const element = render(
325+
<View testID="id" accessibilityLabel="label" />,
326+
);
327+
const test = new ElementAssertion(element.getByTestId("id"));
328+
329+
expect(test.toHaveProp("accessibilityLabel")).toBe(test);
330+
expect(() => test.not.toHaveProp("accessibilityLabel"))
331+
.toThrowError(AssertionError)
332+
.toHaveMessage("Expected element <View ... /> NOT to have prop 'accessibilityLabel'.");
333+
});
334+
});
335+
336+
context("when the element does NOT contain the target prop", () => {
337+
it("throws an error", () => {
338+
const element = render(
339+
<View testID="id" />,
340+
);
341+
const test = new ElementAssertion(element.getByTestId("id"));
342+
343+
expect(test.not.toHaveProp("accessibilityLabel")).toBeEqual(test);
344+
expect(() => test.toHaveProp("accessibilityLabel"))
345+
.toThrowError(AssertionError)
346+
.toHaveMessage("Expected element <View ... /> to have prop 'accessibilityLabel'.");
347+
});
348+
});
349+
350+
context("when the element contains the target prop with a specific value", () => {
351+
it("returns the assertion instance", () => {
352+
const element = render(
353+
<View testID="id" accessibilityLabel="label" />,
354+
);
355+
const test = new ElementAssertion(element.getByTestId("id"));
356+
357+
expect(test.toHaveProp("accessibilityLabel", "label")).toBe(test);
358+
expect(() => test.not.toHaveProp("accessibilityLabel", "label"))
359+
.toThrowError(AssertionError)
360+
.toHaveMessage("Expected element <View ... /> NOT to have prop 'accessibilityLabel' with value 'label'.");
361+
});
362+
});
363+
364+
context("when the element does NOT contain the target prop with a specific value", () => {
365+
it("throws an error", () => {
366+
const element = render(
367+
<View testID="id" accessibilityLabel="new-label" />,
368+
);
369+
const test = new ElementAssertion(element.getByTestId("id"));
370+
371+
expect(test.not.toHaveProp("accessibilityLabel", "label")).toBeEqual(test);
372+
expect(() => test.toHaveProp("accessibilityLabel", "label"))
373+
.toThrowError(AssertionError)
374+
.toHaveMessage("Expected element <View ... /> to have prop 'accessibilityLabel' with value 'label'.");
375+
});
376+
});
377+
});
320378
});

0 commit comments

Comments
 (0)