Skip to content

Instantly share code, notes, and snippets.

@IvanAdmaers
Created June 6, 2022 13:25
Show Gist options
  • Save IvanAdmaers/c018ddbab3dc033cbaebf716a973f3d4 to your computer and use it in GitHub Desktop.
Save IvanAdmaers/c018ddbab3dc033cbaebf716a973f3d4 to your computer and use it in GitHub Desktop.
hasOwnProperty
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 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