Syntax previewClick to edit
1234
export default function myInstanceOf(obj, Constructor) {
throw new Error('Not implemented');
}
Syntax previewClick to edit
12345678910111213141516171819202122232425262728293031323334353637383940414243
import myInstanceOf from './file';
describe('myInstanceOf', () => {
test('returns true when Constructor.prototype is in the prototype chain', () => {
function Person() {}
const p = new Person();
expect(myInstanceOf(p, Person)).toBe(true);
});
test('works for arrays', () => {
expect(myInstanceOf([], Array)).toBe(true);
expect(myInstanceOf({}, Array)).toBe(false);
});
test('works for functions', () => {
function f() {}
expect(myInstanceOf(f, Function)).toBe(true);
});
test('returns false for primitives and null/undefined', () => {
expect(myInstanceOf(1, Number)).toBe(false);
expect(myInstanceOf('a', String)).toBe(false);
expect(myInstanceOf(true, Boolean)).toBe(false);
expect(myInstanceOf(null, Object)).toBe(false);
expect(myInstanceOf(undefined, Object)).toBe(false);
});
test('handles Object.create(null) correctly (null prototype chain)', () => {
const o = Object.create(null);
expect(myInstanceOf(o, Object)).toBe(false);
});
test('throws if Constructor is not a function', () => {
try {
myInstanceOf({}, 123);
expect(true).toBe(false);
} catch (e) {
expect(!!e).toBe(true);
expect(e.name).toBe('TypeError');
}
});
});
Run tests to see results.