|
| 1 | +/*! |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google Inc. |
| 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 | +'use strict'; |
| 19 | + |
| 20 | +import * as chai from 'chai'; |
| 21 | +import * as sinonChai from 'sinon-chai'; |
| 22 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 23 | + |
| 24 | +import * as mocks from '../../resources/mocks'; |
| 25 | +import { App } from '../../../src/app/index'; |
| 26 | +import { instanceId, InstanceId } from '../../../src/instance-id/index'; |
| 27 | + |
| 28 | +chai.should(); |
| 29 | +chai.use(sinonChai); |
| 30 | +chai.use(chaiAsPromised); |
| 31 | + |
| 32 | +const expect = chai.expect; |
| 33 | + |
| 34 | +describe('InstanceId', () => { |
| 35 | + let mockApp: App; |
| 36 | + let mockCredentialApp: App; |
| 37 | + |
| 38 | + const noProjectIdError = 'Failed to determine project ID for InstanceId. Initialize the SDK ' |
| 39 | + + 'with service account credentials or set project ID as an app option. Alternatively set the ' |
| 40 | + + 'GOOGLE_CLOUD_PROJECT environment variable.'; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + mockApp = mocks.app(); |
| 44 | + mockCredentialApp = mocks.mockCredentialApp(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('instanceId()', () => { |
| 48 | + it('should throw when default app is not available', () => { |
| 49 | + expect(() => { |
| 50 | + return instanceId(); |
| 51 | + }).to.throw('The default Firebase app does not exist.'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should reject given an invalid credential without project ID', () => { |
| 55 | + // Project ID not set in the environment. |
| 56 | + delete process.env.GOOGLE_CLOUD_PROJECT; |
| 57 | + delete process.env.GCLOUD_PROJECT; |
| 58 | + const iid = instanceId(mockCredentialApp); |
| 59 | + return iid.deleteInstanceId('iid') |
| 60 | + .should.eventually.rejectedWith(noProjectIdError); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should not throw given a valid app', () => { |
| 64 | + expect(() => { |
| 65 | + return instanceId(mockApp); |
| 66 | + }).not.to.throw(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should return the same instance for a given app instance', () => { |
| 70 | + const iid1: InstanceId = instanceId(mockApp); |
| 71 | + const iid2: InstanceId = instanceId(mockApp); |
| 72 | + expect(iid1).to.equal(iid2); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments