Skip to content

Commit 5ee5409

Browse files
committed
Add jest tests covering all contains() acceptance criteria
1 parent 1743311 commit 5ee5409

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
function contains() {}
1+
function contains(obj, prop) {
2+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
3+
return false;
4+
}
5+
6+
return obj.hasOwnProperty(prop);
7+
}
28

39
module.exports = contains;
10+

‎Sprint-2/implement/contains.test.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,47 @@ as the object doesn't contains a key of 'c'
1616
// Given a contains function
1717
// When passed an object and a property name
1818
// Then it should return true if the object contains the property, false otherwise
19+
test("returns true when object contains the property", () => {
20+
const obj = { a: 1, b: 2 };
21+
expect(contains(obj, "a")).toBe(true);
22+
});
23+
24+
test("returns false when object does not contain the property", () => {
25+
const obj = { a: 1, b: 2 };
26+
expect(contains(obj, "c")).toBe(false);
27+
});
28+
1929

2030
// Given an empty object
2131
// When passed to contains
2232
// Then it should return false
23-
test.todo("contains on empty object returns false");
33+
// test.todo("contains on empty object returns false");
34+
test("contains on empty object returns false", () => {
35+
expect(contains({}, "a")).toBe(false);
36+
});
2437

2538
// Given an object with properties
2639
// When passed to contains with an existing property name
2740
// Then it should return true
41+
test("returns true when object contains the property", () => {
42+
const obj = { a: 1, b: 2 };
43+
expect(contains(obj, "a")).toBe(true);
44+
});
2845

2946
// Given an object with properties
3047
// When passed to contains with a non-existent property name
3148
// Then it should return false
49+
test("returns false when object does not contain the property", () => {
50+
const obj = { a: 1, b: 2 };
51+
expect(contains(obj, "c")).toBe(false);
52+
});
3253

3354
// Given invalid parameters like an array
3455
// When passed to contains
3556
// Then it should return false or throw an error
57+
test("returns false for invalid parameters like an array", () => {
58+
expect(contains([], "a")).toBe(false);
59+
expect(contains(null, "a")).toBe(false);
60+
expect(contains(123, "a")).toBe(false);
61+
expect(contains("hello", "a")).toBe(false);
62+
});

0 commit comments

Comments
 (0)