|
| 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 { expect } from 'chai'; |
| 19 | +import '../test/setup'; |
| 20 | +import { match, stub } from 'sinon'; |
| 21 | +import { |
| 22 | + readHeartbeatsFromIndexedDB, |
| 23 | + writeHeartbeatsToIndexedDB |
| 24 | +} from './indexeddb'; |
| 25 | +import { FirebaseApp } from './public-types'; |
| 26 | +import { AppError } from './errors'; |
| 27 | +import { HeartbeatsInIndexedDB } from './types'; |
| 28 | + |
| 29 | +/** |
| 30 | + * Mostly testing failure cases. heartbeatService.test.ts tests read-write |
| 31 | + * more extensively. |
| 32 | + */ |
| 33 | + |
| 34 | +describe('IndexedDB functions', () => { |
| 35 | + it('readHeartbeatsFromIndexedDB warns if IndexedDB.open() throws', async () => { |
| 36 | + const warnStub = stub(console, 'warn'); |
| 37 | + if (typeof window !== 'undefined') { |
| 38 | + // Ensure that indexedDB.open() fails in browser. It will always fail in Node. |
| 39 | + stub(window.indexedDB, 'open').throws(new Error('abcd')); |
| 40 | + await readHeartbeatsFromIndexedDB({ |
| 41 | + name: 'testname', |
| 42 | + options: { appId: 'test-app-id' } |
| 43 | + } as FirebaseApp); |
| 44 | + expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_GET)); |
| 45 | + } else { |
| 46 | + await readHeartbeatsFromIndexedDB({ |
| 47 | + name: 'testname', |
| 48 | + options: { appId: 'test-app-id' } |
| 49 | + } as FirebaseApp); |
| 50 | + expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_GET)); |
| 51 | + } |
| 52 | + }); |
| 53 | + it('writeHeartbeatsToIndexedDB warns if IndexedDB.open() throws', async () => { |
| 54 | + const warnStub = stub(console, 'warn'); |
| 55 | + if (typeof window !== 'undefined') { |
| 56 | + // Ensure that indexedDB.open() fails in browser. It will always fail in Node. |
| 57 | + stub(window.indexedDB, 'open').throws(new Error('abcd')); |
| 58 | + await writeHeartbeatsToIndexedDB( |
| 59 | + { |
| 60 | + name: 'testname', |
| 61 | + options: { appId: 'test-app-id' } |
| 62 | + } as FirebaseApp, |
| 63 | + {} as HeartbeatsInIndexedDB |
| 64 | + ); |
| 65 | + expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_WRITE)); |
| 66 | + } else { |
| 67 | + await writeHeartbeatsToIndexedDB( |
| 68 | + { |
| 69 | + name: 'testname', |
| 70 | + options: { appId: 'test-app-id' } |
| 71 | + } as FirebaseApp, |
| 72 | + {} as HeartbeatsInIndexedDB |
| 73 | + ); |
| 74 | + expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_WRITE)); |
| 75 | + } |
| 76 | + }); |
| 77 | +}); |
0 commit comments