Skip to content

Latest commit

 

History

History
79 lines (60 loc) · 1.56 KB

SPEC.md

File metadata and controls

79 lines (60 loc) · 1.56 KB

TOC

sync

Is a function.

// const sync = require('sync-decorator');
  expect(sync).to.be.a('function')

Throws TypeError if descriptor argument is not object.

expect(() => sync({}, '', 42)).to.throw(TypeError)

Throws TypeError if value property of descriptor argument is not function.

expect(() => sync({}, '', {})).to.throw(TypeError)

@sync

Appends Sync suffix to name of decorated method.

/*
  class Test {
    @sync
    method() {
      const call = { arguments: [...arguments], this: this };
      this.calls = 1 + (this.calls || 0);
      return delay().then(() => call);
    }
  }
  */
  expect(new Test().method.name).to.equal('methodSync')

Wraps result of decorated method into promise.

expect(new Test().method()).to.be.instanceof(Promise)

Calls decorated method with arguments passed.

new Test().method(42, 'test').then(call => expect(call.arguments).to.include.members([42, 'test']))

Calls decorated method with this binding.

new Test().method(42, 'test').then(call => expect(call.this).to.be.instanceof(Test))

Does not call decorated method again until previous call finishes.

const test = new Test();
const promise = test.method().then(() => expect(test.calls).to.equal(1));
test.method();
return promise;

Calls decorated method again when previous call has finished.

const test = new Test();
test.method();
return test.method().then(() => expect(test.calls).to.equal(2));