From c700d7c3a1cc26fcbf2a9bb89c8b38b7b7562046 Mon Sep 17 00:00:00 2001 From: dimaver Date: Wed, 20 Nov 2019 12:00:37 +0300 Subject: [PATCH] added tests against config.AsyncResult --- .eslintignore | 1 + test/test-helpres.js | 9 ++++++ test/unit/asyncResult.spec.js | 10 +++++++ test/unit/utils.spec.js | 54 ++++++++++++++++++++++++++++++----- 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 .eslintignore create mode 100644 test/test-helpres.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..65e3ba2 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +test/ diff --git a/test/test-helpres.js b/test/test-helpres.js new file mode 100644 index 0000000..f3fd08e --- /dev/null +++ b/test/test-helpres.js @@ -0,0 +1,9 @@ +import { AsyncResult } from '../'; + +export const OwnAsyncResult = function() { + AsyncResult.apply(this, arguments); +}; +OwnAsyncResult.prototype = Object.create(AsyncResult.prototype); +OwnAsyncResult.prototype.constructor = AsyncResult; +OwnAsyncResult.success = AsyncResult.success; +OwnAsyncResult.fail = AsyncResult.fail; diff --git a/test/unit/asyncResult.spec.js b/test/unit/asyncResult.spec.js index ef93017..4780201 100644 --- a/test/unit/asyncResult.spec.js +++ b/test/unit/asyncResult.spec.js @@ -1,4 +1,5 @@ import { AsyncResult } from '../..'; +import { OwnAsyncResult } from '../test-helpres'; describe('# AsyncResult', function() { describe('when created with value and without error', function() { @@ -274,5 +275,14 @@ describe('# AsyncResult', function() { expect(res).to.be.instanceof(AsyncResult); expect(res.err()).to.be.equal('foo'); }); + it('`success` should respect extended AsyncResult version', () => { + let res = OwnAsyncResult.success('foo'); + expect(res).to.be.instanceof(OwnAsyncResult); + }); + it('`fail` should respect extended AsyncResult version', () => { + let res = OwnAsyncResult.fail('foo'); + expect(res).to.be.instanceof(OwnAsyncResult); + }); }); + }); diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.js index 9c9ecfa..8abbeb8 100644 --- a/test/unit/utils.spec.js +++ b/test/unit/utils.spec.js @@ -1,11 +1,6 @@ import { toAsyncResult, wrapMethod, AsyncResult, addAsync } from '../../'; -const OwnAsyncResult = function() { - AsyncResult.apply(this, arguments); -}; -OwnAsyncResult.prototype = Object.create(AsyncResult.prototype); -OwnAsyncResult.prototype.constructor = AsyncResult; -OwnAsyncResult.success = AsyncResult.success; -OwnAsyncResult.fail = AsyncResult.fail; +import { OwnAsyncResult } from '../test-helpres'; +import { config } from '../..'; describe('# utils:', function() { describe('## toAsyncResult:', function() { @@ -202,6 +197,21 @@ describe('# utils:', function() { expect(await wrapped).to.be.equal(value); }); }); + + describe('when AsyncResult redefined in config', () => { + beforeEach(() => { + config.AsyncResult = OwnAsyncResult; + }); + afterEach(() => { + config.AsyncResult = AsyncResult; + }); + it('should return instance of provided AsyncResult class by default', async () => { + let res = await toAsyncResult(); + expect(res).to.be.instanceof(AsyncResult); + expect(res).to.be.instanceof(OwnAsyncResult); + }) + }); + }); describe('## wrapMethod:', function() { @@ -322,6 +332,36 @@ describe('# utils:', function() { }); }); + describe('when own AsyncResult defined in config', () => { + let method; + let methodAsync; + let context; + beforeEach(() => { + context = { foo: 123 }; + config.AsyncResult = OwnAsyncResult; + method = sinon.spy(function() { return this; }); + methodAsync = wrapMethod(method, { context }); + }); + afterEach(() => { + config.AsyncResult = AsyncResult; + }); + + it('should return function', function() { + expect(methodAsync).to.be.a('function'); + }); + + it('should call wrapped method only once', async function() { + await methodAsync(); + expect(method).to.be.calledOnce; + }); + + it('should return OwnAsyncResult instance with expected result after awaiting of call', async function() { + let result = await methodAsync(); + expect(result).to.be.instanceOf(OwnAsyncResult); + expect(result.val()).to.be.equal(context); + }); + }); + describe('when there is a runtime error in wrapedMethod', function() { let method; let methodAsync;