Skip to content

Commit

Permalink
feat(core,product,category)!: type model factory create partial and c…
Browse files Browse the repository at this point in the history
…onstructor args (#1835)

BREAKING CHANGE: partial arguments are now required to meet the model interfaces. Those that do not will cause a type check error
  • Loading branch information
griest024 committed Nov 24, 2023
1 parent ef17259 commit 6efc2c4
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class MockDaffCartWithStoreCredit extends MockCart implements DaffCartWit
@Injectable({
providedIn: 'root',
})
export class DaffCartWithStoreCreditFactory extends DaffModelFactory<DaffCartWithStoreCredit>{
export class DaffCartWithStoreCreditFactory extends DaffModelFactory<DaffCartWithStoreCredit, typeof MockDaffCartWithStoreCredit>{
constructor(
totalFactory: DaffCartTotalFactory,
shippingInformationFactory: DaffCartShippingInformationFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ describe('@daffodil/category/driver/in-memory | DaffInMemoryBackendCategoryServi
});

it('should set totalPages', () => {
const totalProducts = result.body.category.count;
const totalProducts = result.body.categoryPageMetadata.count;
const pageSize = result.body.categoryPageMetadata.pageSize;
expect(result.body.categoryPageMetadata.totalPages).toEqual(Math.ceil(totalProducts/pageSize));
});

it('should set no more products on the category than the pageSize', () => {
expect(result.body.categoryPageMetadata.product_ids.length).toBeLessThanOrEqual(result.body.categoryPageMetadata.pageSize);
expect(result.body.categoryPageMetadata.ids.length).toBeLessThanOrEqual(result.body.categoryPageMetadata.pageSize);
});

it('should set pageSize when the pageSize is provided', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class DaffInMemoryBackendCategoryService implements InMemoryDbService {
].map(id => {
const allCategoryProductIds = this.generateProductIdSubset(this.productInMemoryBackendService.products);

return this.categoryFactory.create({ id, url: `/${id}`, product_ids: allCategoryProductIds, count: allCategoryProductIds.length });
return this.categoryFactory.create({ id, url: `/${id}`, product_ids: allCategoryProductIds, total_products: allCategoryProductIds.length });
});
}

Expand Down Expand Up @@ -97,7 +97,7 @@ export class DaffInMemoryBackendCategoryService implements InMemoryDbService {
pageSize: this.generatePageSize(reqInfo),
currentPage: this.getCurrentPageParam(reqInfo),
totalPages: this.getTotalPages(category.product_ids, this.generatePageSize(reqInfo)),
product_ids: this.trimProductIdsToSinglePage(category.product_ids, this.getCurrentPageParam(reqInfo), this.generatePageSize(reqInfo)),
ids: this.trimProductIdsToSinglePage(category.product_ids, this.getCurrentPageParam(reqInfo), this.generatePageSize(reqInfo)),
count: category.total_products,
});

Expand Down
5 changes: 0 additions & 5 deletions libs/category/driver/magento/src/category.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ describe('@daffodil/category/driver/magento | DaffMagentoCategoryService', () =>
mockMagentoProduct = magentoProductFactory.create();
mockMagentoCategory = magentoCategoryFactory.create({
uid: mockCategory.id,
products: {
items: [
mockMagentoProduct,
],
},
});
mockMagentoProductSortFields = magentoSortFieldsFactory.create();
mockMagentoPriceAggregation = priceAggregateFactory.create();
Expand Down
12 changes: 6 additions & 6 deletions libs/core/testing/src/factories/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ import { IDaffModelFactory } from './factory.interface';
* }
* ```
*/
export abstract class DaffModelFactory<T extends Record<string, any>> implements IDaffModelFactory<T> {
_instantiationArgs: ConstructorParameters<Constructable<T>>;
export abstract class DaffModelFactory<T extends Record<string, any>, Klass extends Constructable<T> = Constructable<T>> implements IDaffModelFactory<T> {
_instantiationArgs: ConstructorParameters<Klass>;

constructor(
public type?: Constructable<T>,
...args: ConstructorParameters<Constructable<T>>
public type?: Klass,
...args: ConstructorParameters<Klass>
) {
this._instantiationArgs = args;
}

create(partial = {}): T {
create(partial: Partial<T> = {}): T {
if (!this.type) {
throw new Error('`type` is required if `create` is not overriden.');
}
Expand All @@ -59,7 +59,7 @@ export abstract class DaffModelFactory<T extends Record<string, any>> implements
};
}

createMany(qty = 1, partial = {}): T[] {
createMany(qty = 1, partial: Partial<T> = {}): T[] {
return range(0, qty - 1).map(() => this.create(partial));
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { Injectable } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import {
RequestInfo,
STATUS,
} from 'angular-in-memory-web-api';
import {
Observable,
of,
} from 'rxjs';

import { DaffPaymentResponse } from '@daffodil/payment';
import {
DaffPaymentResponseFactory,
DaffPaymentTestingModule,
} from '@daffodil/payment/testing';
import { DaffPaymentTestingModule } from '@daffodil/payment/testing';

import { DaffPaymentInMemoryBackendService } from './payment.service';

describe('@daffodil/payment/driver/in-memory | DaffPaymentInMemoryBackendService', () => {
let service: DaffPaymentInMemoryBackendService;
let resultFactory: DaffPaymentResponseFactory;
let mockResults1: DaffPaymentResponse[];
let mockResults2: DaffPaymentResponse[];

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -34,10 +22,6 @@ describe('@daffodil/payment/driver/in-memory | DaffPaymentInMemoryBackendService
});

service = TestBed.inject(DaffPaymentInMemoryBackendService);
resultFactory = TestBed.inject(DaffPaymentResponseFactory);

mockResults1 = resultFactory.createMany(3, { kind: 'TestBackend1' });
mockResults2 = resultFactory.createMany(3, { kind: 'TestBackend2' });
});

it('should be created', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class MockPaymentResponse implements DaffPaymentResponse {
@Injectable({
providedIn: 'root',
})
export class DaffPaymentResponseFactory extends DaffModelFactory<DaffPaymentResponse>{
export class DaffPaymentResponseFactory extends DaffModelFactory<DaffPaymentResponse, typeof MockPaymentResponse>{
constructor() {
super(MockPaymentResponse);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/product/testing/src/factories/extension.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class DaffProductExtensionFactory<T extends DaffProduct = DaffProduct> ex
* Includes extra product types that may be provided by optional product packages.
* This includes all the extra extension factories that may be provided by optional product packages.
*/
create(partial = {}): T {
create(partial: Partial<T> = {}): T {
const kind = this.productKindFactory.create(partial);

return Object.assign(
Expand Down
2 changes: 1 addition & 1 deletion libs/product/testing/src/factories/kind.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DaffProductKindFactory extends DaffModelFactory<DaffProduct> {
* Creates a mock product of random kind.
* Includes extra product kinds that may be provided by optional product packages.
*/
create(partial = {}): DaffProduct {
create(partial: Partial<DaffProduct> = {}): DaffProduct {
return this._randomFactory.create(partial);
}
}

0 comments on commit 6efc2c4

Please sign in to comment.