Skip to content

Commit

Permalink
add tests for categories selector
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 11, 2024
1 parent eb60d75 commit 9397202
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/query-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,25 @@ export class Query<T extends RxCollection> extends SubscribableBase {
return !!clause;
}

findElementSelectorID(field: string): any {}
findElementSelectorID(field: string): any {
for (const clause of this.whereClauses) {
if (clause.field === field && clause.value?.$elemMatch) {
const match = clause.value.$elemMatch.id;
if (match !== undefined) return match;
}
}
return undefined;
}

hasElementSelectorID(field: string, id: any): boolean {
for (const clause of this.whereClauses) {
if (clause.field === field && clause.value?.$elemMatch) {
const match = clause.value.$elemMatch.id;
if (match === id) return true;
}
}
return false;
}

findMetaDataSelector(key: string): any {
for (const clause of this.whereClauses) {
Expand Down
179 changes: 179 additions & 0 deletions tests/query-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,5 +755,184 @@ describe('Query', () => {
});
});

/**
*
*/
it('finds a category by ID', async () => {
const data = [
{ uuid: '1', name: 'Item 1', categories: [
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
{ uuid: '2', name: 'Item 2', categories: [
{
"id": 17,
"name": "Clothing",
"slug": "clothing"
},
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
{ uuid: '3', name: 'Item 3', categories: [
{
"id": 17,
"name": "Clothing",
"slug": "clothing"
}
] },
{ uuid: '4', name: 'Item 4', categories: [
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
{ uuid: '5', name: 'Item 5', categories: [
{
"id": 17,
"name": "Clothing",
"slug": "clothing"
},
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
];

const { success } = await storeDatabase.collections.products.bulkInsert(data);
expect(success.length).toBe(5);

const query = new Query({
collection: storeDatabase.collections.products,
initialParams: {
sortBy: 'name',
sortDirection: 'asc',
}
});

query.where('categories', { $elemMatch: { id: 17 } });
expect(query.getParams()).toEqual({
selector: { $and: [ { categories: { $elemMatch: { id: 17 } } } ] },
sortBy: 'name',
sortDirection: 'asc',
});

return new Promise((resolve) => {
query.result$.subscribe((result) => {
expect(result).toEqual(expect.objectContaining({
elapsed: expect.any(Number),
searchActive: false,
count: 3,
hits: expect.arrayContaining([
expect.objectContaining({ id: '2', document: expect.any(Object) }),
expect.objectContaining({ id: '3', document: expect.any(Object) }),
expect.objectContaining({ id: '5', document: expect.any(Object) })
])
}));

resolve();
});
});
});

/**
*
*/
it('replaces a category selector by ID', async () => {
const data = [
{ uuid: '1', name: 'Item 1', categories: [
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
{ uuid: '2', name: 'Item 2', categories: [
{
"id": 17,
"name": "Clothing",
"slug": "clothing"
},
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
{ uuid: '3', name: 'Item 3', categories: [
{
"id": 17,
"name": "Clothing",
"slug": "clothing"
}
] },
{ uuid: '4', name: 'Item 4', categories: [
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
{ uuid: '5', name: 'Item 5', categories: [
{
"id": 17,
"name": "Clothing",
"slug": "clothing"
},
{
"id": 21,
"name": "Music",
"slug": "music"
}
] },
];

const { success } = await storeDatabase.collections.products.bulkInsert(data);
expect(success.length).toBe(5);

const query = new Query({
collection: storeDatabase.collections.products,
initialParams: {
sortBy: 'name',
sortDirection: 'asc',
selector: {
categories: { $elemMatch: { id: 21 } }
}
}
});

query.where('categories', { $elemMatch: { id: 17 } });
expect(query.getParams()).toEqual({
selector: { $and: [ { categories: { $elemMatch: { id: 17 } } } ] },
sortBy: 'name',
sortDirection: 'asc',
});

return new Promise((resolve) => {
query.result$.subscribe((result) => {
expect(result).toEqual(expect.objectContaining({
elapsed: expect.any(Number),
searchActive: false,
count: 3,
hits: expect.arrayContaining([
expect.objectContaining({ id: '2', document: expect.any(Object) }),
expect.objectContaining({ id: '3', document: expect.any(Object) }),
expect.objectContaining({ id: '5', document: expect.any(Object) })
])
}));

resolve();
});
});
});

});
});

0 comments on commit 9397202

Please sign in to comment.