Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tslint:disable-next-line: no-import-side-effect
- import 'reflect-metadata';
- import { ICompany, IFuritureDatabase, IFurniture, ITask } from '../../src/contracts';
- import { Constants } from './../../src/common/constants';
- import { MaterialType } from './../../src/models/common/material-type';
- import { AddFurnitureToCompany } from './../../src/tasks/add-furniture-to-company';
- describe('AddFurnitureToCompany', () => {
- let companyMock: ICompany;
- let furnitureMock: IFurniture;
- let databaseMock: IFuritureDatabase;
- let addFurnitureToCompanyTask: ITask;
- let companyName: string;
- let furnitureModel: string;
- beforeEach(() => {
- companyMock = {
- name: 'Test Company',
- registrationNumber: '1234567890',
- furnitures: [],
- add(): void {
- // Empty
- },
- remove(): void {
- // Empty
- },
- find(): IFurniture {
- return undefined;
- },
- catalog(): string {
- return 'test';
- }
- };
- furnitureMock = {
- model: 'Test Model',
- materialType: MaterialType.Leather,
- price: 10,
- height: 10,
- print(): string {
- return 'test';
- }
- };
- databaseMock = {
- companies: [companyMock],
- furnitures: [furnitureMock]
- };
- addFurnitureToCompanyTask = new AddFurnitureToCompany(databaseMock);
- companyName = 'Test Company';
- furnitureModel = 'Test Model';
- });
- describe('run should', () => {
- it('throw with the correct error message if the company passed doesn\'t exist in the database', () => {
- const spy: jest.SpyInstance<string, [string]> = jest
- .spyOn(Constants, 'getCompanyNotFoundErrorMessage')
- .mockReturnValue('Company not found error');
- companyName = '';
- expect(() => addFurnitureToCompanyTask.run(companyName, furnitureModel))
- .toThrowError('Company not found error');
- expect(spy).toHaveBeenCalledTimes(1);
- expect(spy).toHaveBeenCalledWith(companyName);
- expect(spy).toHaveReturnedWith('Company not found error');
- spy.mockRestore();
- });
- it('throw with the correct error message if the company passed doesn\'t exist in the database', () => {
- const spy: jest.SpyInstance<string, [string]> = jest
- .spyOn(Constants, 'getFurnitureNotFoundErrorMessage')
- .mockReturnValue('Furniture not found error');
- furnitureModel = '';
- expect(() => addFurnitureToCompanyTask.run(companyName, furnitureModel))
- .toThrowError('Furniture not found error');
- expect(spy).toHaveBeenCalledTimes(1);
- expect(spy).toHaveBeenCalledWith(furnitureModel);
- expect(spy).toHaveReturnedWith('Furniture not found error');
- spy.mockRestore();
- });
- it('not throw if the company and the furniture exist in the database', () => {
- expect(() => addFurnitureToCompanyTask.run(companyName, furnitureModel))
- .not
- .toThrowError();
- });
- it('add the furniture to the company\'s collection of furnitures', () => {
- addFurnitureToCompanyTask.run(companyName, furnitureModel);
- expect(companyMock.furnitures.includes(furnitureMock)).toBe(true);
- expect(companyMock.furnitures).toEqual([furnitureMock]);
- });
- it('return the correct success message', () => {
- const spy: jest.SpyInstance<string, [string, string]> = jest
- .spyOn(Constants, 'getFurnitureAddedSuccessMessage')
- .mockReturnValue('Added furniture to company!');
- addFurnitureToCompanyTask.run(companyName, furnitureModel);
- expect(spy).toHaveBeenCalledTimes(1);
- expect(spy).toBeCalledWith(furnitureModel, companyName);
- expect(spy).toHaveReturnedWith('Added furniture to company!');
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement