import 'reflect-metadata'; import { GroupedLayoutsInterface, LayoutInterface, } from './index'; import { ObjectUtils, } from './../../helpers/functions/index'; import GroupedLayoutsModel from './grouped-layouts.model'; describe('chooseARandomLayout', () => { // tslint:disable-next-line test('It looks for layout properties until one with length more than 0 is found', () => { const groupedLayouts = { frontCover: [], interior: [{}], } as GroupedLayoutsInterface; ObjectUtils.pickRandomProperty = jest.fn() .mockReturnValueOnce('frontCover') .mockReturnValue('interior'); ObjectUtils.randomValueFromArray = jest.fn() .mockReturnValue('random'); GroupedLayoutsModel.chooseARandomLayout( groupedLayouts, undefined, ); expect( ObjectUtils.randomValueFromArray, ).toHaveBeenCalledWith(groupedLayouts.interior); }); // tslint:disable-next-line test('It returns the value from randomValueFromArray', () => { const groupedLayouts = { frontCover: [], interior: [{}], } as GroupedLayoutsInterface; ObjectUtils.pickRandomProperty = jest.fn() .mockReturnValue('interior'); ObjectUtils.randomValueFromArray = jest.fn() .mockReturnValue('test'); expect( GroupedLayoutsModel.chooseARandomLayout( groupedLayouts, undefined, ), ).toBe('test'); }); // tslint:disable-next-line test('It gets values from randomValueFromArray until the response does not equal the lastLayoutValue', () => { const groupedLayouts = { frontCover: [], interior: [{}], } as GroupedLayoutsInterface; const lastLayoutValue = {} as LayoutInterface; ObjectUtils.pickRandomProperty = jest.fn() .mockReturnValue('interior'); ObjectUtils.randomValueFromArray = jest.fn() .mockReturnValueOnce(lastLayoutValue) .mockReturnValue('nottest'); expect( GroupedLayoutsModel.chooseARandomLayout( groupedLayouts, lastLayoutValue, ), ).toBe('nottest'); }); });