Advertisement
nikolayneykov

Untitled

Sep 5th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // tslint:disable-next-line: no-import-side-effect
  2. import 'reflect-metadata';
  3. import { ICompany, IFuritureDatabase, IFurniture, ITask } from '../../src/contracts';
  4. import { Constants } from './../../src/common/constants';
  5. import { MaterialType } from './../../src/models/common/material-type';
  6. import { AddFurnitureToCompany } from './../../src/tasks/add-furniture-to-company';
  7.  
  8. describe('AddFurnitureToCompany', () => {
  9.   let companyMock: ICompany;
  10.   let furnitureMock: IFurniture;
  11.   let databaseMock: IFuritureDatabase;
  12.   let addFurnitureToCompanyTask: ITask;
  13.   let companyName: string;
  14.   let furnitureModel: string;
  15.  
  16.   beforeEach(() => {
  17.     companyMock = {
  18.       name: 'Test Company',
  19.       registrationNumber: '1234567890',
  20.       furnitures: [],
  21.       add(): void {
  22.         // Empty
  23.       },
  24.       remove(): void {
  25.         // Empty
  26.       },
  27.       find(): IFurniture {
  28.         return undefined;
  29.       },
  30.       catalog(): string {
  31.         return 'test';
  32.       }
  33.     };
  34.  
  35.     furnitureMock = {
  36.       model: 'Test Model',
  37.       materialType: MaterialType.Leather,
  38.       price: 10,
  39.       height: 10,
  40.       print(): string {
  41.         return 'test';
  42.       }
  43.     };
  44.  
  45.     databaseMock = {
  46.       companies: [companyMock],
  47.       furnitures: [furnitureMock]
  48.     };
  49.  
  50.     addFurnitureToCompanyTask = new AddFurnitureToCompany(databaseMock);
  51.     companyName = 'Test Company';
  52.     furnitureModel = 'Test Model';
  53.   });
  54.  
  55.   describe('run should', () => {
  56.     it('throw with the correct error message if the company passed doesn\'t exist in the database', () => {
  57.       const spy: jest.SpyInstance<string, [string]> = jest
  58.         .spyOn(Constants, 'getCompanyNotFoundErrorMessage')
  59.         .mockReturnValue('Company not found error');
  60.  
  61.       companyName = '';
  62.  
  63.       expect(() => addFurnitureToCompanyTask.run(companyName, furnitureModel))
  64.         .toThrowError('Company not found error');
  65.  
  66.       expect(spy).toHaveBeenCalledTimes(1);
  67.       expect(spy).toHaveBeenCalledWith(companyName);
  68.       expect(spy).toHaveReturnedWith('Company not found error');
  69.  
  70.       spy.mockRestore();
  71.     });
  72.  
  73.     it('throw with the correct error message if the company passed doesn\'t exist in the database', () => {
  74.       const spy: jest.SpyInstance<string, [string]> = jest
  75.         .spyOn(Constants, 'getFurnitureNotFoundErrorMessage')
  76.         .mockReturnValue('Furniture not found error');
  77.  
  78.       furnitureModel = '';
  79.  
  80.       expect(() => addFurnitureToCompanyTask.run(companyName, furnitureModel))
  81.         .toThrowError('Furniture not found error');
  82.  
  83.       expect(spy).toHaveBeenCalledTimes(1);
  84.       expect(spy).toHaveBeenCalledWith(furnitureModel);
  85.       expect(spy).toHaveReturnedWith('Furniture not found error');
  86.  
  87.       spy.mockRestore();
  88.     });
  89.  
  90.     it('not throw if the company and the furniture exist in the database', () => {
  91.       expect(() => addFurnitureToCompanyTask.run(companyName, furnitureModel))
  92.         .not
  93.         .toThrowError();
  94.     });
  95.  
  96.     it('add the furniture to the company\'s collection of furnitures', () => {
  97.       addFurnitureToCompanyTask.run(companyName, furnitureModel);
  98.  
  99.       expect(companyMock.furnitures.includes(furnitureMock)).toBe(true);
  100.       expect(companyMock.furnitures).toEqual([furnitureMock]);
  101.     });
  102.  
  103.     it('return the correct success message', () => {
  104.       const spy: jest.SpyInstance<string, [string, string]> = jest
  105.         .spyOn(Constants, 'getFurnitureAddedSuccessMessage')
  106.         .mockReturnValue('Added furniture to company!');
  107.  
  108.       addFurnitureToCompanyTask.run(companyName, furnitureModel);
  109.  
  110.       expect(spy).toHaveBeenCalledTimes(1);
  111.       expect(spy).toBeCalledWith(furnitureModel, companyName);
  112.       expect(spy).toHaveReturnedWith('Added furniture to company!');
  113.     });
  114.   });
  115. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement