|
| 1 | +import chai from 'chai'; |
| 2 | +import chaiAsPromised from 'chai-as-promised'; |
| 3 | +import DispatchMessageStatusRealTimeContext from './DispatchMessageStatusRealTimeContext'; |
| 4 | +import RealTimeClient from '../RealTimeClient'; |
| 5 | +import { realTime as mock } from '../mocks'; |
| 6 | + |
| 7 | +chai.should(); |
| 8 | +chai.use(chaiAsPromised); |
| 9 | + |
| 10 | +describe('When creating a subscription for Dispatch Message Status', () => { |
| 11 | + const entity = 'DISPATCH_MESSAGE_STATUSES'; |
| 12 | + const customerCode = 'SYNC'; |
| 13 | + |
| 14 | + it('can add filters for a single user', () => { |
| 15 | + const server = mock.getServer(); |
| 16 | + const realTimeClient = new RealTimeClient(mock.authenticatedClient, mock.options); |
| 17 | + const subject = new DispatchMessageStatusRealTimeContext(realTimeClient, customerCode); |
| 18 | + |
| 19 | + const userHref = '/1/users/1'; |
| 20 | + const expectedFilters = { users: [userHref] }; |
| 21 | + subject.forUser(userHref).on('update', () => { }); |
| 22 | + |
| 23 | + const options = { closeConnection: true, realTimeClient }; |
| 24 | + return server.verifySubscription(entity, options) |
| 25 | + .should.eventually.become(expectedFilters); |
| 26 | + }); |
| 27 | + |
| 28 | + it('can add filters for multiple users', () => { |
| 29 | + const server = mock.getServer(); |
| 30 | + const realTimeClient = new RealTimeClient(mock.authenticatedClient, mock.options); |
| 31 | + const subject = new DispatchMessageStatusRealTimeContext(realTimeClient, customerCode); |
| 32 | + |
| 33 | + const userHrefs = ['/1/users/1', '/1/users/1']; |
| 34 | + const expectedFilters = { users: userHrefs }; |
| 35 | + subject.forUsers(userHrefs).on('update', () => { }); |
| 36 | + |
| 37 | + const options = { closeConnection: true, realTimeClient }; |
| 38 | + return server.verifySubscription(entity, options) |
| 39 | + .should.eventually.become(expectedFilters); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle entity updates', () => { |
| 43 | + const server = mock.getServer(); |
| 44 | + const realTimeClient = new RealTimeClient(mock.authenticatedClient, mock.options); |
| 45 | + const subject = new DispatchMessageStatusRealTimeContext(realTimeClient, customerCode); |
| 46 | + |
| 47 | + let resolver; |
| 48 | + const updateReceived = new Promise((resolve) => { resolver = resolve; }); |
| 49 | + const connectionClosed = updateReceived |
| 50 | + .then(() => server.closeConnection(realTimeClient)); |
| 51 | + |
| 52 | + const userHref = '/1/users/1'; |
| 53 | + const subscription = subject |
| 54 | + .forUser(userHref) |
| 55 | + .on('update', resolver); |
| 56 | + |
| 57 | + return Promise.all([ |
| 58 | + subscription, |
| 59 | + updateReceived, |
| 60 | + connectionClosed, |
| 61 | + ]); |
| 62 | + }); |
| 63 | +}); |
0 commit comments