Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(category): support in-memory backend delegate #3182

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ import {
} from '@daffodil/category/testing';
import { collect } from '@daffodil/core';
import { daffUriTruncateLeadingSlash } from '@daffodil/core/routing';
import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';
import { DaffProduct } from '@daffodil/product';
import { DaffInMemoryBackendProductService } from '@daffodil/product/driver/in-memory';

import { DAFF_CATEGORY_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';

/**
* An in-memory service that mocks out the backend services for getting categories. See the angular in memory documentation for more details.
*/
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryBackendCategoryService implements InMemoryDbService {
export class DaffInMemoryBackendCategoryService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_CATEGORY_IN_MEMORY_COLLECTION_NAME;

protected _root: DaffCategory;
protected _categories: DaffCategory[] = [];
protected _categoryPageMetadata: DaffCategoryPageMetadata;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_CATEGORY_IN_MEMORY_COLLECTION_NAME = 'category';
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
} from '@angular/core';

import { DaffCategoryDriver } from '@daffodil/category/driver';
import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory';

import { DaffInMemoryCategoryService } from './category.service';
import { DaffInMemoryBackendCategoryService } from '../backend/category.service';

/**
* A module that provides the {@link DaffInMemoryCategoryService} for the {@link DaffCategoryDriver} token.
Expand All @@ -25,6 +27,7 @@ export class DaffCategoryInMemoryDriverModule {
provide: DaffCategoryDriver,
useExisting: DaffInMemoryCategoryService,
},
provideDaffInMemoryBackends(DaffInMemoryBackendCategoryService),
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';

import { DaffCategoryRequestKind } from '@daffodil/category';
import { DaffCategoryFactory } from '@daffodil/category/testing';
Expand All @@ -15,7 +16,7 @@ import { DaffProductFactory } from '@daffodil/product/testing';
import { DaffInMemoryCategoryService } from './category.service';

describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', () => {
let categoryService: DaffInMemoryCategoryService;
let service: DaffInMemoryCategoryService;
let httpMock: HttpTestingController;
let categoryFactory: DaffCategoryFactory;
let productFactory: DaffProductFactory;
Expand All @@ -25,13 +26,19 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
imports: [],
providers: [
DaffInMemoryCategoryService,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
});

httpMock = TestBed.inject(HttpTestingController);
categoryService = TestBed.inject(DaffInMemoryCategoryService);
service = TestBed.inject(DaffInMemoryCategoryService);
categoryFactory = TestBed.inject(DaffCategoryFactory);
productFactory = TestBed.inject(DaffProductFactory);
});
Expand All @@ -41,7 +48,7 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
});

it('should be created', () => {
expect(categoryService).toBeTruthy();
expect(service).toBeTruthy();
});

describe('get | getting a single category by ID', () => {
Expand All @@ -50,14 +57,14 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
const mockCategory = categoryFactory.create();
const mockProducts = productFactory.createMany(3);

categoryService.get({ id: mockCategory.id, kind: DaffCategoryRequestKind.ID }).subscribe(categoryResponse => {
service.get({ id: mockCategory.id, kind: DaffCategoryRequestKind.ID }).subscribe(categoryResponse => {
expect(categoryResponse).toEqual(jasmine.objectContaining({
category: mockCategory,
products: mockProducts,
}));
});

const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(`${categoryService.url}`));
const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(`${service['url']}`));
expect(req.request.params.has('pageSize')).toBeTruthy();
expect(req.request.params.has('currentPage')).toBeTruthy();
expect(req.request.method).toBe('GET');
Expand All @@ -77,9 +84,9 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
const mockCategory = categoryFactory.create();
const mockProducts = productFactory.createMany(3);

categoryService.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();
service.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();

const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(categoryService.url));
const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(service['url']));
expect(req.request.url).not.toContain('//');

req.flush({ category: mockCategory, products: mockProducts });
Expand All @@ -89,9 +96,9 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
const mockCategory = categoryFactory.create();
const mockProducts = productFactory.createMany(3);

categoryService.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();
service.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();

const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(categoryService.url));
const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(service['url']));
expect(req.request.params.has('currentPage')).toBeTruthy();

req.flush({ category: mockCategory, products: mockProducts });
Expand All @@ -101,9 +108,9 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
const mockCategory = categoryFactory.create();
const mockProducts = productFactory.createMany(3);

categoryService.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();
service.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();

const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(categoryService.url));
const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(service['url']));
expect(req.request.params.has('pageSize')).toBeTruthy();

req.flush({ category: mockCategory, products: mockProducts });
Expand All @@ -113,9 +120,9 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
const mockCategory = categoryFactory.create();
const mockProducts = productFactory.createMany(3);

categoryService.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();
service.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe();

const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(categoryService.url));
const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(service['url']));
expect(req.request.method).toBe('GET');

req.flush({ category: mockCategory, products: mockProducts });
Expand All @@ -125,14 +132,14 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryCategoryService', ()
const mockCategory = categoryFactory.create();
const mockProducts = productFactory.createMany(3);

categoryService.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe(categoryResponse => {
service.getByUrl({ url: `/${url}`, kind: DaffCategoryRequestKind.URL }).subscribe(categoryResponse => {
expect(categoryResponse).toEqual(jasmine.objectContaining({
category: mockCategory,
products: mockProducts,
}));
});

const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(categoryService.url));
const req = httpMock.expectOne(request => request.method === 'GET' && request.url.includes(service['url']));

req.flush({ category: mockCategory, products: mockProducts });
});
Expand Down
19 changes: 13 additions & 6 deletions libs/category/driver/in-memory/src/drivers/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
HttpParams,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

import {
Expand All @@ -12,6 +13,9 @@ import {
} from '@daffodil/category';
import { DaffCategoryServiceInterface } from '@daffodil/category/driver';
import { daffUriTruncateLeadingSlash } from '@daffodil/core/routing';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

import { DAFF_CATEGORY_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';

/**
* The category in memory driver for mocking the {@link DaffCategoryDriver} with in memory data.
Expand All @@ -21,24 +25,27 @@ import { daffUriTruncateLeadingSlash } from '@daffodil/core/routing';
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryCategoryService implements DaffCategoryServiceInterface {
url = '/api/category/';

constructor(private http: HttpClient) {}
export class DaffInMemoryCategoryService extends DaffInMemoryDriverBase implements DaffCategoryServiceInterface {
constructor(
private http: HttpClient,
config: InMemoryBackendConfig,
) {
super(config, DAFF_CATEGORY_IN_MEMORY_COLLECTION_NAME);
}

get(categoryRequest: DaffCategoryIdRequest): Observable<DaffGetCategoryResponse> {
const params = new HttpParams()
.set('pageSize', categoryRequest.pageSize ? categoryRequest.pageSize.toString() : null)
.set('currentPage', categoryRequest.currentPage ? categoryRequest.currentPage.toString() : null);

return this.http.get<DaffGetCategoryResponse>(this.url + categoryRequest.id, { params });
return this.http.get<DaffGetCategoryResponse>(`${this.url}/${categoryRequest.id}`, { params });
}

getByUrl(categoryRequest: DaffCategoryUrlRequest): Observable<DaffGetCategoryResponse> {
const params = new HttpParams()
.set('pageSize', categoryRequest.pageSize ? categoryRequest.pageSize.toString() : null)
.set('currentPage', categoryRequest.currentPage ? categoryRequest.currentPage.toString() : null);

return this.http.get<DaffGetCategoryResponse>(`${this.url}${daffUriTruncateLeadingSlash(categoryRequest.url)}`, { params });
return this.http.get<DaffGetCategoryResponse>(`${this.url}/${daffUriTruncateLeadingSlash(categoryRequest.url)}`, { params });
}
}
Loading