Skip to content

Commit 5332920

Browse files
authored
feat: introduce decorators to simplify collection methods checks
* use decorators for every collection method to ensure collection was created before * chore: rename files to no prefix
1 parent 5ff78b0 commit 5332920

18 files changed

+227
-97
lines changed

packages/rxdb/collection/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './lib/rxdb-collection.module';
2-
export * from './lib/rxdb-collection.service';
1+
export * from './lib/collection.module';
2+
export * from './lib/collection.service';

packages/rxdb/collection/src/lib/rxdb-collection.module.ts renamed to packages/rxdb/collection/src/lib/collection.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Inject, NgModule } from '@angular/core';
2-
import { NgxRxdbCollection, NgxRxdbCollectionService } from './rxdb-collection.service';
2+
import { NgxRxdbCollection, NgxRxdbCollectionService } from './collection.service';
33

44
/**
55
* (Fake) Feature module for NgxRxdbModule

packages/rxdb/collection/src/lib/rxdb-collection.service.spec.ts renamed to packages/rxdb/collection/src/lib/collection.service.spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import {
99
import { MangoQuery, RxQuery } from 'rxdb';
1010
import { createRxLocalDocument } from 'rxdb/plugins/local-documents';
1111
import { RxReplicationState } from 'rxdb/plugins/replication';
12-
import { EMPTY, Observable, Subject, firstValueFrom } from 'rxjs';
12+
import { EMPTY, Observable, Subject, firstValueFrom, of } from 'rxjs';
1313
import {
1414
NgxRxdbCollection,
1515
NgxRxdbCollectionService,
1616
collectionServiceFactory,
17-
} from './rxdb-collection.service';
17+
} from './collection.service';
1818

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

78-
it('should throw an error if collection is not initialized and initialized$ rejects', async () => {
79-
service['_collection'] = null as any;
80-
service['_init$'] = new Subject() as any;
81-
service['_init$'].complete();
82-
await expect(service['ensureCollection']()).rejects.toThrow(
83-
`Collection "${service.config.name}" was not initialized. Please check previous RxDB errors.`
84-
);
85-
});
86-
8778
it('should destroy collection', async () => {
8879
jest.spyOn(service.collection, 'destroy').mockResolvedValue(null as any);
8980
await service.destroy();
@@ -96,14 +87,23 @@ describe(`NgxRxdbCollectionService`, () => {
9687
expect(service.collection.remove).toHaveBeenCalled();
9788
});
9889

99-
it('should call ensureCollection', async () => {
90+
it('should always ensure collection is created asynchronously before calling any method (e.g. `info`)', async () => {
10091
const spy = jest
101-
.spyOn(service as any, 'ensureCollection')
102-
.mockImplementation(() => Promise.resolve());
103-
await service.sync();
92+
.spyOn(service, 'initialized$', 'get')
93+
.mockImplementation(() => of(true));
94+
await service.info();
10495
expect(spy).toHaveBeenCalled();
10596
});
10697

98+
it('should throw an error if collection is not initialized and initialized$ rejects', async () => {
99+
service['_collection'] = null as any;
100+
service['_init$'] = new Subject() as any;
101+
service['_init$'].complete();
102+
await expect(service.info()).rejects.toThrow(
103+
`Collection "${service.config.name}" was not initialized. Please check RxDB errors.`
104+
);
105+
});
106+
107107
it('should handle valid replicationState', async () => {
108108
service.config.options = {
109109
...service.config.options,

0 commit comments

Comments
 (0)