Syntax previewClick to edit
1234
export default function myBind(fn, thisArg, ...boundArgs) {
throw new Error('Not implemented');
}
Syntax previewClick to edit
123456789101112131415161718192021222324252627282930313233343536373839404142434445
import myBind from './file';
describe('myBind', () => {
test('binds this for normal calls', () => {
function getX() { return this.x; }
const obj = { x: 42 };
const bound = myBind(getX, obj);
expect(bound()).toBe(42);
});
test('supports partial application', () => {
function add(a, b, c) { return a + b + c; }
const add5 = myBind(add, null, 2, 3);
expect(add5(10)).toBe(15);
});
test('when called with new, ignores thisArg and uses the new instance', () => {
function Person(name) { this.name = name; }
Person.prototype.say = function() { return this.name; };
const BoundPerson = myBind(Person, { name: 'SHOULD_NOT_USE' });
const p = new BoundPerson('Ada');
expect(p.name).toBe('Ada');
expect(p.say()).toBe('Ada');
});
test('instances created via new boundFn() are instanceof original fn', () => {
function Person(name) { this.name = name; }
const BoundPerson = myBind(Person, { name: 'X' });
const p = new BoundPerson('Ada');
expect(p instanceof Person).toBe(true);
expect(p instanceof BoundPerson).toBe(true);
});
test('constructor return object behavior is preserved', () => {
function Factory(name) { this.name = name; return { ok: true, name }; }
const BoundFactory = myBind(Factory, { name: 'X' });
const res = new BoundFactory('Ada');
expect(res).toEqual({ ok: true, name: 'Ada' });
});
});
Run tests to see results.