-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(indexeddb.service): add indexeddb test
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
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
88 changes: 88 additions & 0 deletions
88
libs/ngrx-toolkit/src/lib/storageSync/tests/indexeddb.service.spec.ts
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,88 @@ | ||
import { IndexedDBService, keyPath } from '../internal/indexeddb.service'; | ||
|
||
describe('IndexedDBService', () => { | ||
const dbName = 'ngrx-toolkit'; | ||
|
||
const storeName = 'users'; | ||
|
||
const sampleData = { | ||
foo: 'bar', | ||
users: [ | ||
{ name: 'John', age: 30, isAdmin: true }, | ||
{ name: 'Jane', age: 25, isAdmin: false }, | ||
], | ||
}; | ||
|
||
let indexedDBService: IndexedDBService; | ||
|
||
beforeEach(() => { | ||
indexedDBService = new IndexedDBService(); | ||
}); | ||
|
||
it('It should be possible to write data using write() and then read the data using read()', async (): Promise<void> => { | ||
const expectedData = { [keyPath]: keyPath, value: sampleData }; | ||
|
||
await indexedDBService.write(dbName, storeName, sampleData); | ||
|
||
const receivedData = await indexedDBService.read(dbName, storeName); | ||
|
||
expect(receivedData).toEqual(expectedData); | ||
}); | ||
|
||
it('It should be possible to delete data using clear()', async (): Promise<void> => { | ||
await indexedDBService.write(dbName, storeName, sampleData); | ||
|
||
await indexedDBService.clear(dbName, storeName); | ||
|
||
const receivedData = await indexedDBService.read(dbName, storeName); | ||
|
||
expect(receivedData).toBeUndefined(); | ||
}); | ||
|
||
it('When there is no data, read() should return undefined', async (): Promise<void> => { | ||
const receivedData = await indexedDBService.read(dbName, storeName); | ||
|
||
expect(receivedData).toBeUndefined(); | ||
}); | ||
|
||
it('write() should handle null data', async (): Promise<void> => { | ||
await indexedDBService.write(dbName, storeName, null); | ||
|
||
const receivedData = await indexedDBService.read(dbName, storeName); | ||
|
||
expect(receivedData).toEqual({ [keyPath]: keyPath, value: null }); | ||
}); | ||
|
||
it('write() should handle empty object data', async (): Promise<void> => { | ||
const emptyData = {}; | ||
const expectedData = { [keyPath]: keyPath, value: emptyData }; | ||
|
||
await indexedDBService.write(dbName, storeName, emptyData); | ||
|
||
const receivedData = await indexedDBService.read(dbName, storeName); | ||
|
||
expect(receivedData).toEqual(expectedData); | ||
}); | ||
|
||
it('write() should handle large data objects', async (): Promise<void> => { | ||
const largeData = { foo: 'a'.repeat(1000000) }; | ||
const expectedData = { [keyPath]: keyPath, value: largeData }; | ||
|
||
await indexedDBService.write(dbName, storeName, largeData); | ||
|
||
const receivedData = await indexedDBService.read(dbName, storeName); | ||
|
||
expect(receivedData).toEqual(expectedData); | ||
}); | ||
|
||
it('write() should handle special characters in data', async (): Promise<void> => { | ||
const specialCharData = { foo: 'bar!@#$%^&*()_+{}:"<>?' }; | ||
const expectedData = { [keyPath]: keyPath, value: specialCharData }; | ||
|
||
await indexedDBService.write(dbName, storeName, specialCharData); | ||
|
||
const receivedData = await indexedDBService.read(dbName, storeName); | ||
|
||
expect(receivedData).toEqual(expectedData); | ||
}); | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.