Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 5, 2024
1 parent 9d6f449 commit b284ac8
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 139 deletions.
7 changes: 2 additions & 5 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { QueryReplicationState } from './query-replication-state';
import { Query } from './query-state';
import { Registry } from './registry';
import { RelationalQuery } from './relational-query-state';
import { Search } from './search-state';
import { SubscribableBase } from './subscribable-base';
import { buildEndpointWithParams } from './utils';

Expand Down Expand Up @@ -148,16 +147,15 @@ export class Manager<TDatabase extends RxDatabase> extends SubscribableBase {
if (!this.queryStates.has(key)) {
const collection = this.getCollection(collectionName);
if (collection) {
const searchService = new Search({ collection, locale: this.locale });
const queryState = new Query<typeof collection>({
id: key,
collection,
initialParams,
hooks,
searchService,
endpoint,
errorSubject: this.subjects.error,
greedy,
locale: this.locale,
});

this.queryStates.set(key, queryState);
Expand All @@ -180,17 +178,16 @@ export class Manager<TDatabase extends RxDatabase> extends SubscribableBase {
if (!this.queryStates.has(key)) {
const collection = this.getCollection(collectionName);
if (collection) {
const searchService = new Search({ collection, locale: this.locale });
const queryState = new RelationalQuery<typeof collection>(
{
id: key,
collection,
initialParams,
hooks,
searchService,
endpoint,
errorSubject: this.subjects.error,
greedy,
locale: this.locale,
},
childQuery,
parentLookupQuery
Expand Down
18 changes: 12 additions & 6 deletions src/query-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
import { map, switchMap, distinctUntilChanged, debounceTime, tap, startWith } from 'rxjs/operators';

import { SubscribableBase } from './subscribable-base';
import { Search } from './search-state';

import type { Search } from './search-state';
import type { RxCollection, RxDocument } from 'rxdb';

// This type utility extracts the document type from a collection
Expand Down Expand Up @@ -50,10 +50,10 @@ export interface QueryConfig<T> {
collection: T;
initialParams?: QueryParams;
hooks?: QueryHooks;
searchService: Search;
endpoint?: string;
errorSubject: Subject<Error>;
greedy?: boolean;
locale?: string;
}

type WhereClause = { field: string; value: any };
Expand Down Expand Up @@ -111,21 +111,28 @@ export class Query<T extends RxCollection> extends SubscribableBase {
collection,
initialParams = {},
hooks,
searchService,
endpoint,
errorSubject,
greedy = false,
locale,
}: QueryConfig<T>) {
super();
this.id = id;
this.collection = collection;
this.hooks = hooks || {};
this.searchService = searchService;
this.endpoint = endpoint;
this.endpoint = endpoint; // @TODO - do we need this?
this.errorSubject = errorSubject;
this.primaryKey = collection.schema.primaryPath;
this.greedy = greedy;

/**
* Search service
*
* @TODO - we only need a full text search service for some collections and fields
* @TODO - we have full text search, but we need partial string search as well
*/
this.searchService = new Search({ collection, locale });

/**
* Set initial params
*/
Expand All @@ -144,7 +151,6 @@ export class Query<T extends RxCollection> extends SubscribableBase {
this.find$
.pipe(
distinctUntilChanged((prev, next) => {
console.log('DistinctUntilChanged', prev, next);
// Check if search is active and searchTerm has changed
if (prev.searchActive !== next.searchActive || prev.searchTerm !== next.searchTerm) {
return false;
Expand Down
8 changes: 8 additions & 0 deletions src/search-dbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ export async function maybeCreateSearchDB(collection, locale = '') {
searchDB.changed$ = changed.asObservable();
searchDBs.set(key, searchDB);

/**
* This is async, there is a small chance that the collection has been destroyed before the searchDB is created
*/
if(collection.destroyed) {
searchDBs.delete(key);
return searchDB;
}

/**
* Populate the searchDB with the stored records
*/
Expand Down
44 changes: 8 additions & 36 deletions tests/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('Manager', () => {
manager = new Manager(storeDatabase, syncDatabase, httpClientMock);
});

afterEach(() => {
storeDatabase.remove();
syncDatabase.remove();
afterEach(async () => {
await storeDatabase.destroy();
await syncDatabase.destroy();
manager.cancel();
jest.clearAllMocks();
});
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('Manager', () => {
const queryKeys = ['testQuery'];
manager.registerQuery({
queryKeys,
collectionName: 'testCollection',
collectionName: 'products',
initialParams: {},
});
expect(manager.hasQuery(queryKeys)).toBe(true);
Expand All @@ -57,16 +57,6 @@ describe('Manager', () => {
expect(manager.hasQuery(['nonExistentQuery'])).toBe(false);
});

it('should register a new query', () => {
const queryKeys = ['newQuery'];
manager.registerQuery({
queryKeys,
collectionName: 'products',
initialParams: {},
});
expect(manager.queries.has(JSON.stringify(queryKeys))).toBe(true);
});

it('should return the specified collection', () => {
expect(manager.getCollection('products')).toBeDefined();
});
Expand Down Expand Up @@ -109,7 +99,7 @@ describe('Manager', () => {
initialParams: {},
});
manager.deregisterQuery(queryKey);
expect(manager.queries.has(JSON.stringify(queryKey))).toBe(false);
expect(manager.hasQuery(['queryToRemove'])).toBe(false);
});

it('should cancel all queries and subscriptions', () => {
Expand Down Expand Up @@ -159,27 +149,9 @@ describe('Manager', () => {

expect(manager.replicationStates.has('testEndpoint')).toBe(true);

const replicationStatesForQuery1 = manager.getReplicationStatesByQueryKeys(['newQuery1']);
const replicationStatesForQuery2 = manager.getReplicationStatesByQueryKeys(['newQuery2']);
expect(replicationStatesForQuery1).toEqual(replicationStatesForQuery2);
const queryReplication1 = manager.getQuery(['newQuery1']);
const queryReplication2 = manager.getQuery(['newQuery2']);
expect(queryReplication1.collectionReplication).toEqual(queryReplication2.collectionReplication);
});
});

// describe('Query Replication States', () => {
// it('should register a new query replication states', () => {
// const query = manager.registerQuery({
// queryKeys: ['newQuery'],
// collectionName: 'testCollection',
// initialParams: {},
// });

// expect(manager.replicationStates.has('testCollection')).toBe(true);

// const queryReplicationStates = manager.queryReplicationStates.get('testCollection');
// expect(queryReplicationStates.size).toEqual(1);

// query?.where('status', 'completed');
// expect(queryReplicationStates.size).toEqual(2);
// });
// });
});
11 changes: 0 additions & 11 deletions tests/provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,6 @@ describe('QueryProvider', () => {
<TestComponent2 />
</QueryProvider>
);

/**
* It's important to wait for the render to complete before making assertions.
* In this case the 'maybeCreateSearchDB' is created asynchronously and cleanup is called
* before the searchDB is created.
*
* @TODO - find a better way to handle this
*/
await waitFor(() => {
expect(true).toBe(true); // Placeholder to wait for render
});
});

it('should have initial values for query.params$ and query.result$', (done) => {
Expand Down
Loading

0 comments on commit b284ac8

Please sign in to comment.