Skip to content

Commit

Permalink
refactor: improve template related type compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Nov 6, 2020
1 parent 7f28f02 commit a3e1763
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
11 changes: 5 additions & 6 deletions src/app/core/models/product/product.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ export enum ProductCompletenessLevel {

export type AllProductTypes = Product | VariationProduct | VariationProductMaster | ProductBundle | ProductRetailSet;

export type AnyProductType = Product & VariationProduct & VariationProductMaster & ProductBundle & ProductRetailSet;

export type ProductPrices =
| Partial<Pick<ProductRetailSet, 'minListPrice' | 'minSalePrice' | 'summedUpListPrice' | 'summedUpSalePrice'>>
| Partial<Pick<VariationProductMaster, 'minListPrice' | 'minSalePrice' | 'maxListPrice' | 'maxSalePrice'>>
| Partial<Pick<Product, 'salePrice' | 'listPrice'>>;
export type ProductPrices = Partial<
Pick<ProductRetailSet, 'minListPrice' | 'minSalePrice' | 'summedUpListPrice' | 'summedUpSalePrice'>
> &
Partial<Pick<VariationProductMaster, 'minListPrice' | 'minSalePrice' | 'maxListPrice' | 'maxSalePrice'>> &
Partial<Pick<Product, 'salePrice' | 'listPrice'>>;

export class ProductHelper {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { instance, mock, when } from 'ts-mockito';

import { AccountFacade } from 'ish-core/facades/account.facade';
import { PricePipe } from 'ish-core/models/price/price.pipe';
import { AnyProductType } from 'ish-core/models/product/product.model';
import { ProductPrices } from 'ish-core/models/product/product.model';

import { ProductPriceComponent } from './product-price.component';

Expand All @@ -14,9 +14,22 @@ describe('Product Price Component', () => {
let fixture: ComponentFixture<ProductPriceComponent>;
let element: HTMLElement;
let translate: TranslateService;
let product: AnyProductType;
let product: ProductPrices;

beforeEach(async () => {
product = {
listPrice: {
type: 'Money',
value: 11,
currency: 'USD',
},
salePrice: {
type: 'Money',
value: 10,
currency: 'USD',
},
};

const accountFacade = mock(AccountFacade);
when(accountFacade.userPriceDisplayType$).thenReturn(of('gross'));

Expand All @@ -33,17 +46,6 @@ describe('Product Price Component', () => {
component = fixture.componentInstance;
translate.setDefaultLang('en');
translate.use('en');
product = { sku: 'sku' } as AnyProductType;
product.listPrice = {
type: 'Money',
value: 11,
currency: 'USD',
};
product.salePrice = {
type: 'Money',
value: 10,
currency: 'USD',
};
component.product = product;
element = fixture.nativeElement;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core';

import { Price, PriceHelper } from 'ish-core/models/price/price.model';
import { ProductRetailSet } from 'ish-core/models/product/product-retail-set.model';
import { VariationProductMaster } from 'ish-core/models/product/product-variation-master.model';
import { Product } from 'ish-core/models/product/product.model';

declare type PricesOfProducts = Partial<
Pick<Product, 'salePrice' | 'listPrice'> &
Pick<ProductRetailSet, 'summedUpSalePrice'> &
Pick<VariationProductMaster, 'minSalePrice' | 'maxSalePrice'>
>;
import { ProductPrices } from 'ish-core/models/product/product.model';

@Component({
selector: 'ish-product-price',
templateUrl: './product-price.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductPriceComponent implements OnChanges {
@Input() product: PricesOfProducts;
@Input() product: ProductPrices;
@Input() showInformationalPrice: boolean;
@Input() showPriceSavings: boolean;

Expand All @@ -29,7 +21,7 @@ export class ProductPriceComponent implements OnChanges {
this.applyPriceParameters(this.product);
}

private applyPriceParameters(product: PricesOfProducts) {
private applyPriceParameters(product: ProductPrices) {
if (product.listPrice && product.salePrice) {
this.isListPriceGreaterThanSalePrice = product.listPrice.value > product.salePrice.value;
this.isListPriceLessThanSalePrice = product.listPrice.value < product.salePrice.value;
Expand Down

0 comments on commit a3e1763

Please sign in to comment.