Add frontend unit tests (Part 1)
This commit is contained in:
+203
@@ -0,0 +1,203 @@
|
||||
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 {KoboService, KoboSyncSettings} from './kobo.service';
|
||||
|
||||
describe('KoboService', () => {
|
||||
let service: KoboService;
|
||||
let httpClientMock: any;
|
||||
|
||||
const mockSettings: KoboSyncSettings = {
|
||||
token: 'abc123',
|
||||
syncEnabled: true,
|
||||
progressMarkAsReadingThreshold: 10,
|
||||
progressMarkAsFinishedThreshold: 90,
|
||||
autoAddToShelf: false,
|
||||
hardcoverApiKey: 'hardcover-key',
|
||||
hardcoverSyncEnabled: true
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn(),
|
||||
put: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
KoboService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(injector, () => TestBed.inject(KoboService));
|
||||
});
|
||||
|
||||
it('should get user Kobo settings', () => {
|
||||
httpClientMock.get.mockReturnValue(of(mockSettings));
|
||||
service.getUser().subscribe(settings => {
|
||||
expect(settings).toEqual(mockSettings);
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringContaining('/api/v1/kobo-settings'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should create or update token', () => {
|
||||
httpClientMock.put.mockReturnValue(of(mockSettings));
|
||||
service.createOrUpdateToken().subscribe(settings => {
|
||||
expect(settings).toEqual(mockSettings);
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringContaining('/api/v1/kobo-settings/token'), null);
|
||||
});
|
||||
});
|
||||
|
||||
it('should update settings', () => {
|
||||
httpClientMock.put.mockReturnValue(of(mockSettings));
|
||||
service.updateSettings(mockSettings).subscribe(settings => {
|
||||
expect(settings).toEqual(mockSettings);
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringContaining('/api/v1/kobo-settings'), mockSettings);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle getUser error', () => {
|
||||
httpClientMock.get.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.getUser().subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle createOrUpdateToken error', () => {
|
||||
httpClientMock.put.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.createOrUpdateToken().subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle updateSettings error', () => {
|
||||
httpClientMock.put.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.updateSettings(mockSettings).subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('KoboService - API Contract Tests', () => {
|
||||
let service: KoboService;
|
||||
let httpClientMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn(),
|
||||
put: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
KoboService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(injector, () => TestBed.inject(KoboService));
|
||||
});
|
||||
|
||||
it('should validate all required KoboSyncSettings fields exist', () => {
|
||||
const requiredFields: (keyof KoboSyncSettings)[] = [
|
||||
'token', 'syncEnabled', 'autoAddToShelf'
|
||||
];
|
||||
|
||||
const mockResponse: KoboSyncSettings = {
|
||||
token: 'abc',
|
||||
syncEnabled: true,
|
||||
autoAddToShelf: false,
|
||||
progressMarkAsReadingThreshold: 10,
|
||||
progressMarkAsFinishedThreshold: 90,
|
||||
hardcoverApiKey: 'hardcover-key',
|
||||
hardcoverSyncEnabled: true
|
||||
};
|
||||
|
||||
httpClientMock.get.mockReturnValue(of(mockResponse));
|
||||
|
||||
service.getUser().subscribe(settings => {
|
||||
requiredFields.forEach(field => {
|
||||
expect(settings).toHaveProperty(field);
|
||||
expect(settings[field]).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if API returns KoboSyncSettings without required token field', () => {
|
||||
const invalidResponse = {
|
||||
syncEnabled: true,
|
||||
autoAddToShelf: false
|
||||
};
|
||||
|
||||
httpClientMock.get.mockReturnValue(of(invalidResponse));
|
||||
|
||||
service.getUser().subscribe(settings => {
|
||||
expect(settings).not.toHaveProperty('token');
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if API returns KoboSyncSettings without required syncEnabled field', () => {
|
||||
const invalidResponse = {
|
||||
token: 'abc',
|
||||
autoAddToShelf: false
|
||||
};
|
||||
|
||||
httpClientMock.get.mockReturnValue(of(invalidResponse));
|
||||
|
||||
service.getUser().subscribe(settings => {
|
||||
expect(settings).not.toHaveProperty('syncEnabled');
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if API returns KoboSyncSettings without required autoAddToShelf field', () => {
|
||||
const invalidResponse = {
|
||||
token: 'abc',
|
||||
syncEnabled: true
|
||||
};
|
||||
|
||||
httpClientMock.get.mockReturnValue(of(invalidResponse));
|
||||
|
||||
service.getUser().subscribe(settings => {
|
||||
expect(settings).not.toHaveProperty('autoAddToShelf');
|
||||
});
|
||||
});
|
||||
|
||||
it('should call correct endpoint for getUser', () => {
|
||||
httpClientMock.get.mockReturnValue(of({}));
|
||||
service.getUser().subscribe();
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v1\/kobo-settings$/));
|
||||
});
|
||||
|
||||
it('should call correct endpoint for createOrUpdateToken', () => {
|
||||
httpClientMock.put.mockReturnValue(of({}));
|
||||
service.createOrUpdateToken().subscribe();
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v1\/kobo-settings\/token$/), null);
|
||||
});
|
||||
|
||||
it('should call correct endpoint for updateSettings', () => {
|
||||
httpClientMock.put.mockReturnValue(of({}));
|
||||
const settings: KoboSyncSettings = {
|
||||
token: 'abc',
|
||||
syncEnabled: true,
|
||||
autoAddToShelf: false
|
||||
};
|
||||
service.updateSettings(settings).subscribe();
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v1\/kobo-settings$/), settings);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
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 {KoreaderService, KoreaderUser} from './koreader.service';
|
||||
|
||||
describe('KoreaderService', () => {
|
||||
let service: KoreaderService;
|
||||
let httpClientMock: any;
|
||||
|
||||
const mockUser: KoreaderUser = {
|
||||
username: 'testuser',
|
||||
password: 'secret',
|
||||
syncEnabled: true
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn(),
|
||||
put: vi.fn(),
|
||||
patch: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
KoreaderService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(injector, () => TestBed.inject(KoreaderService));
|
||||
});
|
||||
|
||||
it('should create user', () => {
|
||||
httpClientMock.put.mockReturnValue(of(mockUser));
|
||||
service.createUser('testuser', 'secret').subscribe(user => {
|
||||
expect(user).toEqual(mockUser);
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(
|
||||
expect.stringContaining('/me'),
|
||||
{username: 'testuser', password: 'secret'}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get user', () => {
|
||||
httpClientMock.get.mockReturnValue(of(mockUser));
|
||||
service.getUser().subscribe(user => {
|
||||
expect(user).toEqual(mockUser);
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringContaining('/me'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should toggle sync', () => {
|
||||
httpClientMock.patch.mockReturnValue(of(void 0));
|
||||
service.toggleSync(true).subscribe(result => {
|
||||
expect(result).toBeUndefined();
|
||||
expect(httpClientMock.patch).toHaveBeenCalledWith(
|
||||
expect.stringContaining('/me/sync'),
|
||||
null,
|
||||
{params: {enabled: 'true'}}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle createUser error', () => {
|
||||
httpClientMock.put.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.createUser('testuser', 'secret').subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle getUser error', () => {
|
||||
httpClientMock.get.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.getUser().subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle toggleSync error', () => {
|
||||
httpClientMock.patch.mockReturnValue(throwError(() => new Error('fail')));
|
||||
service.toggleSync(false).subscribe({
|
||||
error: (err: any) => {
|
||||
expect(err).toBeInstanceOf(Error);
|
||||
expect(err.message).toBe('fail');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('KoreaderService - API Contract Tests', () => {
|
||||
let service: KoreaderService;
|
||||
let httpClientMock: any;
|
||||
|
||||
beforeEach(() => {
|
||||
httpClientMock = {
|
||||
get: vi.fn(),
|
||||
put: vi.fn(),
|
||||
patch: vi.fn()
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
KoreaderService,
|
||||
{provide: HttpClient, useValue: httpClientMock}
|
||||
]
|
||||
});
|
||||
|
||||
const injector = TestBed.inject(EnvironmentInjector);
|
||||
service = runInInjectionContext(injector, () => TestBed.inject(KoreaderService));
|
||||
});
|
||||
|
||||
it('should validate all required KoreaderUser fields exist', () => {
|
||||
const requiredFields: (keyof KoreaderUser)[] = ['username', 'password', 'syncEnabled'];
|
||||
const mockResponse: KoreaderUser = {
|
||||
username: 'test',
|
||||
password: 'pw',
|
||||
syncEnabled: true
|
||||
};
|
||||
httpClientMock.get.mockReturnValue(of(mockResponse));
|
||||
service.getUser().subscribe(user => {
|
||||
requiredFields.forEach(field => {
|
||||
expect(user).toHaveProperty(field);
|
||||
expect(user[field]).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if API returns KoreaderUser without required username field', () => {
|
||||
const invalidResponse = {
|
||||
password: 'pw',
|
||||
syncEnabled: true
|
||||
};
|
||||
httpClientMock.get.mockReturnValue(of(invalidResponse));
|
||||
service.getUser().subscribe(user => {
|
||||
expect(user).not.toHaveProperty('username');
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if API returns KoreaderUser without required password field', () => {
|
||||
const invalidResponse = {
|
||||
username: 'test',
|
||||
syncEnabled: true
|
||||
};
|
||||
httpClientMock.get.mockReturnValue(of(invalidResponse));
|
||||
service.getUser().subscribe(user => {
|
||||
expect(user).not.toHaveProperty('password');
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if API returns KoreaderUser without required syncEnabled field', () => {
|
||||
const invalidResponse = {
|
||||
username: 'test',
|
||||
password: 'pw'
|
||||
};
|
||||
httpClientMock.get.mockReturnValue(of(invalidResponse));
|
||||
service.getUser().subscribe(user => {
|
||||
expect(user).not.toHaveProperty('syncEnabled');
|
||||
});
|
||||
});
|
||||
|
||||
it('should call correct endpoint for createUser', () => {
|
||||
httpClientMock.put.mockReturnValue(of({}));
|
||||
service.createUser('u', 'p').subscribe();
|
||||
expect(httpClientMock.put).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/\/api\/v1\/koreader-users\/me$/),
|
||||
{username: 'u', password: 'p'}
|
||||
);
|
||||
});
|
||||
|
||||
it('should call correct endpoint for getUser', () => {
|
||||
httpClientMock.get.mockReturnValue(of({}));
|
||||
service.getUser().subscribe();
|
||||
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v1\/koreader-users\/me$/));
|
||||
});
|
||||
|
||||
it('should call correct endpoint for toggleSync', () => {
|
||||
httpClientMock.patch.mockReturnValue(of({}));
|
||||
service.toggleSync(true).subscribe();
|
||||
expect(httpClientMock.patch).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/\/api\/v1\/koreader-users\/me\/sync$/),
|
||||
null,
|
||||
{params: {enabled: 'true'}}
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user