All files / src/app/shared/components/language-selector language-selector.component.ts

90.32% Statements 28/31
70% Branches 7/10
85.71% Functions 6/7
88.46% Lines 23/26

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 6097x   97x 97x 97x   97x 97x             97x   97x   14x     14x   97x 26x 26x   26x                     97x 26x 26x                   97x 12x     97x 12x 12x   97x  
import { Component, Input, OnInit } from '@angular/core';
 
import * as _ from 'lodash';
import { defineLocale } from 'ngx-bootstrap/chronos';
import { BsLocaleService } from 'ngx-bootstrap/datepicker';
 
import { LanguageService } from '../../services/language.service';
import { languageBootstrapMapping, SupportedLanguages } from './supported-languages.enum';
 
@Component({
  selector: 'cd-language-selector',
  template: require('./language-selector.component.html'),
  styles: []
})
export class LanguageSelectorComponent implements OnInit {
  @Input()
  isDropdown = true;
 
  supportedLanguages: Object = SupportedLanguages;
  selectedLanguage: string;
 
  constructor(private localeService: BsLocaleService, private languageService: LanguageService) {}
 
  ngOnInit() {
    this.selectedLanguage = this.languageService.getLocale();
    this.defineUsedLanguage();
 
    this.languageService.getLanguages().subscribe((langs) => {
      this.supportedLanguages = _.pick(this.supportedLanguages, langs) as Object;
    });
  }
 
  /**
   * Sets ngx-bootstrap local based on the current language selection
   *
   * ngx-bootstrap locals documentation:
   * https://valor-software.com/ngx-bootstrap/#/datepicker#locales
   */
  private defineUsedLanguage() {
    const lang = this.selectedLanguage.slice(0, 2);
    Iif (lang in languageBootstrapMapping) {
      defineLocale(lang, languageBootstrapMapping[lang]);
      this.localeService.use(lang);
    }
  }
 
  /**
   * Jest is being more restricted regarding spying on the reload method.
   * This will allow us to spyOn this method instead.
   */
  reloadWindow() {
    window.location.reload();
  }
 
  changeLanguage(lang: string) {
    this.languageService.setLocale(lang);
    this.reloadWindow();
  }
}