1+ import { describe , expect , it } from 'vitest'
2+
3+ import { isKeyOf } from './isKeyOf'
4+
5+ describe ( 'isKeyOf' , ( ) => {
6+ it ( 'should return true when key exists in object' , ( ) => {
7+ const obj = { a : 1 , b : 2 , c : 3 }
8+ expect ( isKeyOf ( obj , 'a' ) ) . toBe ( true )
9+ expect ( isKeyOf ( obj , 'b' ) ) . toBe ( true )
10+ expect ( isKeyOf ( obj , 'c' ) ) . toBe ( true )
11+ } )
12+
13+ it ( 'should return false when key does not exist in object' , ( ) => {
14+ const obj = { a : 1 , b : 2 , c : 3 }
15+ expect ( isKeyOf ( obj , 'd' ) ) . toBe ( false )
16+ expect ( isKeyOf ( obj , 'e' ) ) . toBe ( false )
17+ expect ( isKeyOf ( obj , 'foo' ) ) . toBe ( false )
18+ } )
19+
20+ it ( 'should work with numeric keys' , ( ) => {
21+ const obj = { 1 : 'one' , 2 : 'two' }
22+ expect ( isKeyOf ( obj , 1 ) ) . toBe ( true )
23+ expect ( isKeyOf ( obj , 2 ) ) . toBe ( true )
24+ expect ( isKeyOf ( obj , 3 ) ) . toBe ( false )
25+ } )
26+
27+ it ( 'should work with symbol keys' , ( ) => {
28+ const symKey = Symbol ( 'key' )
29+ const obj = { [ symKey ] : 'value' }
30+ expect ( isKeyOf ( obj , symKey ) ) . toBe ( true )
31+ expect ( isKeyOf ( obj , Symbol ( 'another' ) ) ) . toBe ( false )
32+ } )
33+
34+ it ( 'should work with inherited properties' , ( ) => {
35+ const proto = { a : 1 }
36+ const obj = Object . create ( proto )
37+ obj . b = 2
38+
39+ expect ( isKeyOf ( obj , 'a' ) ) . toBe ( true )
40+ expect ( isKeyOf ( obj , 'b' ) ) . toBe ( true )
41+ expect ( isKeyOf ( obj , 'toString' ) ) . toBe ( true )
42+ } )
43+ } )
0 commit comments