All files / src/app/extensions/rating/shared/product-rating product-rating.component.ts

100% Statements 16/16
87.5% Branches 7/8
100% Functions 5/5
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 381x 1x   1x   1x 1x 1x                     1x 3x             3x     3x 3x 15x   3x 3x      
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { range } from 'lodash-es';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
 
import { ProductContextFacade } from 'ish-core/facades/product-context.facade';
import { ProductHelper } from 'ish-core/models/product/product.helper';
import { GenerateLazyComponent } from 'ish-core/utils/module-loader/generate-lazy-component.decorator';
 
/**
 * The Product Rating Component renders rating stars for a product (besides variation masters) with rounded average rating as number. *
 */
@Component({
  selector: 'ish-product-rating',
  templateUrl: './product-rating.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
@GenerateLazyComponent()
export class ProductRatingComponent implements OnInit {
  @Input() hideNumberOfReviews = false;
 
  stars$: Observable<('full' | 'half' | 'empty')[]>;
  rating$: Observable<number>;
  numberOfReviews$: Observable<number>;
  isVariationMaster$: Observable<boolean>;
 
  constructor(private context: ProductContextFacade) {}
 
  ngOnInit() {
    this.rating$ = this.context.select('product', 'roundedAverageRating').pipe(filter(x => typeof x === 'number'));
    this.stars$ = this.rating$.pipe(
      map(rate => range(1, 6).map(index => (index <= rate ? 'full' : index - 0.5 === rate ? 'half' : 'empty')))
    );
    this.numberOfReviews$ = this.context.select('product', 'numberOfReviews');
    this.isVariationMaster$ = this.context.select('product').pipe(map(ProductHelper.isMasterProduct));
  }
}