75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
import { TestBed } from '@angular/core/testing';
|
|
import { Location } from '@angular/common';
|
|
import { Router } from '@angular/router';
|
|
|
|
import { ProductSettings } from './product-settings';
|
|
import { ImageStorage } from './image-storage';
|
|
import { ProductDAO } from '../dao/ProductDAO';
|
|
import { Product } from '../models/Product';
|
|
|
|
const PRODUCT_SETTINGS_PATH = ['settings', 'products'];
|
|
describe('ProductSettings', () => {
|
|
let service: ProductSettings;
|
|
|
|
let imageStorage: Partial<ImageStorage>;
|
|
let location: Partial<Location>;
|
|
let productDAO: Partial<ProductDAO>;
|
|
let router: Partial<Router>;
|
|
|
|
let IMG_MOCK: File;
|
|
let PRODUCT_MOCK: Product;
|
|
beforeEach(() => {
|
|
IMG_MOCK = new File([], 'image.jpg', { type: 'image/jpeg' });
|
|
PRODUCT_MOCK = new Product('mock', 'mock', 'mock.jpg', 1);
|
|
imageStorage = {
|
|
deleteImage: vi.fn(),
|
|
saveImage: vi.fn().mockResolvedValue('resolved_mock.jpg'),
|
|
};
|
|
location = {
|
|
replaceState: vi.fn(),
|
|
};
|
|
productDAO = {
|
|
insert: vi.fn(),
|
|
update: vi.fn(),
|
|
};
|
|
router = {
|
|
navigate: vi.fn(),
|
|
};
|
|
|
|
TestBed.configureTestingModule({
|
|
providers: [
|
|
{ provide: ImageStorage, useValue: imageStorage },
|
|
{ provide: Location, useValue: location },
|
|
{ provide: ProductDAO, useValue: productDAO },
|
|
{ provide: Router, useValue: router },
|
|
],
|
|
});
|
|
service = TestBed.inject(ProductSettings);
|
|
});
|
|
|
|
it('should be created', () => {
|
|
expect(service).toBeTruthy();
|
|
});
|
|
|
|
it('should save', async () => {
|
|
imageStorage.saveImage = vi.fn().mockResolvedValue(IMG_MOCK.name);
|
|
await service.save(PRODUCT_MOCK, IMG_MOCK);
|
|
expect(imageStorage.saveImage).toHaveBeenCalledExactlyOnceWith(IMG_MOCK);
|
|
expect(productDAO.insert).toHaveBeenCalledExactlyOnceWith(PRODUCT_MOCK);
|
|
expect(router.navigate).toHaveBeenCalledExactlyOnceWith(PRODUCT_SETTINGS_PATH);
|
|
});
|
|
|
|
it('should update', async () => {
|
|
const MODIFIED_IMG_MOCK = new File([], 'new_image.jpg', { type: 'image/jpeg' });
|
|
const MODIFIED_PRODUCT_MOCK = {...PRODUCT_MOCK, image: ''};
|
|
await service.update(PRODUCT_MOCK, MODIFIED_PRODUCT_MOCK, MODIFIED_IMG_MOCK);
|
|
expect(imageStorage.saveImage).toHaveBeenCalledExactlyOnceWith(MODIFIED_IMG_MOCK);
|
|
expect(imageStorage.deleteImage).toHaveBeenCalledExactlyOnceWith(PRODUCT_MOCK.image);
|
|
expect(MODIFIED_PRODUCT_MOCK.image).toEqual('resolved_mock.jpg');
|
|
expect(productDAO.update).toHaveBeenCalledExactlyOnceWith(MODIFIED_PRODUCT_MOCK, {
|
|
id: MODIFIED_PRODUCT_MOCK.id,
|
|
});
|
|
expect(router.navigate).toHaveBeenCalledExactlyOnceWith(PRODUCT_SETTINGS_PATH);
|
|
});
|
|
});
|