Skip to content

Commit

Permalink
feat(category): create tree in in-memory driver (#2631)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Nov 30, 2023
1 parent a033da4 commit 791cc7c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryBackendCategoryServi
expect(backend.categories.length).toBeGreaterThan(0);
});

it('should create a root category tree of depth 3 with products IDs from the product backend', () => {
expect(backend.rootCategory.children[0].children[0].children.length).toBeGreaterThan(0);
expect(backend.rootCategory.product_ids).toEqual(jasmine.arrayContaining(inMemoryBackendProductService.products.map(({ id }) => id)));
expect(backend.rootCategory.total_products).toEqual(inMemoryBackendProductService.products.length);
});

describe('get', () => {

let reqInfoStub;
Expand All @@ -46,7 +52,7 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryBackendCategoryServi
const paramsMap = new Map()
.set('pageSize', [stubPageSize])
.set('currentPage', [stubCurrentPage]);
id = '2001';
id = backend.rootCategory.id;
reqInfoStub = {
id,
req: {
Expand All @@ -64,7 +70,7 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryBackendCategoryServi

it('should return a GetCategoryResponse', () => {
expect(result.body).toEqual({
category: backend.categories[0],
category: backend.rootCategory,
categoryPageMetadata: backend.categoryPageMetadata,
products: inMemoryBackendProductService.products,
});
Expand Down
36 changes: 15 additions & 21 deletions libs/category/driver/in-memory/src/backend/category.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { faker } from '@faker-js/faker';
import {
InMemoryDbService,
RequestInfoUtilities,
Expand All @@ -14,7 +15,7 @@ import {
DaffCategoryFactory,
DaffCategoryPageMetadataFactory,
} from '@daffodil/category/testing';
import { randomSubset } from '@daffodil/core';
import { collect } from '@daffodil/core';
import { daffUriTruncateLeadingSlash } from '@daffodil/core/routing';
import { DaffProduct } from '@daffodil/product';
import { DaffInMemoryBackendProductService } from '@daffodil/product/driver/in-memory';
Expand All @@ -26,9 +27,14 @@ import { DaffInMemoryBackendProductService } from '@daffodil/product/driver/in-m
providedIn: 'root',
})
export class DaffInMemoryBackendCategoryService implements InMemoryDbService {
protected _root: DaffCategory;
protected _categories: DaffCategory[] = [];
protected _categoryPageMetadata: DaffCategoryPageMetadata;

get rootCategory(): DaffCategory {
return this._root;
}

/**
* The collection of categories in the backend.
*/
Expand All @@ -48,22 +54,14 @@ export class DaffInMemoryBackendCategoryService implements InMemoryDbService {
private metadataFactory: DaffCategoryPageMetadataFactory,
private productInMemoryBackendService: DaffInMemoryBackendProductService,
) {
this._categories = [
'2001',
'2002',
'2003',
'2004',
'2005',
'2006',
'2007',
'2008',
'2009',
'2010',
].map(id => {
const allCategoryProductIds = this.generateProductIdSubset(this.productInMemoryBackendService.products);

return this.categoryFactory.create({ id, url: `/${id}`, product_ids: allCategoryProductIds, total_products: allCategoryProductIds.length });
});
this._root = this.categoryFactory.createTree(
3,
this.productInMemoryBackendService.products.map(({ id }) => id),
);
this._categories = collect(
this._root,
(category) => category?.children || [],
);
}

/**
Expand Down Expand Up @@ -131,10 +129,6 @@ export class DaffInMemoryBackendCategoryService implements InMemoryDbService {
return tempIds;
}

protected generateProductIdSubset(products: DaffProduct[]): DaffProduct['id'][] {
return randomSubset(products).map(product => product.id);
}

protected generatePageSize(reqInfo) {
if(reqInfo.req.params.map && reqInfo.req.params.map.get('pageSize') && reqInfo.req.params.map.get('pageSize')[0]) {
return parseInt(reqInfo.req.params.map.get('pageSize')[0], 10);
Expand Down

0 comments on commit 791cc7c

Please sign in to comment.