Skip to content

Commit 6ad767c

Browse files
committed
feat: add IndexedDB unit test
1 parent 79d22b9 commit 6ad767c

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ module.exports = {
99
testPathIgnorePatterns: ['/node_modules/'],
1010
testMatch: ['**/__test__/**/(*.)+test.[jt]s?(x)'],
1111
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
12+
setupFilesAfterEnv: ['./jest.setup.ts'],
1213
};

jest.setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import structuredClone from '@ungap/structured-clone';
2+
import 'fake-indexeddb/auto';
3+
4+
if (typeof globalThis.structuredClone !== 'function') {
5+
globalThis.structuredClone = structuredClone as unknown as typeof globalThis.structuredClone;
6+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
"@types/js-cookie": "^3.0.6",
4747
"@types/lodash-es": "^4.17.12",
4848
"@types/ua-parser-js": "^0.7.39",
49+
"@ungap/structured-clone": "^1.3.0",
4950
"commitizen": "^4.3.1",
5051
"commitlint": "^19.5.0",
5152
"conventional-changelog-cli": "^2.0.31",
5253
"cz-conventional-changelog": "^3.3.0",
54+
"fake-indexeddb": "^6.2.5",
5355
"gh-pages": "^6.3.0",
5456
"husky": "^9.1.6",
5557
"jest": "^29.7.0",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import IndexedDB from '../index';
2+
3+
describe('IndexedDB', () => {
4+
const DB_NAME = 'testDB';
5+
const STORE_NAME = 'testStore';
6+
const VERSION = 1;
7+
8+
let db: IndexedDB;
9+
10+
beforeEach(async () => {
11+
db = new IndexedDB(DB_NAME, VERSION, STORE_NAME);
12+
await db.open();
13+
});
14+
15+
afterEach(async () => {
16+
const req = indexedDB.deleteDatabase(DB_NAME);
17+
await new Promise((resolve) => {
18+
req.onsuccess = req.onerror = req.onblocked = () => resolve(null);
19+
});
20+
});
21+
22+
// open
23+
test('opens database successfully', async () => {
24+
const instance = await db.open();
25+
expect(instance).toBeDefined();
26+
expect(instance.name).toBe(DB_NAME);
27+
});
28+
29+
test('returns existing connection if already opened', async () => {
30+
const first = await db.open();
31+
const second = await db.open();
32+
expect(first).toBe(second);
33+
});
34+
35+
// 'add & get'
36+
test('adds and retrieves data', async () => {
37+
await db.add('user1', { name: 'John', age: 30 });
38+
39+
const result = await db.get<{ name: string; age: number }>('user1');
40+
41+
expect(result).toEqual({ name: 'John', age: 30 });
42+
});
43+
44+
// set(update)
45+
test('updates existing data', async () => {
46+
await db.add('user1', { name: 'John', age: 30 });
47+
await db.set('user1', { name: 'John', age: 31 });
48+
49+
const result = await db.get<{ age: number }>('user1');
50+
expect(result.age).toBe(31);
51+
});
52+
53+
// delete
54+
test('deletes data by key', async () => {
55+
await db.add('user1', { name: 'John' });
56+
await db.delete('user1');
57+
58+
const result = await db.get('user1');
59+
expect(result).toBeUndefined();
60+
});
61+
62+
// clear
63+
test('clears all data in store', async () => {
64+
await db.add('user1', { name: 'John' });
65+
await db.add('user2', { name: 'Jane' });
66+
67+
await db.clear();
68+
69+
const result1 = await db.get('user1');
70+
const result2 = await db.get('user2');
71+
72+
expect(result1).toBeUndefined();
73+
expect(result2).toBeUndefined();
74+
});
75+
76+
// ensureConnection / error handling
77+
test('throws error when operating before open', async () => {
78+
const newDB = new IndexedDB('newDB', 1, 'store');
79+
80+
await expect(newDB.get('key')).resolves.toBeUndefined();
81+
});
82+
83+
test('handles non-existing key gracefully', async () => {
84+
const result = await db.get('not-exist');
85+
expect(result).toBeUndefined();
86+
});
87+
88+
// version upgrade
89+
test('creates object store on upgrade', async () => {
90+
const oldDb = new IndexedDB('upgradeDB', 1, 'store1');
91+
await oldDb.open();
92+
93+
const newDb = new IndexedDB('upgradeDB', 2, 'store2');
94+
const instance = await newDb.open();
95+
96+
expect(instance.objectStoreNames.contains('store2')).toBe(true);
97+
});
98+
});

0 commit comments

Comments
 (0)