1
1
import nock from 'nock' ;
2
2
3
- import { Payload } from '../../src/core/types/api ' ;
3
+ import StatusCategory from '../../src/core/constants/categories ' ;
4
4
import PubNub from '../../src/node/index' ;
5
5
import utils from '../utils' ;
6
6
@@ -42,6 +42,28 @@ describe('EventEngine', () => {
42
42
unsub ( ) ;
43
43
} ) ;
44
44
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
+
45
67
function forEvent ( eventLabel : string , timeout ?: number ) {
46
68
return new Promise < void > ( ( resolve , reject ) => {
47
69
let timeoutId : NodeJS . Timeout | null = null ;
@@ -57,7 +79,7 @@ describe('EventEngine', () => {
57
79
if ( timeout ) {
58
80
timeoutId = setTimeout ( ( ) => {
59
81
unsubscribe ( ) ;
60
- reject ( new Error ( `Timeout occured while waiting for state ${ eventLabel } ` ) ) ;
82
+ reject ( new Error ( `Timeout occurred while waiting for state ${ eventLabel } ` ) ) ;
61
83
} , timeout ) ;
62
84
}
63
85
} ) ;
@@ -118,6 +140,33 @@ describe('EventEngine', () => {
118
140
await forState ( 'UNSUBSCRIBED' , 1000 ) ;
119
141
} ) ;
120
142
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
+
121
170
// TODO: retry with configuration
122
171
// it('should retry correctly', async () => {
123
172
// utils.createNock().get('/v2/subscribe/demo/test/0').query(true).reply(200, '{"t":{"t":"12345","r":1}, "m": []}');
0 commit comments