From 193313de32a1d50150a80928cdcde572813606e1 Mon Sep 17 00:00:00 2001 From: 4gray Date: Sat, 15 May 2021 23:08:41 +0200 Subject: [PATCH] test: extend tests --- src/app/home/home.component.spec.ts | 124 +++++++++++++++++++- src/app/settings/settings.component.spec.ts | 2 +- 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/src/app/home/home.component.spec.ts b/src/app/home/home.component.spec.ts index b1f5ac7af..f4ef06c07 100644 --- a/src/app/home/home.component.spec.ts +++ b/src/app/home/home.component.spec.ts @@ -1,7 +1,10 @@ +import { UploadFile } from 'ngx-uploader'; +import { PLAYLIST_PARSE } from './../../../electron/ipc-commands'; +import { PLAYLIST_UPDATE } from './../../../ipc-commands'; import { TranslatePipe } from '@ngx-translate/core'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { MockComponent, MockModule, MockPipe } from 'ng-mocks'; -import { HomeComponent } from './home.component'; +import { HomeComponent, PlaylistMeta } from './home.component'; import { HeaderComponent } from '../shared/components/header/header.component'; import { RecentPlaylistsComponent } from '../home/recent-playlists/recent-playlists.component'; import { FileUploadComponent } from '../home/file-upload/file-upload.component'; @@ -12,6 +15,7 @@ import { MatProgressBarModule } from '@angular/material/progress-bar'; import { RouterTestingModule } from '@angular/router/testing'; import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar'; import { ElectronService } from '../services/electron.service'; +import { Router } from '@angular/router'; class MatSnackBarStub { open(): void {} @@ -34,6 +38,8 @@ export class ElectronServiceStub { describe('HomeComponent', () => { let component: HomeComponent; let fixture: ComponentFixture; + let electronService: ElectronService; + let router: Router; beforeEach(() => { TestBed.configureTestingModule({ @@ -50,7 +56,7 @@ describe('HomeComponent', () => { MockModule(MatIconModule), MockModule(MatProgressBarModule), MockModule(MatSnackBarModule), - RouterTestingModule, + MockModule(RouterTestingModule), ], providers: [ { provide: MatSnackBar, useClass: MatSnackBarStub }, @@ -62,6 +68,8 @@ describe('HomeComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(HomeComponent); component = fixture.componentInstance; + electronService = TestBed.inject(ElectronService); + router = TestBed.inject(Router); TestBed.inject(ElectronService); fixture.detectChanges(); }); @@ -69,4 +77,116 @@ describe('HomeComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + it('should send an event to the main process to remove a playlist', () => { + const playlistId = '12345'; + spyOn(electronService.ipcRenderer, 'send'); + component.removePlaylist(playlistId); + expect(electronService.ipcRenderer.send).toHaveBeenCalledWith( + 'playlist-remove-by-id', + { id: playlistId } + ); + }); + + it('should send an event to the main process to refresh a playlist', () => { + const playlistMeta: PlaylistMeta = { + _id: 'iptv1', + filePath: '/home/user/lists/iptv.m3u', + } as PlaylistMeta; + spyOn(electronService.ipcRenderer, 'send'); + component.refreshPlaylist(playlistMeta); + expect(electronService.ipcRenderer.send).toHaveBeenCalledWith( + PLAYLIST_UPDATE, + { id: playlistMeta._id, filePath: playlistMeta.filePath } + ); + }); + + it('should send an event to the main process to get a playlist', () => { + const playlistId = '6789'; + spyOn(electronService.ipcRenderer, 'send'); + component.getPlaylist(playlistId); + expect(electronService.ipcRenderer.send).toHaveBeenCalledWith( + 'playlist-by-id', + { + id: playlistId, + } + ); + }); + + it('should send an event to the main process to get a playlist by URL', () => { + const playlistTitle = 'playlist.m3u'; + const playlistUrl = 'http://test.com/' + playlistTitle; + spyOn(electronService.ipcRenderer, 'send'); + component.sendPlaylistsUrl(playlistUrl); + expect(electronService.ipcRenderer.send).toHaveBeenCalledWith( + 'parse-playlist-by-url', + { + title: playlistTitle, + url: playlistUrl, + } + ); + }); + + it('should send an event to the main process to parse a playlist', () => { + spyOn(electronService.ipcRenderer, 'send'); + const title = 'my-list.m3u'; + const path = '/home/user/iptv/' + title; + const playlistContent = 'test'; + const file: UploadFile = { + nativeFile: { path }, + name: title, + } as unknown as UploadFile; + const uploadEvent: Event = { + target: { result: playlistContent }, + } as unknown as Event; + component.handlePlaylist({ file, uploadEvent }); + expect(electronService.ipcRenderer.send).toHaveBeenCalledWith( + PLAYLIST_PARSE, + { title, playlist: [playlistContent], path } + ); + }); + + it('should return the last last segment from an url', () => { + expect(component.getLastUrlSegment('http://example.com')).toEqual( + 'example.com' + ); + expect( + component.getLastUrlSegment('http://example.com/playlist.m3u') + ).toEqual('playlist.m3u'); + expect( + component.getLastUrlSegment('http://example.com/playlist.m3u/') + ).toEqual(''); + }); + + it('should set IPC event listeners', () => { + spyOn(electronService.ipcRenderer, 'on'); + component.setRendererListeners(); + expect(electronService.ipcRenderer.on).toHaveBeenCalledTimes( + component.commandsList.length + ); + }); + + it('should send notification on file reject', () => { + spyOn(component, 'showNotification'); + component.rejectFile('wrong-file.txt'); + expect(component.showNotification).toHaveBeenCalledTimes(1); + }); + + it('should navigate to the player view', () => { + spyOn(router, 'navigateByUrl'); + component.navigateToPlayer(); + expect(router.navigateByUrl).toHaveBeenCalledTimes(1); + expect(router.navigateByUrl).toHaveBeenCalledWith( + '/iptv', + expect.anything() + ); + }); + + it('should remove all ipc listeners on destroy', () => { + spyOn(electronService.ipcRenderer, 'removeAllListeners'); + component.ngOnDestroy(); + expect( + electronService.ipcRenderer.removeAllListeners + ).toHaveBeenCalledTimes(component.commandsList.length); + }); }); diff --git a/src/app/settings/settings.component.spec.ts b/src/app/settings/settings.component.spec.ts index c58efe5bc..7d167f287 100644 --- a/src/app/settings/settings.component.spec.ts +++ b/src/app/settings/settings.component.spec.ts @@ -26,7 +26,7 @@ class MatSnackBarStub { open(): void {} } -class MockRouter { +export class MockRouter { navigateByUrl(url: string) { return url; }