Skip to content

Commit 4d8df92

Browse files
committed
Update products service tests
1 parent 7efbaff commit 4d8df92

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

apps/api/src/models/products/products.service.spec.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Types } from 'mongoose';
33
import { CreateProductDto } from 'shared-types';
44
import { ProductsRepository } from './products.repository';
55
import { ProductsService } from './products.service';
6+
import { ImagesService } from '../../images/images.service';
67

78
describe('ProductsService', () => {
89
let service: ProductsService;
@@ -31,6 +32,12 @@ describe('ProductsService', () => {
3132
countDocuments: jest.fn(() => 100),
3233
};
3334

35+
const mockImagesService = {
36+
handleImageDtoAndGetKey: jest.fn(() => 'img-key'),
37+
};
38+
39+
const handleImageDtoAndGetKeySpy = jest.spyOn(mockImagesService, 'handleImageDtoAndGetKey');
40+
3441
beforeEach(async () => {
3542
const module: TestingModule = await Test.createTestingModule({
3643
providers: [
@@ -39,6 +46,10 @@ describe('ProductsService', () => {
3946
provide: ProductsRepository,
4047
useValue: mockProductsRepository,
4148
},
49+
{
50+
provide: ImagesService,
51+
useValue: mockImagesService,
52+
},
4253
],
4354
}).compile();
4455

@@ -61,16 +72,18 @@ describe('ProductsService', () => {
6172
);
6273
});
6374

64-
it('should update product', () => {
65-
const product = service.update(new Types.ObjectId(), {
75+
it('should update product', async () => {
76+
const product = await service.update(new Types.ObjectId(), {
6677
organizationId: '123',
6778
name: 'updated-product',
79+
image: { hasImage: false },
6880
});
6981
expect(product).toEqual(
7082
expect.objectContaining({
7183
name: 'updated-product',
7284
}),
7385
);
86+
expect(handleImageDtoAndGetKeySpy).toBeCalled();
7487
});
7588

7689
it('should delete product', () => {

apps/api/src/models/products/products.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ProductsService {
2323
const { image, ...rest } = dto;
2424
const product = await this.productsRepository.findOneByIdAndUpdate(id, rest);
2525
product.imageKey = await this.imagesService.handleImageDtoAndGetKey(product, image);
26-
return product.save();
26+
return await this.productsRepository.findOneByIdAndUpdate(id, product);
2727
}
2828

2929
delete(id: mongoose.Types.ObjectId): Promise<ProductDocument> {

0 commit comments

Comments
 (0)