Skip to content

Commit

Permalink
feat: introduce decorators to simplify collection methods checks
Browse files Browse the repository at this point in the history
* use decorators for every collection method to ensure collection was created before
* chore: rename files to no prefix
  • Loading branch information
voznik authored Mar 12, 2024
1 parent 5ff78b0 commit 5332920
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 97 deletions.
4 changes: 2 additions & 2 deletions packages/rxdb/collection/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './lib/rxdb-collection.module';
export * from './lib/rxdb-collection.service';
export * from './lib/collection.module';
export * from './lib/collection.service';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, NgModule } from '@angular/core';
import { NgxRxdbCollection, NgxRxdbCollectionService } from './rxdb-collection.service';
import { NgxRxdbCollection, NgxRxdbCollectionService } from './collection.service';

/**
* (Fake) Feature module for NgxRxdbModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
import { MangoQuery, RxQuery } from 'rxdb';
import { createRxLocalDocument } from 'rxdb/plugins/local-documents';
import { RxReplicationState } from 'rxdb/plugins/replication';
import { EMPTY, Observable, Subject, firstValueFrom } from 'rxjs';
import { EMPTY, Observable, Subject, firstValueFrom, of } from 'rxjs';
import {
NgxRxdbCollection,
NgxRxdbCollectionService,
collectionServiceFactory,
} from './rxdb-collection.service';
} from './collection.service';

const getMockReplicationState = (obj: Partial<RxReplicationState<any, any>>) => {
obj.reSync = jest.fn();
Expand Down Expand Up @@ -75,15 +75,6 @@ describe(`NgxRxdbCollectionService`, () => {
expect(service.collection).toBeDefined();
});

it('should throw an error if collection is not initialized and initialized$ rejects', async () => {
service['_collection'] = null as any;
service['_init$'] = new Subject() as any;
service['_init$'].complete();
await expect(service['ensureCollection']()).rejects.toThrow(
`Collection "${service.config.name}" was not initialized. Please check previous RxDB errors.`
);
});

it('should destroy collection', async () => {
jest.spyOn(service.collection, 'destroy').mockResolvedValue(null as any);
await service.destroy();
Expand All @@ -96,14 +87,23 @@ describe(`NgxRxdbCollectionService`, () => {
expect(service.collection.remove).toHaveBeenCalled();
});

it('should call ensureCollection', async () => {
it('should always ensure collection is created asynchronously before calling any method (e.g. `info`)', async () => {
const spy = jest
.spyOn(service as any, 'ensureCollection')
.mockImplementation(() => Promise.resolve());
await service.sync();
.spyOn(service, 'initialized$', 'get')
.mockImplementation(() => of(true));
await service.info();
expect(spy).toHaveBeenCalled();
});

it('should throw an error if collection is not initialized and initialized$ rejects', async () => {
service['_collection'] = null as any;
service['_init$'] = new Subject() as any;
service['_init$'].complete();
await expect(service.info()).rejects.toThrow(
`Collection "${service.config.name}" was not initialized. Please check RxDB errors.`
);
});

it('should handle valid replicationState', async () => {
service.config.options = {
...service.config.options,
Expand Down
Loading

0 comments on commit 5332920

Please sign in to comment.