test: increase test coverage

This commit is contained in:
4gray authored and GitHub committed 2021-10-26 19:55:55 +00:00
1 parent c0255c8a48
commit 21316cef3d
11 files changed
+216 -110

No files matched your search

+101 -81
View File
@@ -1,11 +1,11 @@
import { TranslateService } from '@ngx-translate/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { StorageMap } from '@ngx-pwa/local-storage';
import { Router } from '@angular/router';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Observable, Subscription } from 'rxjs';
import { Settings, VideoPlayer } from './settings.interface';
import { Settings, SETTINGS_STORE_KEY, VideoPlayer } from './settings.interface';
import { HttpClient } from '@angular/common/http';
import * as semver from 'semver';
import { ElectronService } from '../services/electron.service';
@@ -14,6 +14,7 @@ import { EPG_FETCH } from '../../../shared/ipc-commands';
import { Language } from './language.enum';
import { Theme } from './theme.enum';
import { SettingsService } from './../services/settings.service';
import { catchError } from 'rxjs/operators';
/** Url of the package.json file in the app repository, required to get the version of the released app */
const PACKAGE_JSON_URL =
@@ -24,10 +25,7 @@ const PACKAGE_JSON_URL =
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.scss'],
})
export class SettingsComponent implements OnInit, OnDestroy {
/** Subscription object */
private subscription: Subscription = new Subscription();
export class SettingsComponent implements OnInit {
/** List with available languages as enum */
languageEnum = Language;
@@ -61,7 +59,8 @@ export class SettingsComponent implements OnInit, OnDestroy {
themeEnum = Theme;
/**
* Creates an instance of SettingsComponent and injects some dependencies into the component
* Creates an instance of SettingsComponent and injects
* required dependencies into the component
* @param channelQuery
* @param electronService
* @param formBuilder
@@ -91,84 +90,112 @@ export class SettingsComponent implements OnInit, OnDestroy {
theme: Theme.LightTheme,
});
this.subscription.add(
this.http
.get(PACKAGE_JSON_URL)
.subscribe((response: { version: string }) => {
this.version = this.electronService.getAppVersion();
const isOutdated = semver.lt(
this.version,
response.version
);
if (isOutdated) {
this.updateMessage = `${
this.translate.instant(
'SETTINGS.NEW_VERSION_AVAILABLE'
) as string
}: ${response.version}`;
} else {
this.updateMessage = this.translate.instant(
'SETTINGS.LATEST_VERSION'
);
}
})
);
this.checkAppVersion();
}
/**
* Reads the config object from the browsers storage (indexed db)
* Reads the config object from the browsers
* storage (indexed db)
*/
ngOnInit(): void {
this.subscription.add(
this.storage.get('settings').subscribe((settings: Settings) => {
if (settings) {
this.settingsForm.setValue({
player: settings.player
? settings.player
: VideoPlayer.VideoJs,
epgUrl: settings.epgUrl ? settings.epgUrl : '',
language: settings.language
? settings.language
: Language.ENGLISH,
showCaptions: settings.showCaptions
? settings.showCaptions
: false,
theme: settings.theme
? settings.theme
: Theme.LightTheme,
});
}
})
this.storage.get(SETTINGS_STORE_KEY).subscribe((settings: Settings) => {
if (settings) {
this.settingsForm.setValue({
player: settings.player
? settings.player
: VideoPlayer.VideoJs,
epgUrl: settings.epgUrl ? settings.epgUrl : '',
language: settings.language
? settings.language
: Language.ENGLISH,
showCaptions: settings.showCaptions
? settings.showCaptions
: false,
theme: settings.theme
? settings.theme
: Theme.LightTheme,
});
}
});
}
/**
* Checks whether the latest version of the application
* is used and updates the version message in the
* settings UI
*/
checkAppVersion(): void {
this.http
.get(PACKAGE_JSON_URL)
.pipe(catchError(err => {
console.error(err);
throw new Error(err);
}))
.subscribe((response: { version: string }) => {
this.showVersionInformation(response.version);
});
}
/**
* Updates the message in settings UI about the used
* version of the app
* @param currentVersion current version of the application
*/
showVersionInformation(currentVersion: string): void {
const isOutdated = this.isCurrentVersionOutdated(currentVersion);
if (isOutdated) {
this.updateMessage = `${
this.translate.instant(
'SETTINGS.NEW_VERSION_AVAILABLE'
) as string
}: ${currentVersion}`;
} else {
this.updateMessage = this.translate.instant(
'SETTINGS.LATEST_VERSION'
);
}
}
/**
* Compares actual with latest version of the
* application
* @param latestVersion latest version
* @returns returns true if an update is available
*/
isCurrentVersionOutdated(latestVersion: string): boolean {
this.version = this.electronService.getAppVersion();
return semver.lt(
this.version,
latestVersion
);
}
/**
* Triggers on form submit and saves the config object to the indexed db store
* Triggers on form submit and saves the config object to
* the indexed db store
*/
onSubmit(): void {
this.subscription.add(
this.storage
.set('settings', this.settingsForm.value)
.subscribe(() => {
this.settingsForm.markAsPristine();
// check whether the epg url was changed or not
if (this.settingsForm.value.epgUrl) {
this.fetchEpg();
this.storage
.set(SETTINGS_STORE_KEY, this.settingsForm.value)
.subscribe(() => {
this.settingsForm.markAsPristine();
// check whether the epg url was changed or not
if (this.settingsForm.value.epgUrl) {
this.fetchEpg();
}
this.translate.use(this.settingsForm.value.language);
this.settingsService.changeTheme(
this.settingsForm.value.theme
);
this.snackBar.open(
this.translate.instant('SETTINGS.SETTINGS_SAVED'),
null,
{
duration: 2000,
}
this.translate.use(this.settingsForm.value.language);
this.settingsService.changeTheme(
this.settingsForm.value.theme
);
this.snackBar.open(
this.translate.instant('SETTINGS.SETTINGS_SAVED'),
null,
{
duration: 2000,
}
);
})
);
);
});
}
/**
@@ -178,18 +205,11 @@ export class SettingsComponent implements OnInit, OnDestroy {
this.router.navigateByUrl('/', { skipLocationChange: true });
}
/**
* Unsubscribe on destroy
*/
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
/**
* Fetches and updates EPG from the given URL
*/
fetchEpg(): void {
this.electronService.ipcRenderer.send(EPG_FETCH, {
this.electronService.sendIpcEvent(EPG_FETCH, {
url: this.settingsForm.value.epgUrl,
});
this.snackBar.open(this.translate.instant('EPG.FETCH_EPG'), 'Close', {