-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back custom Promise class to not introduce breaking changes
- Loading branch information
1 parent
72967a0
commit 89ddc3c
Showing
6 changed files
with
232 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const deprecationLog = (className, methodName) => { | ||
console.warn(`ZOOKEEPER LOG: ${className}::${methodName} is being deprecated!`); | ||
}; | ||
|
||
exports = module.exports = { | ||
deprecationLog, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable no-param-reassign */ | ||
/* eslint-disable no-return-assign */ | ||
|
||
const { deprecationLog } = require('./helper'); | ||
|
||
class ZkPromise extends Promise { | ||
/** | ||
* @deprecated | ||
*/ | ||
get(propertyName) { | ||
deprecationLog(ZkPromise.name, 'get'); | ||
return this.then(object => object[propertyName]); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
put(propertyName, value) { | ||
deprecationLog(ZkPromise.name, 'put'); | ||
return this.then(object => object[propertyName] = value); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
call(functionName, ...args) { | ||
deprecationLog(ZkPromise.name, 'call'); | ||
return this.then(object => object[functionName](...args)); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
addCallback(callback) { | ||
deprecationLog(ZkPromise.name, 'addCallback'); | ||
return this.then(callback); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
addErrback(callback) { | ||
deprecationLog(ZkPromise.name, 'addErrback'); | ||
return this.catch(callback); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
addBoth(callback) { | ||
deprecationLog(ZkPromise.name, 'addBoth'); | ||
return this.then(callback, callback); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
addCallbacks(callback, errback) { | ||
deprecationLog(ZkPromise.name, 'addCallbacks'); | ||
return this.then(callback, errback); | ||
} | ||
} | ||
|
||
exports = module.exports = ZkPromise; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const sinon = require('sinon'); | ||
const test = require('tape'); | ||
const { deprecationLog } = require('../../lib/helper'); | ||
|
||
class Test { | ||
static method() { | ||
deprecationLog(Test.name, 'method'); | ||
} | ||
} | ||
|
||
test('deprecation log is called with proper arguments', (t) => { | ||
const deprecationLogStub = sinon.stub(); | ||
t.plan(3); | ||
|
||
deprecationLogStub(Test.name, 'method'); | ||
|
||
t.equal(deprecationLogStub.callCount, 1); | ||
t.equal(deprecationLogStub.getCall(0).args[0], 'Test'); | ||
t.equal(deprecationLogStub.getCall(0).args[1], 'method'); | ||
}); | ||
|
||
test('deprecation log gives proper message', (t) => { | ||
const consoleStub = sinon.stub(console, 'warn'); | ||
t.plan(2); | ||
|
||
Test.method(); | ||
|
||
t.equal(consoleStub.callCount, 1); | ||
t.equal(consoleStub.getCall(0).args[0], 'ZOOKEEPER LOG: Test::method is being deprecated!'); | ||
|
||
consoleStub.restore(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
const sinon = require('sinon'); | ||
const test = require('tape'); | ||
|
||
const ZkPromise = require('../../lib/promise'); | ||
|
||
test('ZkPromise get', (t) => { | ||
t.plan(1); | ||
|
||
const expected = 'value'; | ||
const promise = ZkPromise.resolve({ | ||
property: expected, | ||
}); | ||
|
||
promise.get('property').then((actual) => { | ||
t.equal(actual, expected); | ||
}); | ||
}); | ||
|
||
test('ZkPromise put', (t) => { | ||
t.plan(1); | ||
|
||
const expected = 'value'; | ||
const promise = ZkPromise.resolve({}); | ||
|
||
promise.put('property', expected).then((actual) => { | ||
t.equal(actual, expected); | ||
}); | ||
}); | ||
|
||
test('ZkPromise call', (t) => { | ||
t.plan(1); | ||
|
||
const promise = ZkPromise.resolve({ | ||
multiply: (a, b) => a * b, | ||
}); | ||
|
||
promise.call('multiply', 2, 3).then((actual) => { | ||
t.equal(actual, 2 * 3); | ||
}); | ||
}); | ||
|
||
test('ZkPromise addCallback', (t) => { | ||
t.plan(3); | ||
|
||
const callback = sinon.stub().callsFake(value => value * 2); | ||
const promise = ZkPromise.resolve(10); | ||
|
||
promise.addCallback(callback).then((actual) => { | ||
t.equal(actual, 20); | ||
t.equal(callback.callCount, 1); | ||
t.equal(callback.getCall(0).args[0], 10); | ||
}); | ||
}); | ||
|
||
test('ZkPromise addErrback', (t) => { | ||
t.plan(3); | ||
|
||
const callback = sinon.stub().callsFake(value => value * 2); | ||
const promise = ZkPromise.reject(10); | ||
|
||
promise.addErrback(callback).then((actual) => { | ||
t.equal(actual, 20); | ||
t.equal(callback.callCount, 1); | ||
t.equal(callback.getCall(0).args[0], 10); | ||
}); | ||
}); | ||
|
||
test('ZkPromise addBoth', (t) => { | ||
t.plan(7); | ||
|
||
const callback = sinon.stub().callsFake(value => value * 2); | ||
const errback = sinon.stub().callsFake(() => { | ||
throw new Error('errback!'); | ||
}); | ||
// eslint-disable-next-line max-len | ||
const promise = succeeds => new ZkPromise((resolve, reject) => (succeeds ? resolve(10) : reject(10))); | ||
|
||
promise(true).addBoth(callback) | ||
.then((actual) => { | ||
t.equal(actual, 20); | ||
t.equal(callback.callCount, 1); | ||
t.equal(callback.getCall(0).args[0], 10); | ||
return promise(false).addBoth(errback); | ||
}) | ||
.catch((actual) => { | ||
t.true(actual instanceof Error); | ||
t.equal(actual.message, 'errback!'); | ||
t.equal(errback.callCount, 1); | ||
t.equal(errback.getCall(0).args[0], 10); | ||
}); | ||
}); | ||
|
||
test('ZkPromise addCallbacks', (t) => { | ||
t.plan(9); | ||
|
||
const callback = sinon.stub().callsFake(value => value * 2); | ||
const errback = sinon.stub().callsFake(() => { | ||
throw new Error('errback!'); | ||
}); | ||
// eslint-disable-next-line max-len | ||
const promise = succeeds => new ZkPromise((resolve, reject) => (succeeds ? resolve(10) : reject(10))); | ||
|
||
promise(true).addCallbacks(callback, errback) | ||
.then((actual) => { | ||
t.equal(actual, 20); | ||
t.equal(callback.callCount, 1); | ||
t.equal(callback.getCall(0).args[0], 10); | ||
t.equal(errback.callCount, 0); | ||
callback.resetHistory(); | ||
errback.resetHistory(); | ||
return promise(false).addCallbacks(callback, errback); | ||
}) | ||
.catch((actual) => { | ||
t.true(actual instanceof Error); | ||
t.equal(actual.message, 'errback!'); | ||
t.equal(callback.callCount, 0); | ||
t.equal(errback.callCount, 1); | ||
t.equal(errback.getCall(0).args[0], 10); | ||
}); | ||
}); |