Add frontend unit tests (Part 2)
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {EnvironmentInjector, runInInjectionContext} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {of, throwError} from 'rxjs';
|
||||
|
||||
import {UtilityService} from './utility.service';
|
||||
|
||||
describe('UtilityService', () => {
|
||||
let service: UtilityService;
|
||||
let httpClientMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
UtilityService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(
|
||||
injector,
|
||||
() => TestBed.inject(UtilityService)
|
||||
);
|
||||
});
|
||||
|
||||
it('should get folders for a given path', () => {
|
||||
httpClientMock.get.mockReturnValue(of(['folder1', 'folder2']));
|
||||
service.getFolders('/some/path').subscribe(folders => {
|
||||
expect(folders).toEqual(['folder1', 'folder2']);
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(
|
||||
expect.stringContaining('/api/v1/path'),
|
||||
{params: expect.anything()}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle error when getting folders', () => {
|
||||
httpClientMock.get.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.getFolders('/bad/path').subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('UtilityService - API Contract Tests', () => {
|
||||
let service: UtilityService;
|
||||
let httpClientMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
UtilityService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(
|
||||
injector,
|
||||
() => TestBed.inject(UtilityService)
|
||||
);
|
||||
});
|
||||
|
||||
it('should call correct endpoint and params for getFolders', () => {
|
||||
httpClientMock.get.mockReturnValue(of(['a', 'b']));
|
||||
service.getFolders('/root').subscribe();
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/\/api\/v1\/path$/),
|
||||
{params: expect.objectContaining({get: expect.any(Function)})}
|
||||
);
|
||||
});
|
||||
|
||||
it('should expect string[] from getFolders', () => {
|
||||
httpClientMock.get.mockReturnValue(of(['dirA', 'dirB']));
|
||||
service.getFolders('/dir').subscribe(result => {
|
||||
expect(Array.isArray(result)).toBe(true);
|
||||
expect(typeof result[0]).toBe('string');
|
||||
});
|
||||
});
|
||||
|
||||
it('should send correct query param for path', () => {
|
||||
httpClientMock.get.mockImplementation((url: string, opts: any) => {
|
||||
expect(opts.params.get('path')).toBe('/my/path');
|
||||
return of([]);
|
||||
});
|
||||
service.getFolders('/my/path').subscribe();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,7 +10,7 @@ import {Subject} from 'rxjs';
|
||||
|
||||
import {BookService} from '../../../features/book/service/book.service';
|
||||
import {Book} from '../../../features/book/model/book.model';
|
||||
import {FileMoveRequest, FileOperationsService} from '../../service/file-operations-service';
|
||||
import {FileMoveRequest, FileOperationsService} from '../../service/file-operations.service';
|
||||
import {LibraryService} from "../../../features/book/service/library.service";
|
||||
import {AppSettingsService} from '../../service/app-settings.service';
|
||||
import {Select} from 'primeng/select';
|
||||
@@ -393,8 +393,4 @@ export class FileMoverComponent implements OnDestroy {
|
||||
togglePatternsCollapsed(): void {
|
||||
this.patternsCollapsed = !this.patternsCollapsed;
|
||||
}
|
||||
|
||||
trackByBookId(index: number, preview: FilePreview): number {
|
||||
return preview.bookId;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import {Tooltip} from 'primeng/tooltip';
|
||||
import {MessageService} from 'primeng/api';
|
||||
|
||||
import {MetadataBatchProgressNotification, MetadataBatchStatus, MetadataBatchStatusLabels} from '../../model/metadata-batch-progress.model';
|
||||
import {MetadataProgressService} from '../../service/metadata-progress-service';
|
||||
import {MetadataProgressService} from '../../service/metadata-progress.service';
|
||||
import {MetadataTaskService} from '../../../features/book/service/metadata-task';
|
||||
import {Tag} from 'primeng/tag';
|
||||
import {TaskService} from '../../../features/settings/task-management/task.service';
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {EnvironmentInjector, runInInjectionContext} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {of, throwError} from 'rxjs';
|
||||
|
||||
import {SetupPayload, SetupService} from './setup.service';
|
||||
|
||||
describe('SetupService', () => {
|
||||
let service: SetupService;
|
||||
let httpClientMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
post: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
SetupService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(
|
||||
injector,
|
||||
() => TestBed.inject(SetupService)
|
||||
);
|
||||
});
|
||||
|
||||
it('should create admin with correct payload', () => {
|
||||
httpClientMock.post.mockReturnValue(of(void 0));
|
||||
const payload: SetupPayload = {email: 'admin@test.com', password: 'pass'};
|
||||
service.createAdmin(payload).subscribe(result => {
|
||||
expect(result).toBeUndefined();
|
||||
expect(httpClientMock.post).toHaveBeenCalledWith(
|
||||
expect.stringContaining('/api/v1/setup'),
|
||||
payload
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle error when creating admin', () => {
|
||||
httpClientMock.post.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.createAdmin({email: 'fail', password: 'fail'}).subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('SetupService - API Contract Tests', () => {
|
||||
let service: SetupService;
|
||||
let httpClientMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
post: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
SetupService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(
|
||||
injector,
|
||||
() => TestBed.inject(SetupService)
|
||||
);
|
||||
});
|
||||
|
||||
it('should call correct endpoint for createAdmin', () => {
|
||||
httpClientMock.post.mockReturnValue(of(void 0));
|
||||
const payload: SetupPayload = {email: 'a@b.com', password: 'pw'};
|
||||
service.createAdmin(payload).subscribe();
|
||||
expect(httpClientMock.post).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/\/api\/v1\/setup$/),
|
||||
payload
|
||||
);
|
||||
});
|
||||
|
||||
it('should expect void from createAdmin', () => {
|
||||
httpClientMock.post.mockReturnValue(of(void 0));
|
||||
service.createAdmin({email: 'x@y.com', password: 'pw'}).subscribe(result => {
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it('should send correct payload structure', () => {
|
||||
httpClientMock.post.mockImplementation((url: string, body: SetupPayload) => {
|
||||
expect(body).toHaveProperty('email');
|
||||
expect(body).toHaveProperty('password');
|
||||
return of(void 0);
|
||||
});
|
||||
service.createAdmin({email: 'z@z.com', password: 'pw'}).subscribe();
|
||||
});
|
||||
});
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
import {Component, inject} from '@angular/core';
|
||||
import {LiveNotificationBoxComponent} from '../live-notification-box/live-notification-box.component';
|
||||
import {MetadataProgressService} from '../../service/metadata-progress-service';
|
||||
import {MetadataProgressService} from '../../service/metadata-progress.service';
|
||||
import {map} from 'rxjs/operators';
|
||||
import {AsyncPipe} from '@angular/common';
|
||||
import {BookdropFileService} from '../../../features/bookdrop/service/bookdrop-file.service';
|
||||
|
||||
Reference in New Issue
Block a user