Skip to content

Commit

Permalink
added tests against config.AsyncResult
Browse files Browse the repository at this point in the history
  • Loading branch information
taburetkin committed Nov 20, 2019
1 parent cfe7ab5 commit c700d7c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
test/
9 changes: 9 additions & 0 deletions 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;
10 changes: 10 additions & 0 deletions 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() {
Expand Down Expand Up @@ -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);
});
});

});
54 changes: 47 additions & 7 deletions 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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c700d7c

Please sign in to comment.