Advertisement
efelinto

Untitled

Apr 4th, 2021
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { ConfigModule } from '@nestjs/config';
  2. import { Test, TestingModule } from '@nestjs/testing';
  3.  
  4. import { nestConfig } from '@/app/config/nest.config';
  5. import { PrismaService } from '@/app/prisma/prisma.service';
  6. import { mockedPrismaService } from '@/app/__mocks__';
  7. import { ProductsService } from '../../services/products.service';
  8. import { mockedCreateProductDto, mockedProduct } from '../../__mocks__';
  9. import { ConflictProductExistsError } from '../../exceptions';
  10.  
  11. describe(ProductsService.name, () => {
  12.   let service: ProductsService;
  13.  
  14.   beforeEach(async () => {
  15.     const module: TestingModule = await Test.createTestingModule({
  16.       imports: [ConfigModule.forFeature(nestConfig)],
  17.       providers: [
  18.         ProductsService,
  19.         {
  20.           provide: PrismaService,
  21.           useValue: mockedPrismaService,
  22.         },
  23.       ],
  24.     }).compile();
  25.  
  26.     service = module.get<ProductsService>(ProductsService);
  27.   });
  28.  
  29.   it('should be defined', () => {
  30.     expect(service).toBeDefined();
  31.   });
  32.  
  33.   describe(ProductsService.prototype.newProduct.name, () => {
  34.     it('should create a product when provide valid product data', async () => {
  35.       jest
  36.         .spyOn(mockedPrismaService.product, 'findFirst')
  37.         .mockImplementationOnce(() => null);
  38.  
  39.       const product = service.newProduct(mockedCreateProductDto);
  40.  
  41.       await expect(product).resolves.toBe(mockedProduct);
  42.     });
  43.  
  44.     it('should return an error when product exists', async () => {
  45.       jest
  46.         .spyOn(mockedPrismaService.product, 'findFirst')
  47.         .mockImplementationOnce(() => mockedProduct);
  48.  
  49.       const product = service.newProduct(mockedCreateProductDto);
  50.  
  51.       await expect(product).rejects.toThrow(ConflictProductExistsError);
  52.     });
  53.   });
  54.  
  55.   describe(ProductsService.prototype.getTotalProducts.name, () => {
  56.     it('should get total quantity of products', async () => {
  57.       jest
  58.         .spyOn(mockedPrismaService.product, 'findFirst')
  59.         .mockImplementationOnce(() => null);
  60.  
  61.       const totalProducts = await service.getTotalProducts();
  62.  
  63.       expect(typeof totalProducts).toBe('number');
  64.     });
  65.   });
  66.  
  67.   describe(ProductsService.prototype.getProductById.name, () => {
  68.     it('should return a product', async () => {
  69.       const product = await service.getProductById(mockedProduct.id);
  70.  
  71.       expect(product).toStrictEqual(mockedProduct);
  72.     });
  73.  
  74.     it('should return an error if provide a invalid product id', async () => {
  75.       jest
  76.         .spyOn(mockedPrismaService.product, 'findFirst')
  77.         .mockImplementation(() => null);
  78.  
  79.       const product = service.getProductById('invalid id');
  80.  
  81.       await expect(product).rejects.toThrowError();
  82.     });
  83.   });
  84.   describe(ProductsService.prototype.findProduct.name, () => {
  85.     it('should return a product when use search', async () => {
  86.       jest
  87.         .spyOn(mockedPrismaService.product, 'findFirst')
  88.         .mockImplementation(() => mockedProduct);
  89.  
  90.       const product = await service.findProduct(mockedProduct.id);
  91.  
  92.       expect(product).toBe(mockedProduct);
  93.     });
  94.   });
  95.   describe(ProductsService.prototype.findProductsBySearchQuery.name, () => {
  96.     it('should find a product by id when provide a search query', async () => {
  97.       const product = await service.findProductsBySearchQuery(mockedProduct.id);
  98.  
  99.       expect(product).toStrictEqual([mockedProduct]);
  100.     });
  101.   });
  102.   describe(ProductsService.prototype.getProducts.name, () => {
  103.     it('should return the list of all products', async () => {
  104.       const products = await service.getProducts();
  105.  
  106.       expect(products).toStrictEqual([mockedProduct]);
  107.     });
  108.   });
  109. });
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement