@@ -8,7 +8,7 @@ E.g. contains({a: 1, b: 2}, 'a') // returns true
88as the object contains a key of 'a'
99
1010E.g. contains({a: 1, b: 2}, 'c') // returns false
11- as the object doesn't contains a key of 'c'
11+ as the object doesn't contain a key of 'c'
1212*/
1313
1414// Acceptance criteria:
@@ -17,19 +17,54 @@ as the object doesn't contains a key of 'c'
1717// When passed an object and a property name
1818// Then it should return true if the object contains the property, false otherwise
1919
20+ // Returns true when the property exists
21+ test ( "contains returns true if the property exists" , ( ) => {
22+ expect ( contains ( { a : 1 , b : 2 } , "a" ) ) . toBe ( true ) ;
23+ } ) ;
24+
2025// Given an empty object
2126// When passed to contains
2227// Then it should return false
23- test . todo ( "contains on empty object returns false" ) ;
28+
29+ // An empty object has no properties
30+ test ( "contains returns false for an empty object" , ( ) => {
31+ expect ( contains ( { } , "a" ) ) . toBe ( false ) ;
32+ } ) ;
2433
2534// Given an object with properties
2635// When passed to contains with an existing property name
2736// Then it should return true
2837
38+ // Finds a named property in a typical object
39+ test ( "contains returns true for another property that exists" , ( ) => {
40+ expect ( contains ( { name : "Lisa" , age : 73 } , "name" ) ) . toBe ( true ) ;
41+ } ) ;
42+
2943// Given an object with properties
30- // When passed to contains with a non-existent property name
44+ // When passed to contains with a property name that does not exist
45+ // Then it should return false
46+
47+ // Returns false for a missing property
48+ test ( "contains returns false for a property that does not exist" , ( ) => {
49+ expect ( contains ( { name : "Leo" , age : 19 } , "height" ) ) . toBe ( false ) ;
50+ } ) ;
51+
52+ // Given an object
53+ // When passed to contains with a property that does not exist
3154// Then it should return false
3255
56+ // Returns false for a property not in the object
57+ test ( "contains returns false when the property is not present" , ( ) => {
58+ expect ( contains ( { a : 1 , b : 2 } , "c" ) ) . toBe ( false ) ;
59+ } ) ;
60+
3361// Given invalid parameters like an array
3462// When passed to contains
3563// Then it should return false or throw an error
64+
65+ // Handles invalid inputs gracefully
66+ test ( "contains handles invalid parameters" , ( ) => {
67+ expect ( contains ( [ ] , "a" ) ) . toBe ( false ) ;
68+ expect ( contains ( null , "a" ) ) . toBe ( false ) ;
69+ expect ( contains ( undefined , "a" ) ) . toBe ( false ) ;
70+ } ) ;
0 commit comments