|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 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 { InstallationIdProvider } from './installation-id-provider'; |
| 19 | +import { _FirebaseInstallationsInternal } from '@firebase/installations'; |
| 20 | +import { expect } from 'chai'; |
| 21 | + |
| 22 | +describe('InstallationIdProvider', () => { |
| 23 | + it('should cache the installation id after the first call', async () => { |
| 24 | + let callCount = 0; |
| 25 | + const mockInstallations = { |
| 26 | + getId: async () => { |
| 27 | + callCount++; |
| 28 | + return 'iid-123'; |
| 29 | + } |
| 30 | + } as unknown as _FirebaseInstallationsInternal; |
| 31 | + |
| 32 | + const mockProvider = { |
| 33 | + getImmediate: () => mockInstallations, |
| 34 | + get: async () => mockInstallations |
| 35 | + } as any; |
| 36 | + |
| 37 | + const provider = new InstallationIdProvider(mockProvider); |
| 38 | + |
| 39 | + const attr1 = await provider.getAttribute(); |
| 40 | + expect(attr1).to.deep.equal(['user.id', 'iid-123']); |
| 41 | + expect(callCount).to.equal(1); |
| 42 | + |
| 43 | + const attr2 = await provider.getAttribute(); |
| 44 | + expect(attr2).to.deep.equal(['user.id', 'iid-123']); |
| 45 | + expect(callCount).to.equal(1); // Should still be 1 |
| 46 | + }); |
| 47 | + |
| 48 | + it('should not cache if installation id is null', async () => { |
| 49 | + let callCount = 0; |
| 50 | + let returnValue: string | null = null; |
| 51 | + const mockInstallations = { |
| 52 | + getId: async () => { |
| 53 | + callCount++; |
| 54 | + return returnValue; |
| 55 | + } |
| 56 | + } as unknown as _FirebaseInstallationsInternal; |
| 57 | + |
| 58 | + const mockProvider = { |
| 59 | + getImmediate: () => mockInstallations, |
| 60 | + get: async () => mockInstallations |
| 61 | + } as any; |
| 62 | + |
| 63 | + const provider = new InstallationIdProvider(mockProvider); |
| 64 | + |
| 65 | + const attr1 = await provider.getAttribute(); |
| 66 | + expect(attr1).to.be.null; |
| 67 | + expect(callCount).to.equal(1); |
| 68 | + |
| 69 | + returnValue = 'iid-456'; |
| 70 | + const attr2 = await provider.getAttribute(); |
| 71 | + expect(attr2).to.deep.equal(['user.id', 'iid-456']); |
| 72 | + expect(callCount).to.equal(2); |
| 73 | + |
| 74 | + // Should cache now |
| 75 | + const attr3 = await provider.getAttribute(); |
| 76 | + expect(attr3).to.deep.equal(['user.id', 'iid-456']); |
| 77 | + expect(callCount).to.equal(2); |
| 78 | + }); |
| 79 | +}); |
0 commit comments