diff options
author | Martin Hanzel <mhanzel@gitlab.com> | 2019-08-15 16:43:29 -0400 |
---|---|---|
committer | Martin Hanzel <mhanzel@gitlab.com> | 2019-08-15 16:58:55 -0400 |
commit | 23906d6478b46fe0ac21f22d21321f9a9f0b9f81 (patch) | |
tree | bc64e0b8a6001fb017fa0c287c2568354f5866e3 /spec/frontend/lib/utils/pubsub_spec.js | |
parent | 23754943a7ec119f123694a93c79fc07c32b7ba5 (diff) | |
download | gitlab-ce-mh/pubsub.tar.gz |
Add pubsub messaging systemmh/pubsub
Diffstat (limited to 'spec/frontend/lib/utils/pubsub_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/pubsub_spec.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/spec/frontend/lib/utils/pubsub_spec.js b/spec/frontend/lib/utils/pubsub_spec.js new file mode 100644 index 00000000000..244e6d3bbc8 --- /dev/null +++ b/spec/frontend/lib/utils/pubsub_spec.js @@ -0,0 +1,41 @@ +import { publish, subscribe } from '~/lib/utils/pubsub'; + +describe('Pub/sub messaging', () => { + it('sends and receives messages asynchronously', done => { + const receiver1 = jest.fn(); + const receiver2 = jest.fn(); + const receiver3 = jest.fn(); + subscribe('namespace:topic', receiver1); + subscribe('namespace:topic', receiver2); + subscribe('namespace:topic2', receiver3); + + publish('shoudnotreceive', 'shouldnotreceive'); + publish('namespace:topic', 1); + publish('namespace:topic', 2); + publish('namespace:topic2', 3); + + // Receivers should not be called synchronously + expect(receiver1).not.toHaveBeenCalled(); + expect(receiver2).not.toHaveBeenCalled(); + expect(receiver3).not.toHaveBeenCalled(); + + setImmediate(() => { + expect(receiver1.mock.calls).toEqual([[1], [2]]); + expect(receiver2.mock.calls).toEqual([[1], [2]]); + expect(receiver3.mock.calls).toEqual([[3]]); + done(); + }); + }); + + it('allows clients to unsubscribe', done => { + const receiver = jest.fn(); + const unsubscribe = subscribe('topic', receiver); + publish('topic', 1); + unsubscribe(); + publish('topic', 2); + setImmediate(() => { + expect(receiver.mock.calls).toEqual([[1]]); + done(); + }); + }); +}); |