Skip to content

Commit

Permalink
feat(navigation,demo): pull navigation tree from category in-memory t…
Browse files Browse the repository at this point in the history
…ree (#2632)
  • Loading branch information
griest024 committed Nov 30, 2023
1 parent 791cc7c commit 40cf496
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
20 changes: 18 additions & 2 deletions apps/demo/src/app/drivers/in-memory/in-memory.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { NgModule } from '@angular/core';
import {
NgModule,
inject,
} from '@angular/core';
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';

import { DaffAuthInMemoryDriverModule } from '@daffodil/auth/driver/in-memory';
import { DaffCartInMemoryDriverModule } from '@daffodil/cart/driver/in-memory';
import { DaffCategoryInMemoryDriverModule } from '@daffodil/category/driver/in-memory';
import { DaffInMemoryBackendCategoryService } from '@daffodil/category/driver/in-memory';
import { DaffCheckoutInMemoryDriverModule } from '@daffodil/checkout/testing';
import { DaffExternalRouterDriverInMemoryModule } from '@daffodil/external-router/driver/in-memory';
import { DaffGeographyInMemoryDriverModule } from '@daffodil/geography/driver/in-memory';
import { DaffNavigationInMemoryDriverModule } from '@daffodil/navigation/driver/in-memory';
import {
DaffNavigationInMemoryDriverModule,
DAFF_NAVIGATION_IN_MEMORY_SEED_DATA_PROVIDER,
} from '@daffodil/navigation/driver/in-memory';
import { DaffNewsletterInMemoryDriverModule } from '@daffodil/newsletter/driver/in-memory';
import { DaffProductInMemoryDriverModule } from '@daffodil/product/driver/in-memory';
import { DaffCompositeProductInMemoryDriverModule } from '@daffodil/product-composite/driver/in-memory';
Expand All @@ -32,5 +39,14 @@ import { DEMO_EXTERNAL_ROUTER_DRIVER_IN_MEMORY_CONFIG } from './external-router.
DaffCategoryInMemoryDriverModule.forRoot(),
DaffExternalRouterDriverInMemoryModule.forRoot(DEMO_EXTERNAL_ROUTER_DRIVER_IN_MEMORY_CONFIG),
],
providers: [
{
provide: DAFF_NAVIGATION_IN_MEMORY_SEED_DATA_PROVIDER,
useFactory: () => {
const categoryBackend = inject(DaffInMemoryBackendCategoryService);
return () => categoryBackend.categories[0];
},
},
],
})
export class DemoInMemoryDriverModule { }
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { TestBed } from '@angular/core/testing';

import { isNavigation } from '@daffodil/navigation/testing';
import { DaffNavigationTree } from '@daffodil/navigation';
import { DAFF_NAVIGATION_IN_MEMORY_SEED_DATA_PROVIDER } from '@daffodil/navigation/driver/in-memory';
import {
DaffNavigationTreeFactory,
isNavigation,
} from '@daffodil/navigation/testing';

import { DaffInMemoryBackendNavigationService } from './navigation.service';

describe('Driver | InMemory | Navigation | DaffInMemoryBackendNavigationService', () => {
let navigationTestingService;
describe('@daffodil/navigation/driver/in-memory | DaffInMemoryBackendNavigationService', () => {
let navigationTestingService: DaffInMemoryBackendNavigationService;
let navigationFactory: DaffNavigationTreeFactory;
let navigationTree: DaffNavigationTree;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [DaffInMemoryBackendNavigationService],
providers: [
DaffInMemoryBackendNavigationService,
{
provide: DAFF_NAVIGATION_IN_MEMORY_SEED_DATA_PROVIDER,
useFactory: () => {
navigationFactory = TestBed.inject(DaffNavigationTreeFactory);
navigationTree = navigationFactory.create();
return () => navigationTree;
},
},
],
});

navigationTestingService = TestBed.inject(DaffInMemoryBackendNavigationService);
Expand All @@ -19,6 +36,10 @@ describe('Driver | InMemory | Navigation | DaffInMemoryBackendNavigationService'
expect(navigationTestingService).toBeTruthy();
});

it('should seed the navigation tree from the provider', () => {
expect(navigationTestingService.navigationTree).toEqual(navigationTree);
});

describe('createDb', () => {
let result;

Expand Down
24 changes: 20 additions & 4 deletions libs/navigation/driver/in-memory/src/backend/navigation.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Injectable } from '@angular/core';
import {
Inject,
Injectable,
Optional,
} from '@angular/core';
import {
InMemoryDbService,
RequestInfoUtilities,
Expand All @@ -9,14 +13,26 @@ import {
import { DaffNavigationTree } from '@daffodil/navigation';
import { DaffNavigationTreeFactory } from '@daffodil/navigation/testing';

import {
DAFF_NAVIGATION_IN_MEMORY_SEED_DATA_PROVIDER,
DaffNavigationInMemorySeedDataProvider,
} from '../seed-data-provider/public_api';

@Injectable({
providedIn: 'root',
})
export class DaffInMemoryBackendNavigationService implements InMemoryDbService {
navigationTree: DaffNavigationTree;
private _navigationTree: DaffNavigationTree;

get navigationTree(): DaffNavigationTree {
return this.seedDataProvider?.() || this._navigationTree;
}

constructor(private navigationTreeFactory: DaffNavigationTreeFactory) {
this.navigationTree = this.navigationTreeFactory.create();
constructor(
private navigationTreeFactory: DaffNavigationTreeFactory,
@Inject(DAFF_NAVIGATION_IN_MEMORY_SEED_DATA_PROVIDER) @Optional() private seedDataProvider: DaffNavigationInMemorySeedDataProvider | null,
) {
this._navigationTree = seedDataProvider?.() || this.navigationTreeFactory.create();
}

parseRequestUrl(url: string, utils: RequestInfoUtilities): ParsedRequestUrl {
Expand Down

0 comments on commit 40cf496

Please sign in to comment.