Skip to content

Commit 663412b

Browse files
author
JorvanW
committed
Added code and tests for contains.js
1 parent 5c2b4b5 commit 663412b

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
function contains() {}
1+
function contains(object, property) {
2+
if (Array.isArray(object)) {
3+
throw new Error("Invalid parameter");
4+
}
5+
6+
return property in object;
7+
}
28

39
module.exports = contains;

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,39 @@ 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("contains a passed object should return true, false otherwise",() => {
20+
expect(contains({a: 1, b: 2}, 2)).toEqual(false);
21+
expect(contains({a: 1, b: 2}, 'b')).toEqual(true);
22+
});
1923

2024
// Given an empty object
2125
// When passed to contains
2226
// Then it should return false
23-
test.todo("contains on empty object returns false");
27+
test("contains an empty object, returns false",() => {
28+
expect(contains({})).toEqual(false);
29+
});
2430

2531
// Given an object with properties
2632
// When passed to contains with an existing property name
2733
// Then it should return true
34+
test("contains object with properties, return true when passed with existing property name",() => {
35+
expect(contains({name: 'alice'}, 'name')).toEqual(true);
36+
});
37+
2838

2939
// Given an object with properties
3040
// When passed to contains with a non-existent property name
3141
// Then it should return false
42+
test("contains a passed object with non-existent property names return false",() => {
43+
expect(contains({a: 1, b: 2}, 'c')).toEqual(false);
44+
});
45+
3246

3347
// Given invalid parameters like an array
3448
// When passed to contains
3549
// Then it should return false or throw an error
50+
test("contains passed invalid parameters like an array to throw error", () => {
51+
expect(() => contains(['horse', 'dog', 'fish'], 'fish'))
52+
.toThrow("Invalid parameter");
53+
});
54+

0 commit comments

Comments
 (0)