-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(indexeddb): testing indexeddb implementation
Signed-off-by: Christiantyemele <yemelechristian2@gmail.com>
- Loading branch information
1 parent
731cd49
commit d44d91f
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {IndexedDBStorageService} from './StorageContext'; | ||
import 'fake-indexeddb/auto' | ||
|
||
declare const structuredClone: (obj: any) => any; | ||
if (!('structuredClone' in self)) { | ||
self["structuredClone"] = (obj: any) => { | ||
return JSON.parse(JSON.stringify(obj)); | ||
}; | ||
} | ||
|
||
describe('IndexedDBService', | ||
|
||
() => { | ||
let indexedDBService: IndexedDBStorageService; | ||
|
||
beforeAll(async () => { | ||
indexedDBService = new IndexedDBStorageService('testDB', 2); | ||
|
||
}); | ||
|
||
it('should open and close database', async () => { | ||
|
||
await indexedDBService.openDatabase('dbname', 2); | ||
|
||
expect(indexedDBService['db']).not.toBeNull(); | ||
|
||
}); | ||
|
||
it('should add data to the objectStore and retrieve it', async () => { | ||
|
||
const testData: any = {id: 1, name: 'Test'}; | ||
await indexedDBService.setItem(testData); | ||
|
||
const retrievedData = await indexedDBService.getItem(1); | ||
const revert = await indexedDBService.clear() | ||
|
||
expect(retrievedData).toEqual(testData); | ||
}); | ||
|
||
it('should get data from objectStore', async () => { | ||
const retrieveData = await indexedDBService.getItem(1); | ||
expect(retrieveData).toEqual(undefined) | ||
}) | ||
|
||
it('should delete data from the objectStore', async () => { | ||
|
||
const removedData = await indexedDBService.removeItem('keystore') | ||
|
||
const retrieveData = await indexedDBService.getItem('keystore') | ||
expect(retrieveData).toEqual(undefined) | ||
}); | ||
it('should clear all the data out of the object store', async () => { | ||
const testData = {id: 1, name: 'mark'} | ||
const testData2 = {id: "2", name: "christian"} | ||
|
||
const setData = await indexedDBService.setItem(testData) | ||
const setData2 = await indexedDBService.setItem(testData2) | ||
|
||
const clearData = await indexedDBService.clear() | ||
expect(await indexedDBService.getItem(1)).toEqual(undefined) | ||
}) | ||
}); |