|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2022 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { CONSTANTS } from '@firebase/util'; |
| 19 | +import { expect, use } from 'chai'; |
| 20 | +import { createSandbox, SinonSandbox, SinonSpy } from 'sinon'; |
| 21 | +import sinonChai from 'sinon-chai'; |
| 22 | + |
| 23 | +import { forceLongPolling, forceWebSockets } from '../src'; |
| 24 | +import * as Util from '../src/core/util/util'; |
| 25 | +import { BrowserPollConnection } from '../src/realtime/BrowserPollConnection'; |
| 26 | +import { TransportManager } from '../src/realtime/TransportManager'; |
| 27 | +import { WebSocketConnection } from '../src/realtime/WebSocketConnection'; |
| 28 | + |
| 29 | +use(sinonChai); |
| 30 | +const transportInitError = |
| 31 | + 'Transport has already been initialized. Please call this function before calling ref or setting up a listener'; |
| 32 | +describe('Force Transport', () => { |
| 33 | + const oldNodeValue = CONSTANTS.NODE_CLIENT; |
| 34 | + let mySandbox: SinonSandbox; |
| 35 | + let spyWarn: SinonSpy; |
| 36 | + beforeEach(() => { |
| 37 | + CONSTANTS.NODE_CLIENT = false; |
| 38 | + mySandbox = createSandbox(); |
| 39 | + spyWarn = mySandbox.spy(Util, 'warn'); |
| 40 | + }); |
| 41 | + afterEach(() => { |
| 42 | + // Resetting to old values |
| 43 | + TransportManager.globalTransportInitialized_ = false; |
| 44 | + CONSTANTS.NODE_CLIENT = oldNodeValue; |
| 45 | + BrowserPollConnection.forceAllow_ = false; |
| 46 | + BrowserPollConnection.forceDisallow_ = true; |
| 47 | + WebSocketConnection.forceDisallow_ = false; |
| 48 | + mySandbox.restore(); |
| 49 | + }); |
| 50 | + it('should enable websockets and disable longPolling', () => { |
| 51 | + forceWebSockets(); |
| 52 | + expect(spyWarn.called).to.equal(false); |
| 53 | + expect(WebSocketConnection.isAvailable()).to.equal(true); |
| 54 | + expect(BrowserPollConnection.isAvailable()).to.equal(false); |
| 55 | + }); |
| 56 | + it('should throw an error when calling forceWebsockets() if TransportManager has already been initialized', () => { |
| 57 | + TransportManager.globalTransportInitialized_ = true; |
| 58 | + forceWebSockets(); |
| 59 | + expect(spyWarn).to.have.been.calledWith(transportInitError); |
| 60 | + expect(WebSocketConnection.isAvailable()).to.equal(true); |
| 61 | + expect(BrowserPollConnection.isAvailable()).to.equal(false); |
| 62 | + }); |
| 63 | + it('should enable longPolling and disable websockets', () => { |
| 64 | + forceLongPolling(); |
| 65 | + expect(spyWarn.called).to.equal(false); |
| 66 | + expect(WebSocketConnection.isAvailable()).to.equal(false); |
| 67 | + expect(BrowserPollConnection.isAvailable()).to.equal(true); |
| 68 | + }); |
| 69 | + it('should throw an error when calling forceLongPolling() if TransportManager has already been initialized', () => { |
| 70 | + TransportManager.globalTransportInitialized_ = true; |
| 71 | + forceLongPolling(); |
| 72 | + expect(spyWarn).to.have.been.calledWith(transportInitError); |
| 73 | + expect(WebSocketConnection.isAvailable()).to.equal(false); |
| 74 | + expect(BrowserPollConnection.isAvailable()).to.equal(true); |
| 75 | + }); |
| 76 | +}); |
0 commit comments