Skip to content

Commit

Permalink
Merge pull request #1 from r24y/feat/no-proxy
Browse files Browse the repository at this point in the history
New: Add `no-proxy` version for usage in Node 4
  • Loading branch information
ryaninvents authored Oct 17, 2017
2 parents 86375e4 + a1982a6 commit 1294544
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/__tests__/no-proxy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const anticipatedCall = require('../no-proxy');

describe('no-proxy', () => {
describe('nextCall', () => {
test('should delay resolution until first call', () => {
const spy = jest.fn();
const myFn = anticipatedCall(spy);

const promise = myFn.anticipated.nextCall
.then(() => {
expect(spy).toHaveBeenCalledTimes(1);
});
expect(spy).not.toHaveBeenCalled();
myFn();
expect(spy).toHaveBeenCalledTimes(1);
});
test('should return a new Promise', () => {
const myFn = anticipatedCall();
const promise1 = myFn.anticipated.nextCall;
const promise2 = myFn.anticipated.nextCall;
expect(promise1).not.toBe(promise2);
});
test('should handle subsequent calls as expected', () => {
const spy = jest.fn();
const myFn = anticipatedCall(spy);

const promise1 = myFn.anticipated.nextCall;

myFn();

return promise1.then(() => {
expect(spy).toHaveBeenCalledTimes(1);

const promise2 = myFn.anticipated.nextCall;
myFn();
return promise2;
}).then(() => {
expect(spy).toHaveBeenCalledTimes(2);
})
})
})
})
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ function anticipateCall(fn = noop) {
return new Proxy(fn, new Anticipated());
}

module.exports = anticipateCall;
module.exports = anticipateCall;
module.exports.Anticipated = Anticipated;
module.exports.noop = noop;
12 changes: 12 additions & 0 deletions src/no-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const {Anticipated, noop} = require('./index');

function anticipateCallWithoutProxy(fn) {
const anticipated = new Anticipated();
function proxiedCall(...args) {
return anticipated.apply(fn, this, args);
}
proxiedCall.anticipated = anticipated;
return proxiedCall;
}

module.exports = anticipateCallWithoutProxy;

0 comments on commit 1294544

Please sign in to comment.