Skip to content

Commit 9349875

Browse files
committed
Add tests
1 parent a94b638 commit 9349875

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

packages/app/src/indexeddb.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { expect } from 'chai';
2+
import '../test/setup';
3+
import { match, stub } from 'sinon';
4+
import { readHeartbeatsFromIndexedDB, writeHeartbeatsToIndexedDB } from './indexeddb';
5+
import { FirebaseApp } from './public-types';
6+
import { AppError } from './errors';
7+
import { HeartbeatsInIndexedDB } from './types';
8+
9+
/**
10+
* Mostly testing failure cases. heartbeatService.test.ts tests read-write
11+
* more extensively.
12+
*/
13+
14+
describe('IndexedDB functions', () => {
15+
it('readHeartbeatsFromIndexedDB warns if IndexedDB.open() throws', async () => {
16+
const warnStub = stub(console, 'warn');
17+
if (typeof window !== 'undefined') {
18+
// Ensure that indexedDB.open() fails in browser. It will always fail in Node.
19+
stub(window.indexedDB, 'open').throws(new Error('abcd'));
20+
await readHeartbeatsFromIndexedDB({
21+
name: 'testname',
22+
options: { appId: 'test-app-id' }
23+
} as FirebaseApp
24+
);
25+
expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_GET));
26+
} else {
27+
await readHeartbeatsFromIndexedDB({
28+
name: 'testname',
29+
options: { appId: 'test-app-id' }
30+
} as FirebaseApp
31+
);
32+
expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_GET));
33+
}
34+
});
35+
it('writeHeartbeatsToIndexedDB 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 writeHeartbeatsToIndexedDB({
41+
name: 'testname',
42+
options: { appId: 'test-app-id' }
43+
} as FirebaseApp,
44+
{} as HeartbeatsInIndexedDB
45+
);
46+
expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_WRITE));
47+
} else {
48+
await writeHeartbeatsToIndexedDB({
49+
name: 'testname',
50+
options: { appId: 'test-app-id' }
51+
} as FirebaseApp,
52+
{} as HeartbeatsInIndexedDB
53+
);
54+
expect(warnStub).to.be.calledWith(match.any, match(AppError.IDB_WRITE));
55+
}
56+
});
57+
});

0 commit comments

Comments
 (0)