Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2834] module.initialize() now defers publish calls
It will now wait until all modules have loaded before publishing the events to ensure that all modules have a chance of receiving them regardless of order.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 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
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,92 @@ | ||
describe('ckan.pubsub', function () { | ||
beforeEach(function () { | ||
ckan.pubsub.events = jQuery({}); | ||
ckan.pubsub.queue = []; | ||
}); | ||
|
||
describe('.enqueue()', function () { | ||
beforeEach(function () { | ||
this.target = sinon.spy(); | ||
}); | ||
|
||
it('should defer callbacks for published events until later', function () { | ||
ckan.pubsub.subscribe('change', this.target); | ||
ckan.pubsub.enqueue(); | ||
ckan.pubsub.publish('change'); | ||
|
||
assert.notCalled(this.target); | ||
}); | ||
|
||
it('should add the published calls to the .queue', function () { | ||
var queue = ckan.pubsub.queue; | ||
|
||
ckan.pubsub.enqueue(); | ||
|
||
ckan.pubsub.publish('change'); | ||
assert.equal(queue.length, 1); | ||
|
||
ckan.pubsub.publish('change'); | ||
assert.equal(queue.length, 2); | ||
|
||
ckan.pubsub.publish('change'); | ||
assert.equal(queue.length, 3); | ||
}); | ||
}); | ||
|
||
describe('.dequeue()', function () { | ||
beforeEach(function () { | ||
ckan.pubsub.queue = [ | ||
['change'], | ||
['change', 'arg1', 'arg2'], | ||
['update', 'arg1'] | ||
]; | ||
|
||
this.target1 = sinon.spy(); | ||
this.target2 = sinon.spy(); | ||
this.target3 = sinon.spy(); | ||
|
||
ckan.pubsub.subscribe('change', this.target1); | ||
ckan.pubsub.subscribe('change', this.target2); | ||
ckan.pubsub.subscribe('update', this.target3); | ||
}); | ||
|
||
it('should publish all queued callbacks', function () { | ||
ckan.pubsub.dequeue(); | ||
|
||
assert.calledTwice(this.target1); | ||
assert.calledWith(this.target1, 'arg1', 'arg2'); | ||
|
||
assert.calledTwice(this.target2); | ||
assert.calledWith(this.target2, 'arg1', 'arg2'); | ||
|
||
assert.calledOnce(this.target3); | ||
}); | ||
|
||
it('should set the queue to null to allow new events to be published', function () { | ||
ckan.pubsub.dequeue(); | ||
assert.isNull(ckan.pubsub.queue); | ||
}); | ||
|
||
it('should not block new events from being published', function () { | ||
var pubsub = ckan.pubsub; | ||
|
||
var second = sinon.spy(); | ||
var third = sinon.spy(); | ||
|
||
pubsub.enqueue(); | ||
pubsub.subscribe('first', function () { | ||
pubsub.publish('third'); | ||
}); | ||
pubsub.subscribe('second', second); | ||
pubsub.subscribe('third', third); | ||
|
||
pubsub.publish('first'); | ||
pubsub.publish('second'); | ||
|
||
pubsub.dequeue(); | ||
|
||
assert.called(second); | ||
assert.called(third); | ||
}); | ||
}); | ||
}); |