Created
June 6, 2022 13:25
-
-
Save IvanAdmaers/c018ddbab3dc033cbaebf716a973f3d4 to your computer and use it in GitHub Desktop.
hasOwnProperty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hasOwnProperty from './hasOwnProperty'; | |
const object = { | |
name: 'John', | |
get userName(): string { | |
return object.name; | |
}, | |
set setUserName(name: string) { | |
object.name = name; | |
}, | |
}; | |
describe('hasOwnProperty', () => { | |
it('should return true for an existing property', () => { | |
expect(hasOwnProperty(object, 'name')).toBe(true); | |
}); | |
it('should return true for a getter', () => { | |
expect(hasOwnProperty(object, 'userName')).toBe(true); | |
}); | |
it('should return true for a setter', () => { | |
expect(hasOwnProperty(object, 'setUserName')).toBe(true); | |
}); | |
it('should return false for a non-existent property', () => { | |
expect(hasOwnProperty(object, 'property_that_doesnt_exist')).toBe(false); | |
}); | |
it('should return false for a reference to the Object constructor', () => { | |
expect(hasOwnProperty(object, 'constructor')).toBe(false); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function checks has object own property | |
*/ | |
const hasOwnProperty = (object: object, property: string): boolean => | |
Object.prototype.hasOwnProperty.call(object, property); | |
export default hasOwnProperty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment