15
15
* limitations under the License.
16
16
*/
17
17
18
+
19
+
18
20
import { CONSTANTS } from '@firebase/util' ;
19
- import { expect } from 'chai' ;
21
+ import { expect , use } from 'chai' ;
22
+ import { createSandbox , SinonSandbox , SinonSpy } from 'sinon' ;
23
+ import sinonChai from 'sinon-chai' ;
20
24
21
25
import { forceLongPolling , forceWebSockets } from '../src' ;
26
+ import * as Util from '../src/core/util/util' ;
22
27
import { BrowserPollConnection } from '../src/realtime/BrowserPollConnection' ;
23
28
import { TransportManager } from '../src/realtime/TransportManager' ;
24
29
import { WebSocketConnection } from '../src/realtime/WebSocketConnection' ;
25
30
31
+ use ( sinonChai ) ;
26
32
const transportInitError =
27
33
'Transport has already been initialized. Please call this function before calling ref or setting up a listener' ;
28
34
describe ( 'Force Transport' , ( ) => {
29
35
const oldNodeValue = CONSTANTS . NODE_CLIENT ;
36
+ let mySandbox : SinonSandbox ;
37
+ let spyWarn : SinonSpy ;
30
38
beforeEach ( ( ) => {
31
39
CONSTANTS . NODE_CLIENT = false ;
40
+ mySandbox = createSandbox ( ) ;
41
+ spyWarn = mySandbox . spy ( Util , 'warn' ) ;
32
42
} ) ;
33
43
afterEach ( ( ) => {
34
44
// Resetting to old values
@@ -37,27 +47,32 @@ describe('Force Transport', () => {
37
47
BrowserPollConnection . forceAllow_ = false ;
38
48
BrowserPollConnection . forceDisallow_ = true ;
39
49
WebSocketConnection . forceDisallow_ = false ;
50
+ mySandbox . restore ( ) ;
40
51
} ) ;
41
52
it ( 'should enable websockets and disable longPolling' , ( ) => {
42
- expect ( forceWebSockets ) . to . not . throw ( ) ;
53
+ forceWebSockets ( ) ;
54
+ expect ( spyWarn . called ) . to . equal ( false ) ;
43
55
expect ( WebSocketConnection . isAvailable ( ) ) . to . equal ( true ) ;
44
56
expect ( BrowserPollConnection . isAvailable ( ) ) . to . equal ( false ) ;
45
57
} ) ;
46
58
it ( 'should throw an error when calling forceWebsockets() if TransportManager has already been initialized' , ( ) => {
47
59
TransportManager . globalTransportInitialized_ = true ;
48
- expect ( forceWebSockets ) . to . throw ( transportInitError ) ;
60
+ forceWebSockets ( ) ;
61
+ expect ( spyWarn ) . to . have . been . calledWith ( transportInitError ) ;
49
62
expect ( WebSocketConnection . isAvailable ( ) ) . to . equal ( true ) ;
50
63
expect ( BrowserPollConnection . isAvailable ( ) ) . to . equal ( false ) ;
51
64
} ) ;
52
65
it ( 'should enable longPolling and disable websockets' , ( ) => {
53
- expect ( forceLongPolling ) . to . not . throw ( ) ;
66
+ forceLongPolling ( ) ;
67
+ expect ( spyWarn . called ) . to . equal ( false ) ;
54
68
expect ( WebSocketConnection . isAvailable ( ) ) . to . equal ( false ) ;
55
69
expect ( BrowserPollConnection . isAvailable ( ) ) . to . equal ( true ) ;
56
70
} ) ;
57
71
it ( 'should throw an error when calling forceLongPolling() if TransportManager has already been initialized' , ( ) => {
58
72
TransportManager . globalTransportInitialized_ = true ;
59
- expect ( forceLongPolling ) . to . throw ( transportInitError ) ;
60
- expect ( WebSocketConnection . isAvailable ( ) ) . to . equal ( true ) ;
61
- expect ( BrowserPollConnection . isAvailable ( ) ) . to . equal ( false ) ;
73
+ forceLongPolling ( ) ;
74
+ expect ( spyWarn ) . to . have . been . calledWith ( transportInitError ) ;
75
+ expect ( WebSocketConnection . isAvailable ( ) ) . to . equal ( false ) ;
76
+ expect ( BrowserPollConnection . isAvailable ( ) ) . to . equal ( true ) ;
62
77
} ) ;
63
78
} ) ;
0 commit comments