Skip to content

Commit

Permalink
test(indexeddb.service): add indexeddb test
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkmnk committed Jan 21, 2025
1 parent 1b4330a commit 03298f8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/ngrx-toolkit/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
displayName: 'ngrx-toolkit',
setupFiles: ['fake-indexeddb/auto'],
setupFiles: ['fake-indexeddb/auto', 'core-js'],
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
coverageDirectory: '../../coverage/libs/ngrx-toolkit',
Expand Down
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);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@ngrx/signals": "19.0.0",
"@ngrx/store": "19.0.0",
"@nx/angular": "20.3.0",
"core-js": "^3.40.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "0.15.0"
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03298f8

Please sign in to comment.