|
1 | 1 | const contains = require("./contains.js"); |
2 | 2 |
|
| 3 | + |
3 | 4 | /* |
4 | 5 | Implement a function called contains that checks an object contains a |
5 | 6 | particular property |
6 | 7 |
|
| 8 | +
|
7 | 9 | E.g. contains({a: 1, b: 2}, 'a') // returns true |
8 | 10 | as the object contains a key of 'a' |
9 | 11 |
|
| 12 | +
|
10 | 13 | E.g. contains({a: 1, b: 2}, 'c') // returns false |
11 | 14 | as the object doesn't contains a key of 'c' |
12 | 15 | */ |
13 | 16 |
|
| 17 | + |
14 | 18 | // Acceptance criteria: |
15 | 19 |
|
| 20 | + |
16 | 21 | // Given a contains function |
17 | 22 | // When passed an object and a property name |
18 | 23 | // Then it should return true if the object contains the property, false otherwise |
19 | 24 |
|
| 25 | + |
20 | 26 | // Given an empty object |
21 | 27 | // When passed to contains |
22 | 28 | // Then it should return false |
23 | | -test.todo("contains on empty object returns false"); |
| 29 | +test("contains on empty object returns false", () => { |
| 30 | + expect(contains({}, "a")).toBe(false); |
| 31 | +}); |
| 32 | + |
24 | 33 |
|
25 | 34 | // Given an object with properties |
26 | 35 | // When passed to contains with an existing property name |
27 | 36 | // Then it should return true |
| 37 | +test("contains returns true for existing property", () => { |
| 38 | + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); |
| 39 | +}); |
| 40 | + |
28 | 41 |
|
29 | 42 | // Given an object with properties |
30 | 43 | // When passed to contains with a non-existent property name |
31 | 44 | // Then it should return false |
| 45 | +test("contains returns false for non-existent property", () => { |
| 46 | + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); |
| 47 | +}); |
| 48 | + |
32 | 49 |
|
33 | 50 | // Given invalid parameters like an array |
34 | 51 | // When passed to contains |
35 | 52 | // Then it should return false or throw an error |
| 53 | +test("contains with invalid parameters returns false", () => { |
| 54 | + expect(contains([], "a")).toBe(false); |
| 55 | +}); |
0 commit comments