All files / src/app/shared/components/product/product-price product-price.component.ts

100% Statements 14/14
75% Branches 12/16
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 567x 7x 7x   7x 7x 7x             7x     12x                           12x     12x 12x   12x   12x 11x                                
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
 
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { Price, PriceHelper, Pricing } from 'ish-core/models/price/price.model';
import { ProductHelper } from 'ish-core/models/product/product.model';
 
@Component({
  selector: 'ish-product-price',
  templateUrl: './product-price.component.html',
  changeDetection: ChangeDetectionStrategy.Default,
})
export class ProductPriceComponent implements OnInit {
  @Input() showInformationalPrice: boolean;
  @Input() showPriceSavings: boolean;
  @Input() showScaledPrices = true;
 
  visible$: Observable<boolean>;
  isPriceRange$: Observable<boolean>;
  data$: Observable<
    {
      isListPriceGreaterThanSalePrice: boolean;
      isListPriceLessThanSalePrice: boolean;
      priceSavings: Price;
      lowerPrice: Price;
      upperPrice: Price;
    } & Pricing
  >;
 
  constructor(private context: ProductContextFacade) {}
 
  ngOnInit() {
    this.visible$ = this.context.select('displayProperties', 'price');
    this.isPriceRange$ = this.context
      .select('product')
      .pipe(map(product => ProductHelper.isMasterProduct(product) || ProductHelper.isRetailSet(product)));
 
    this.data$ = combineLatest([this.context.select('product'), this.context.select('prices')]).pipe(
      map(([product, prices]) => ({
        ...prices,
        isListPriceGreaterThanSalePrice: prices.listPrice?.value > prices.salePrice?.value,
        isListPriceLessThanSalePrice: prices.listPrice?.value < prices.salePrice?.value,
        priceSavings: prices.listPrice && prices.salePrice && PriceHelper.diff(prices.listPrice, prices.salePrice),
        lowerPrice:
          (ProductHelper.isMasterProduct(product) || ProductHelper.isRetailSet(product)) && prices.minSalePrice,
        upperPrice: ProductHelper.isMasterProduct(product)
          ? prices.maxSalePrice
          : ProductHelper.isRetailSet(product)
          ? prices.summedUpSalePrice
          : undefined,
      }))
    );
  }
}