|
1 | 1 | const contains = require("./contains.js"); |
2 | 2 |
|
3 | | -/* |
4 | | -Implement a function called contains that checks an object contains a |
5 | | -particular property |
6 | | -
|
7 | | -E.g. contains({a: 1, b: 2}, 'a') // returns true |
8 | | -as the object contains a key of 'a' |
9 | | -
|
10 | | -E.g. contains({a: 1, b: 2}, 'c') // returns false |
11 | | -as the object doesn't contains a key of 'c' |
12 | | -*/ |
13 | | - |
14 | | -// Acceptance criteria: |
15 | | - |
16 | | -// Given a contains function |
17 | | -// When passed an object and a property name |
18 | | -// Then it should return true if the object contains the property, false otherwise |
19 | | - |
20 | | -// Given an empty object |
21 | | -// When passed to contains |
22 | | -// Then it should return false |
23 | | -test.todo("contains on empty object returns false"); |
24 | | - |
25 | | -// Given an object with properties |
26 | | -// When passed to contains with an existing property name |
27 | | -// Then it should return true |
28 | | - |
29 | | -// Given an object with properties |
30 | | -// When passed to contains with a non-existent property name |
31 | | -// Then it should return false |
32 | | - |
33 | | -// Given invalid parameters like an array |
34 | | -// When passed to contains |
35 | | -// Then it should return false or throw an error |
| 3 | +describe("contains()", () => { |
| 4 | + test("returns false for an empty object", () => { |
| 5 | + expect(contains({}, "a")).toBe(false); |
| 6 | + }); |
| 7 | + |
| 8 | + test("returns true when the property exists", () => { |
| 9 | + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + test("returns false when the property does not exist", () => { |
| 13 | + expect(contains({ a: 1, b: 2 }, "c")).toBe(false); |
| 14 | + }); |
| 15 | + |
| 16 | + test("returns false for inherited properties", () => { |
| 17 | + expect(contains({ a: 1, b: 2 }, "toString")).toBe(false); |
| 18 | + }); |
| 19 | + |
| 20 | + test("returns false when given an array with a realistic array key", () => { |
| 21 | + expect(contains(["a", "b"], 0)).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + test("returns false when given null", () => { |
| 25 | + expect(contains(null, "a")).toBe(false); |
| 26 | + }); |
| 27 | + |
| 28 | + test("supports non-string property names", () => { |
| 29 | + const obj = { 3: 12 }; |
| 30 | + expect(contains(obj, 3)).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + test("supports empty string as a property name", () => { |
| 34 | + const obj = { "": 99 }; |
| 35 | + expect(contains(obj, "")).toBe(true); |
| 36 | + }); |
| 37 | +}); |
0 commit comments