Skip to content

Commit b7d2add

Browse files
committed
test(subscription-change): add unit test to check subscription change emitted
Add test which tests whether `subscription change` is emitted when subscribing to the new channels.
1 parent a4d4ce1 commit b7d2add

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

test/integration/components/listeners.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ describe('#listeners', () => {
2525
useRequestId: false,
2626
enableEventEngine: true,
2727
autoNetworkDetection: false,
28-
keepAlive: false,
2928
});
3029
});
3130

test/unit/event_engine.test.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import nock from 'nock';
22

3-
import { Payload } from '../../src/core/types/api';
3+
import StatusCategory from '../../src/core/constants/categories';
44
import PubNub from '../../src/node/index';
55
import utils from '../utils';
66

@@ -42,6 +42,28 @@ describe('EventEngine', () => {
4242
unsub();
4343
});
4444

45+
function forStatus(statusCategory: StatusCategory, timeout?: number) {
46+
return new Promise<void>((resolve, reject) => {
47+
let timeoutId: ReturnType<typeof setTimeout>;
48+
49+
pubnub.addListener({
50+
status: (statusEvent) => {
51+
if (statusEvent.category === statusCategory) {
52+
pubnub.removeAllListeners();
53+
resolve();
54+
}
55+
},
56+
});
57+
58+
if (timeout) {
59+
timeoutId = setTimeout(() => {
60+
pubnub.removeAllListeners();
61+
reject(new Error(`Timeout occurred while waiting for state ${statusCategory}`));
62+
}, timeout);
63+
}
64+
});
65+
}
66+
4567
function forEvent(eventLabel: string, timeout?: number) {
4668
return new Promise<void>((resolve, reject) => {
4769
let timeoutId: NodeJS.Timeout | null = null;
@@ -57,7 +79,7 @@ describe('EventEngine', () => {
5779
if (timeout) {
5880
timeoutId = setTimeout(() => {
5981
unsubscribe();
60-
reject(new Error(`Timeout occured while waiting for state ${eventLabel}`));
82+
reject(new Error(`Timeout occurred while waiting for state ${eventLabel}`));
6183
}, timeout);
6284
}
6385
});
@@ -118,6 +140,33 @@ describe('EventEngine', () => {
118140
await forState('UNSUBSCRIBED', 1000);
119141
});
120142

143+
it('should work correctly', async () => {
144+
utils.createNock().get('/v2/subscribe/demo/test/0').query(true).reply(200, '{"t":{"t":"12345","r":1}, "m": []}');
145+
utils.createNock().get('/v2/subscribe/demo/test/0').query(true).reply(200, '{"t":{"t":"12345","r":1}, "m": []}');
146+
utils
147+
.createNock()
148+
.get('/v2/subscribe/demo/test,test1/0')
149+
.query(true)
150+
.reply(200, '{"t":{"t":"12345","r":1}, "m":[]}');
151+
utils
152+
.createNock()
153+
.get('/v2/subscribe/demo/test,test1/0')
154+
.query(true)
155+
.reply(200, '{"t":{"t":"12345","r":1}, "m":[]}');
156+
157+
pubnub.subscribe({ channels: ['test'] });
158+
159+
await forEvent('HANDSHAKE_SUCCESS', 1000);
160+
161+
pubnub.subscribe({ channels: ['test1'] });
162+
163+
await forStatus(StatusCategory.PNSubscriptionChangedCategory);
164+
165+
pubnub.unsubscribe({ channels: ['test', 'test1'] });
166+
167+
await forState('UNSUBSCRIBED', 1000);
168+
});
169+
121170
// TODO: retry with configuration
122171
// it('should retry correctly', async () => {
123172
// utils.createNock().get('/v2/subscribe/demo/test/0').query(true).reply(200, '{"t":{"t":"12345","r":1}, "m": []}');

0 commit comments

Comments
 (0)