Add frontend unit tests (Part 1)

This commit is contained in:
acx10
2026-01-05 17:10:40 -07:00
parent e283b61e2e
commit 34851ef35c
36 changed files with 4592 additions and 50 deletions
@@ -0,0 +1,271 @@
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {TestBed} from '@angular/core/testing';
import {HttpClient} from '@angular/common/http';
import {of, throwError} from 'rxjs';
import {EmailV2ProviderService} from './email-v2-provider.service';
import {EmailProvider} from '../email-provider.model';
describe('EmailV2ProviderService', () => {
let service: EmailV2ProviderService;
let httpClientMock: any;
const mockProvider: EmailProvider = {
isEditing: false,
id: 1,
userId: 2,
name: 'Provider1',
host: 'smtp.example.com',
port: 587,
username: 'user',
password: 'pass',
fromAddress: 'from@example.com',
auth: true,
startTls: true,
defaultProvider: false,
shared: false
};
beforeEach(() => {
httpClientMock = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
patch: vi.fn()
};
TestBed.configureTestingModule({
providers: [
EmailV2ProviderService,
{provide: HttpClient, useValue: httpClientMock}
]
});
service = TestBed.inject(EmailV2ProviderService);
});
it('should get email providers', () => {
httpClientMock.get.mockReturnValue(of([mockProvider]));
service.getEmailProviders().subscribe(providers => {
expect(providers).toEqual([mockProvider]);
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/providers'));
});
});
it('should create email provider', () => {
httpClientMock.post.mockReturnValue(of(mockProvider));
service.createEmailProvider(mockProvider).subscribe(provider => {
expect(provider).toEqual(mockProvider);
expect(httpClientMock.post).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/providers'), mockProvider);
});
});
it('should update provider', () => {
httpClientMock.put.mockReturnValue(of(mockProvider));
service.updateProvider(mockProvider).subscribe(provider => {
expect(provider).toEqual(mockProvider);
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringContaining(`/api/v2/email/providers/${mockProvider.id}`), mockProvider);
});
});
it('should delete provider', () => {
httpClientMock.delete.mockReturnValue(of(void 0));
service.deleteProvider(1).subscribe(result => {
expect(result).toBeUndefined();
expect(httpClientMock.delete).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/providers/1'));
});
});
it('should set default provider', () => {
httpClientMock.patch.mockReturnValue(of(void 0));
service.setDefaultProvider(1).subscribe(result => {
expect(result).toBeUndefined();
expect(httpClientMock.patch).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/providers/1/set-default'), {});
});
});
it('should handle getEmailProviders error', () => {
httpClientMock.get.mockReturnValue(throwError(() => new Error('fail')));
service.getEmailProviders().subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle createEmailProvider error', () => {
httpClientMock.post.mockReturnValue(throwError(() => new Error('fail')));
service.createEmailProvider(mockProvider).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle updateProvider error', () => {
httpClientMock.put.mockReturnValue(throwError(() => new Error('fail')));
service.updateProvider(mockProvider).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle deleteProvider error', () => {
httpClientMock.delete.mockReturnValue(throwError(() => new Error('fail')));
service.deleteProvider(1).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle setDefaultProvider error', () => {
httpClientMock.patch.mockReturnValue(throwError(() => new Error('fail')));
service.setDefaultProvider(1).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
});
describe('EmailV2ProviderService - API Contract Tests', () => {
let service: EmailV2ProviderService;
let httpClientMock: any;
beforeEach(() => {
httpClientMock = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
patch: vi.fn()
};
TestBed.configureTestingModule({
providers: [
EmailV2ProviderService,
{provide: HttpClient, useValue: httpClientMock}
]
});
service = TestBed.inject(EmailV2ProviderService);
});
it('should validate all required EmailProvider fields exist', () => {
const requiredFields: (keyof EmailProvider)[] = [
'isEditing', 'id', 'userId', 'name', 'host', 'port', 'username', 'password',
'fromAddress', 'auth', 'startTls', 'defaultProvider', 'shared'
];
const mockResponse: EmailProvider = {
isEditing: false,
id: 1,
userId: 2,
name: 'Provider1',
host: 'smtp.example.com',
port: 587,
username: 'user',
password: 'pass',
fromAddress: 'from@example.com',
auth: true,
startTls: true,
defaultProvider: false,
shared: false
};
httpClientMock.get.mockReturnValue(of([mockResponse]));
service.getEmailProviders().subscribe(providers => {
requiredFields.forEach(field => {
expect(providers[0]).toHaveProperty(field);
expect(providers[0][field]).toBeDefined();
});
});
});
it('should fail if API returns EmailProvider without required id field', () => {
const invalidResponse = [{
isEditing: false,
userId: 2,
name: 'Provider1',
host: 'smtp.example.com',
port: 587,
username: 'user',
password: 'pass',
fromAddress: 'from@example.com',
auth: true,
startTls: true,
defaultProvider: false,
shared: false
}];
httpClientMock.get.mockReturnValue(of(invalidResponse));
service.getEmailProviders().subscribe(providers => {
expect(providers[0]).not.toHaveProperty('id');
});
});
it('should call correct endpoint for getEmailProviders', () => {
httpClientMock.get.mockReturnValue(of([]));
service.getEmailProviders().subscribe();
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/providers$/));
});
it('should call correct endpoint for createEmailProvider', () => {
httpClientMock.post.mockReturnValue(of({}));
const provider = {
isEditing: false,
id: 1,
userId: 2,
name: 'Provider1',
host: 'smtp.example.com',
port: 587,
username: 'user',
password: 'pass',
fromAddress: 'from@example.com',
auth: true,
startTls: true,
defaultProvider: false,
shared: false
};
service.createEmailProvider(provider).subscribe();
expect(httpClientMock.post).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/providers$/), provider);
});
it('should call correct endpoint for updateProvider', () => {
httpClientMock.put.mockReturnValue(of({}));
const provider = {
isEditing: false,
id: 1,
userId: 2,
name: 'Provider1',
host: 'smtp.example.com',
port: 587,
username: 'user',
password: 'pass',
fromAddress: 'from@example.com',
auth: true,
startTls: true,
defaultProvider: false,
shared: false
};
service.updateProvider(provider).subscribe();
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/providers\/1$/), provider);
});
it('should call correct endpoint for deleteProvider', () => {
httpClientMock.delete.mockReturnValue(of(void 0));
service.deleteProvider(1).subscribe();
expect(httpClientMock.delete).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/providers\/1$/));
});
it('should call correct endpoint for setDefaultProvider', () => {
httpClientMock.patch.mockReturnValue(of(void 0));
service.setDefaultProvider(1).subscribe();
expect(httpClientMock.patch).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/providers\/1\/set-default$/), {});
});
});
@@ -0,0 +1,230 @@
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {TestBed} from '@angular/core/testing';
import {HttpClient} from '@angular/common/http';
import {of, throwError} from 'rxjs';
import {EmailV2RecipientService} from './email-v2-recipient.service';
import {EmailRecipient} from '../email-recipient.model';
describe('EmailV2RecipientService', () => {
let service: EmailV2RecipientService;
let httpClientMock: any;
const mockRecipient: EmailRecipient = {
id: 1,
email: 'to@example.com',
name: 'Recipient1',
defaultRecipient: false,
isEditing: false
};
beforeEach(() => {
httpClientMock = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
patch: vi.fn()
};
TestBed.configureTestingModule({
providers: [
EmailV2RecipientService,
{provide: HttpClient, useValue: httpClientMock}
]
});
service = TestBed.inject(EmailV2RecipientService);
});
it('should get recipients', () => {
httpClientMock.get.mockReturnValue(of([mockRecipient]));
service.getRecipients().subscribe(recipients => {
expect(recipients).toEqual([mockRecipient]);
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/recipients'));
});
});
it('should create recipient', () => {
httpClientMock.post.mockReturnValue(of(mockRecipient));
service.createRecipient(mockRecipient).subscribe(recipient => {
expect(recipient).toEqual(mockRecipient);
expect(httpClientMock.post).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/recipients'), mockRecipient);
});
});
it('should update recipient', () => {
httpClientMock.put.mockReturnValue(of(mockRecipient));
service.updateRecipient(mockRecipient).subscribe(recipient => {
expect(recipient).toEqual(mockRecipient);
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringContaining(`/api/v2/email/recipients/${mockRecipient.id}`), mockRecipient);
});
});
it('should delete recipient', () => {
httpClientMock.delete.mockReturnValue(of(void 0));
service.deleteRecipient(1).subscribe(result => {
expect(result).toBeUndefined();
expect(httpClientMock.delete).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/recipients/1'));
});
});
it('should set default recipient', () => {
httpClientMock.patch.mockReturnValue(of(void 0));
service.setDefaultRecipient(1).subscribe(result => {
expect(result).toBeUndefined();
expect(httpClientMock.patch).toHaveBeenCalledWith(expect.stringContaining('/api/v2/email/recipients/1/set-default'), {});
});
});
it('should handle getRecipients error', () => {
httpClientMock.get.mockReturnValue(throwError(() => new Error('fail')));
service.getRecipients().subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle createRecipient error', () => {
httpClientMock.post.mockReturnValue(throwError(() => new Error('fail')));
service.createRecipient(mockRecipient).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle updateRecipient error', () => {
httpClientMock.put.mockReturnValue(throwError(() => new Error('fail')));
service.updateRecipient(mockRecipient).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle deleteRecipient error', () => {
httpClientMock.delete.mockReturnValue(throwError(() => new Error('fail')));
service.deleteRecipient(1).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle setDefaultRecipient error', () => {
httpClientMock.patch.mockReturnValue(throwError(() => new Error('fail')));
service.setDefaultRecipient(1).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
});
describe('EmailV2RecipientService - API Contract Tests', () => {
let service: EmailV2RecipientService;
let httpClientMock: any;
beforeEach(() => {
httpClientMock = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
patch: vi.fn()
};
TestBed.configureTestingModule({
providers: [
EmailV2RecipientService,
{provide: HttpClient, useValue: httpClientMock}
]
});
service = TestBed.inject(EmailV2RecipientService);
});
it('should validate all required EmailRecipient fields exist', () => {
const requiredFields: (keyof EmailRecipient)[] = [
'id', 'email', 'name', 'defaultRecipient', 'isEditing'
];
const mockResponse: EmailRecipient = {
id: 1,
email: 'to@example.com',
name: 'Recipient1',
defaultRecipient: false,
isEditing: false
};
httpClientMock.get.mockReturnValue(of([mockResponse]));
service.getRecipients().subscribe(recipients => {
requiredFields.forEach(field => {
expect(recipients[0]).toHaveProperty(field);
expect(recipients[0][field]).toBeDefined();
});
});
});
it('should fail if API returns EmailRecipient without required id field', () => {
const invalidResponse = [{
email: 'to@example.com',
name: 'Recipient1',
defaultRecipient: false,
isEditing: false
}];
httpClientMock.get.mockReturnValue(of(invalidResponse));
service.getRecipients().subscribe(recipients => {
expect(recipients[0]).not.toHaveProperty('id');
});
});
it('should call correct endpoint for getRecipients', () => {
httpClientMock.get.mockReturnValue(of([]));
service.getRecipients().subscribe();
expect(httpClientMock.get).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/recipients$/));
});
it('should call correct endpoint for createRecipient', () => {
httpClientMock.post.mockReturnValue(of({}));
const recipient = {
id: 1,
email: 'to@example.com',
name: 'Recipient1',
defaultRecipient: false,
isEditing: false
};
service.createRecipient(recipient).subscribe();
expect(httpClientMock.post).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/recipients$/), recipient);
});
it('should call correct endpoint for updateRecipient', () => {
httpClientMock.put.mockReturnValue(of({}));
const recipient = {
id: 1,
email: 'to@example.com',
name: 'Recipient1',
defaultRecipient: false,
isEditing: false
};
service.updateRecipient(recipient).subscribe();
expect(httpClientMock.put).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/recipients\/1$/), recipient);
});
it('should call correct endpoint for deleteRecipient', () => {
httpClientMock.delete.mockReturnValue(of(void 0));
service.deleteRecipient(1).subscribe();
expect(httpClientMock.delete).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/recipients\/1$/));
});
it('should call correct endpoint for setDefaultRecipient', () => {
httpClientMock.patch.mockReturnValue(of(void 0));
service.setDefaultRecipient(1).subscribe();
expect(httpClientMock.patch).toHaveBeenCalledWith(expect.stringMatching(/\/api\/v2\/email\/recipients\/1\/set-default$/), {});
});
});
@@ -0,0 +1,115 @@
import {beforeEach, describe, expect, it, vi} from 'vitest';
import {TestBed} from '@angular/core/testing';
import {HttpClient} from '@angular/common/http';
import {of, throwError} from 'rxjs';
import {EmailService} from './email.service';
describe('EmailService', () => {
let service: EmailService;
let httpClientMock: any;
beforeEach(() => {
httpClientMock = {
post: vi.fn()
};
TestBed.configureTestingModule({
providers: [
EmailService,
{provide: HttpClient, useValue: httpClientMock}
]
});
service = TestBed.inject(EmailService);
});
it('should email book', () => {
httpClientMock.post.mockReturnValue(of(void 0));
const req = {bookId: 1, providerId: 2, recipientId: 3};
service.emailBook(req).subscribe(result => {
expect(result).toBeUndefined();
expect(httpClientMock.post).toHaveBeenCalledWith(
expect.stringContaining('/api/v2/email/book'),
req
);
});
});
it('should email book quick', () => {
httpClientMock.post.mockReturnValue(of(void 0));
service.emailBookQuick(42).subscribe(result => {
expect(result).toBeUndefined();
expect(httpClientMock.post).toHaveBeenCalledWith(
expect.stringContaining('/api/v2/email/book/42'),
{}
);
});
});
it('should handle error for emailBook', () => {
httpClientMock.post.mockReturnValue(throwError(() => new Error('fail')));
service.emailBook({bookId: 1, providerId: 2, recipientId: 3}).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
it('should handle error for emailBookQuick', () => {
httpClientMock.post.mockReturnValue(throwError(() => new Error('fail')));
service.emailBookQuick(42).subscribe({
error: (err: any) => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('fail');
}
});
});
});
describe('EmailService - API Contract Tests', () => {
let service: EmailService;
let httpClientMock: any;
beforeEach(() => {
httpClientMock = {
post: vi.fn()
};
TestBed.configureTestingModule({
providers: [
EmailService,
{provide: HttpClient, useValue: httpClientMock}
]
});
service = TestBed.inject(EmailService);
});
it('should call correct endpoint for emailBook', () => {
httpClientMock.post.mockReturnValue(of(void 0));
const req = {bookId: 1, providerId: 2, recipientId: 3};
service.emailBook(req).subscribe();
expect(httpClientMock.post).toHaveBeenCalledWith(
expect.stringMatching(/\/api\/v2\/email\/book$/),
req
);
});
it('should call correct endpoint for emailBookQuick', () => {
httpClientMock.post.mockReturnValue(of(void 0));
service.emailBookQuick(99).subscribe();
expect(httpClientMock.post).toHaveBeenCalledWith(
expect.stringMatching(/\/api\/v2\/email\/book\/99$/),
{}
);
});
it('should require all fields for emailBook request', () => {
const req = {bookId: 1, providerId: 2, recipientId: 3};
expect(req).toHaveProperty('bookId');
expect(req).toHaveProperty('providerId');
expect(req).toHaveProperty('recipientId');
});
});