|
| 1 | +/** |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { expect } from 'chai'; |
| 18 | +import { sandbox, SinonSandbox, SinonStub } from 'sinon'; |
| 19 | + |
| 20 | +import { FirebaseApp } from '@firebase/app-types'; |
| 21 | +import { _FirebaseNamespace } from '@firebase/app-types/private'; |
| 22 | + |
| 23 | +import { MessagingServiceFactory, registerMessaging } from '../index'; |
| 24 | +import { ERROR_CODES } from '../src/models/errors'; |
| 25 | + |
| 26 | +import { SWController } from '../src/controllers/sw-controller'; |
| 27 | +import { WindowController } from '../src/controllers/window-controller'; |
| 28 | +import { makeFakeApp } from './testing-utils/make-fake-app'; |
| 29 | + |
| 30 | +describe('Firebase Messaging > registerMessaging', () => { |
| 31 | + let sinonSandbox: SinonSandbox; |
| 32 | + let registerService: SinonStub; |
| 33 | + let fakeFirebase: _FirebaseNamespace; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + sinonSandbox = sandbox.create(); |
| 37 | + registerService = sinonSandbox.stub(); |
| 38 | + |
| 39 | + fakeFirebase = { |
| 40 | + INTERNAL: { registerService } |
| 41 | + } as any; |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + sinonSandbox.restore(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('calls registerService', () => { |
| 49 | + registerMessaging(fakeFirebase); |
| 50 | + expect(registerService.callCount).to.equal(1); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('factoryMethod', () => { |
| 54 | + let factoryMethod: MessagingServiceFactory; |
| 55 | + let fakeApp: FirebaseApp; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + registerMessaging(fakeFirebase); |
| 59 | + factoryMethod = registerService.getCall(0).args[1]; |
| 60 | + |
| 61 | + fakeApp = makeFakeApp({ |
| 62 | + messagingSenderId: '1234567890' |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('isSupported', () => { |
| 67 | + it('is a static method on factoryMethod', () => { |
| 68 | + expect(factoryMethod.isSupported).to.be.a('function'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('in Service Worker context', () => { |
| 73 | + beforeEach(() => { |
| 74 | + // self.ServiceWorkerGlobalScope exists |
| 75 | + // can't stub a non-existing property, so no sinon.stub(). |
| 76 | + (self as any).ServiceWorkerGlobalScope = {}; |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + delete (self as any).ServiceWorkerGlobalScope; |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns a SWController', () => { |
| 84 | + const firebaseService = factoryMethod(fakeApp); |
| 85 | + expect(firebaseService).to.be.instanceOf(SWController); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('in Window context', () => { |
| 90 | + it('throws if required globals do not exist', () => { |
| 91 | + // Empty navigator, no navigator.serviceWorker ¯\_(ツ)_/¯ |
| 92 | + sinonSandbox.stub(window, 'navigator').value({}); |
| 93 | + |
| 94 | + try { |
| 95 | + factoryMethod(fakeApp); |
| 96 | + } catch (e) { |
| 97 | + expect(e.code).to.equal( |
| 98 | + 'messaging/' + ERROR_CODES.UNSUPPORTED_BROWSER |
| 99 | + ); |
| 100 | + return; |
| 101 | + } |
| 102 | + throw new Error('Expected getToken to throw '); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns a WindowController', () => { |
| 106 | + const firebaseService = factoryMethod(fakeApp); |
| 107 | + expect(firebaseService).to.be.instanceOf(WindowController); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments