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

80% Statements 16/20
64.28% Branches 9/14
57.14% Functions 4/7
83.33% Lines 15/18

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 56 57 58 59 60 61 62 63 64 65 66 673x 3x 3x 3x   3x                                     3x     4x   4x   4x         4x     3x           3x 3x           1x 1x                          
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { Observable, map, shareReplay, take } from 'rxjs';
import { v4 as uuid } from 'uuid';
 
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { Warranty } from 'ish-core/models/warranty/warranty.model';
 
/**
 * The Product Warranty Component displays either a form element (select box or radio buttons), so the user can select a warranty or displays the selected warranty.
 * The available warranty options are provided by the product via the product context facade instance.
 * If the user selects a warranty a submitWarranty event is emitted.
 *
 * @example
 * <ish-product-warranty
 *   [selectedWarrantySku]="pli.warranty?.sku"
 *   viewType="select" />
 */
 
@Component({
  selector: 'ish-product-warranty',
  templateUrl: './product-warranty.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductWarrantyComponent implements OnInit {
  // preselect a warranty
  @Input() selectedWarrantySku: string;
  @Input() viewType: 'radio' | 'select' | 'display' = 'radio';
 
  @Output() submitWarranty = new EventEmitter<string>();
 
  uuid: string = uuid();
 
  warranties$: Observable<Warranty[]>;
  private noWarranty: Warranty;
 
  constructor(private productContext: ProductContextFacade, private translateService: TranslateService) {}
 
  ngOnInit() {
    this.noWarranty = {
      id: '',
      name: this.translateService.instant('product.warranty.no_protection_plan'),
      price: undefined,
    };
 
    this.warranties$ = this.productContext.select('product').pipe(
      map(product => (product.availableWarranties?.length ? [...product.availableWarranties, this.noWarranty] : [])),
      shareReplay(1)
    );
  }
 
  updateWarranty(warranty: string | EventTarget) {
    if (typeof warranty === 'string') {
      this.submitWarranty.emit(warranty);
    } else E{
      this.submitWarranty.emit(warranty ? (warranty as HTMLDataElement).value : '');
    }
  }
 
  getSelectedWarranty$(warrantySku: string) {
    return this.warranties$.pipe(
      map(warranties => warranties.find(warranty => warranty.id === warrantySku)),
      take(1)
    );
  }
}