Skip to content

Commit

Permalink
Fixed a subscription of the queue on a failed item
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Kurkin committed Jun 22, 2017
1 parent 9737a97 commit f3fa90b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 14 deletions.
15 changes: 14 additions & 1 deletion Response.js
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,20 @@ function checkResponse(queue, item) {
}

if (isCompatible(item) && !isItemResolved(item)) {
then(item, onResolveItem, onRejectItem, null, queue);
if (item.onChangeState && !isItemRejected(item)) {
item.once(EVENT_CHANGE_STATE, function listener(state) {
switch (state) {
case STATE_RESOLVED:
onResolveItem.call(queue);
break;
case STATE_REJECTED:
onRejectItem.call(queue, this.getReason());
break;
}
});
} else {
then(item, onResolveItem, onRejectItem, null, queue);
}
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Response",
"author": "Vladislav Kurkin <[email protected]> (https://github.com/B-Vladi)",
"version": "0.1.10",
"version": "0.1.11",
"main": "Response.js",
"license": "MIT",
"repository": {
Expand Down
86 changes: 74 additions & 12 deletions spec/Queue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ describe('Queue:', function () {

queue = new Queue()
.push(1, 'a')
.push(function b () {
.push(function b() {
expect(this.keys).toEqual(['a', 'b', 'c']);

this.push(q, 'd');
Expand All @@ -287,12 +287,12 @@ describe('Queue:', function () {

it('dynamic push in items', function () {
queue = new Queue([{
name: 'key0'
}])
.push(function key1 () {
name: 'key0'
}])
.push(function key1() {
this.push(listener, 'key3');
})
.push(function key2 () {
.push(function key2() {
this
.push(listener, 'key4')
.push(listener, 'key5');
Expand Down Expand Up @@ -383,7 +383,8 @@ describe('Queue:', function () {
it('on "itemRejected" event should not be fired if all items has resolved', function () {
queue
.push(1)
.push(function () {})
.push(function () {
})
.push((new Response()).resolve())
.onItemRejected(listener)
.start();
Expand Down Expand Up @@ -434,7 +435,7 @@ describe('Queue:', function () {
});

it('on "itemRejected" event should fire once for each rejected item', function () {
function failingFn () {
function failingFn() {
throw new Error();
}

Expand Down Expand Up @@ -616,7 +617,7 @@ describe('Queue:', function () {

it('start should accept arguments for first task', function () {
var testArg1 = {};
var testArg2= {};
var testArg2 = {};

queue
.push(function (arg1, arg2) {
Expand All @@ -628,9 +629,9 @@ describe('Queue:', function () {

it('start arguments should not propagate to second task', function () {
queue
.push(function task1 () {
.push(function task1() {
})
.push(function task2 () {
.push(function task2() {
expect(arguments.length).toBe(0);
})
.start([1, 2]);
Expand Down Expand Up @@ -797,10 +798,10 @@ describe('Queue:', function () {

it('should not pass undefined to next task', function () {
queue
.push(function task1 () {
.push(function task1() {

})
.push(function task2 () {
.push(function task2() {
expect(arguments.length).toBe(0);
});
});
Expand Down Expand Up @@ -841,4 +842,65 @@ describe('Queue:', function () {
it('destroy for empty Queue don`t throw expection', function () {
new Queue().destroy().destroy(true);
});

describe('External errors:', function () {
var faultyListener;
var goodListener;

beforeEach(function () {
faultyListener = jasmine
.createSpy('faultyListener').and
.callFake(function throwsError() {
throw new Error('handler throws');
});

goodListener = jasmine.createSpy('goodListener');
});

it('resolves when .onResolve for an item throws error', function () {
expect(faultyListener).toThrow();

var item = new Response().onResolve(faultyListener);

queue
.push(item)
.onResolve(goodListener)
.start();

item.resolve(1);

expect(goodListener).toHaveBeenCalled();
});

it('resolves when .onReject for an item throws error', function () {
expect(faultyListener).toThrow();

var item = new Response().onReject(faultyListener);

queue
.push(item)
.onResolve(goodListener)
.start();

item.reject(new Error('item fails'));

expect(goodListener).toHaveBeenCalled();
});

it('rejects if .strict and .onReject for an item throws an error', function () {
expect(faultyListener).toThrow();

var item = new Response().onReject(faultyListener);

queue
.push(item)
.onReject(goodListener)
.strict()
.start();

item.reject(new Error('item fails'));

expect(goodListener).toHaveBeenCalled();
});
});
});

0 comments on commit f3fa90b

Please sign in to comment.