import 'reflect-metadata'; import { FontOptionsInterface, LayoutBoxInterface, PictureInterface, ProductViewItemInterface, ProductViewItemModel, } from '../index'; import { TextLogicFunctions, } from './../../helpers/functions/index'; import LayoutBoxModel from './layout-box.model'; describe('needsPicture', () => { test('It should return true if containedItem is undefined', () => { const layoutBox = {} as LayoutBoxInterface; expect(LayoutBoxModel.needsPicture(layoutBox)).toBe(true); }); test('It should return true if containedItem is null', () => { const layoutBox = { containedItem: null } as LayoutBoxInterface; expect(LayoutBoxModel.needsPicture(layoutBox)).toBe(true); }); // tslint:disable-next-line test('It should return false if containedItem is not an instance of ProductViewImageInterface', () => { const layoutBox = { containedItem: {} } as LayoutBoxInterface; expect(LayoutBoxModel.needsPicture(layoutBox)).toBe(false); }); // tslint:disable-next-line test('It should return false if containedItem is an instance of ProductViewImageInterface but isLoading', () => { const layoutBox = { containedItem: { flipped: false, isLoading: true, }, } as any; expect(LayoutBoxModel.needsPicture(layoutBox)).toBe(false); }); // tslint:disable-next-line test('It should return false if containedItem is an instance of ProductViewImageInterface but has a picture ', () => { const layoutBox = { containedItem: { flipped: false, picture: {}, }, } as any; expect(LayoutBoxModel.needsPicture(layoutBox)).toBe(false); }); // tslint:disable-next-line test('It should return true if containedItem is an instance of ProductViewImageInterface but has no picture ', () => { const layoutBox = { containedItem: { flipped: false, picture: null, }, } as any; expect(LayoutBoxModel.needsPicture(layoutBox)).toBe(true); }); }); describe('needsText', () => { // tslint:disable-next-line test('It returns true if there is no containedItem', () => { const layoutBox = {} as LayoutBoxInterface; expect( LayoutBoxModel.needsText( layoutBox, ), ).toBe(true); }); // tslint:disable-next-line test('It returns false if the containedItems text has a value and ProductViewItemModel.isText returns false', () => { const layoutBox = { containedItem: { text: 'a', } as ProductViewItemInterface, } as LayoutBoxInterface; ProductViewItemModel.isText = jest.fn() .mockReturnValue(false); expect( LayoutBoxModel.needsText( layoutBox, ), ).toBe(false); }); // tslint:disable-next-line test('It returns true if the containedItems text has no length and ProductViewItemModel.isText returns true', () => { const layoutBox = { containedItem: { text: '', } as ProductViewItemInterface, } as LayoutBoxInterface; ProductViewItemModel.isText = jest.fn() .mockReturnValue(true); expect( LayoutBoxModel.needsText( layoutBox, ), ).toBe(true); }); // tslint:disable-next-line test('It returns false if the containedItems text has a value and ProductViewItemModel.isText returns true', () => { const layoutBox = { containedItem: { text: 'a', } as ProductViewItemInterface, } as LayoutBoxInterface; ProductViewItemModel.isText = jest.fn() .mockReturnValue(true); expect( LayoutBoxModel.needsText( layoutBox, ), ).toBe(false); }); }); describe('isTextBox', () => { // tslint:disable-next-line test('It returns false if layoutBox is null', () => { expect(LayoutBoxModel.isTextBox( undefined, )).toBe(false); }); // tslint:disable-next-line test('It returns false if layoutBox.contentType is not text', () => { expect(LayoutBoxModel.isTextBox( { contentType: 'other', } as LayoutBoxInterface, )).toBe(false); }); // tslint:disable-next-line test('It returns true if layoutBox.contentType is text', () => { expect(LayoutBoxModel.isTextBox( { contentType: 'text', } as LayoutBoxInterface, )).toBe(true); }); // tslint:disable-next-line test('It returns false if ProductViewItemModel.isText returns false', () => { const layoutBox = {} as LayoutBoxInterface; ProductViewItemModel.isText = jest.fn().mockReturnValue(false); expect(LayoutBoxModel.isTextBox(layoutBox)).toBe(false); }); // tslint:disable-next-line test('It returns false if ProductViewItemModel.isText returns true', () => { const layoutBox = {} as LayoutBoxInterface; ProductViewItemModel.isText = jest.fn().mockReturnValue(true); expect(LayoutBoxModel.isTextBox(layoutBox)).toBe(true); }); }); describe('getCountOfTypes', () => { // tslint:disable-next-line test('It counts the correct number of text boxes', () => { const layoutBoxes = [ {}, {}, {}, ] as LayoutBoxInterface[]; LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(true); expect(LayoutBoxModel.getCountOfTypes(layoutBoxes)) .toEqual({ imageBoxCount: 0, textBoxCount: layoutBoxes.length, }); }); // tslint:disable-next-line test('It counts the correct number of image boxes', () => { const layoutBoxes = [ {}, {}, {}, ] as LayoutBoxInterface[]; LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(false); expect(LayoutBoxModel.getCountOfTypes(layoutBoxes)) .toEqual({ imageBoxCount: layoutBoxes.length, textBoxCount: 0, }); }); }); describe('isTooLowQualityToPrint', () => { // tslint:disable-next-line test('It returns false if LayoutBoxModel.isTextBox returns true', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(true); expect(LayoutBoxModel.isTooLowQualityToPrint( undefined, )).toBe(false); }); // tslint:disable-next-line test('It returns false if LayoutBoxModel.isTextBox returns false and the layoutBox has no containedItem', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(false); expect(LayoutBoxModel.isTooLowQualityToPrint( {} as LayoutBoxInterface, )).toBe(false); }); // tslint:disable-next-line test('It returns false if LayoutBoxModel.isTextBox returns false and the layoutBox has a containedItem and isTooLowQualityToPrint is false', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(false); expect(LayoutBoxModel.isTooLowQualityToPrint( { containedItem: { isTooLowQualityToPrint: false, } as ProductViewItemInterface, } as LayoutBoxInterface, )).toBe(false); }); // tslint:disable-next-line test('It returns true if LayoutBoxModel.isTextBox returns false and the layoutBox has a containedItem and isTooLowQualityToPrint is true', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(false); expect(LayoutBoxModel.isTooLowQualityToPrint( { containedItem: { isTooLowQualityToPrint: true, } as ProductViewItemInterface, } as LayoutBoxInterface, )).toBe(true); }); }); describe('containsPicture', () => { // tslint:disable-next-line test('Returns false if LayoutBoxModel.isTextBox is true', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(true); expect(LayoutBoxModel.containsPicture( undefined, undefined, )).toBe(false); }); // tslint:disable-next-line test('Returns false if LayoutBoxModel.isTextBox is false and layoutBox.containedItem is undefined', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(false); const layoutBox = {} as LayoutBoxInterface; expect(LayoutBoxModel.containsPicture( layoutBox, undefined, )).toBe(false); }); // tslint:disable-next-line test('Returns false if LayoutBoxModel.isTextBox is false and layoutBox.containedItem has a picture but the url does not match the supplied pictures', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(false); const layoutBox = { containedItem: { picture: { url: 'test', }, } as ProductViewItemInterface, } as LayoutBoxInterface; const picture = { url: 'not test', } as PictureInterface; expect(LayoutBoxModel.containsPicture( layoutBox, picture, )).toBe(false); }); // tslint:disable-next-line test('Returns true if LayoutBoxModel.isTextBox is false and layoutBox.containedItem has a picture where the url matches the supplied pictures url', () => { LayoutBoxModel.isTextBox = jest.fn().mockReturnValue(false); const layoutBox = { containedItem: { picture: { url: 'test', }, } as ProductViewItemInterface, } as LayoutBoxInterface; const picture = { url: 'test', } as PictureInterface; expect(LayoutBoxModel.containsPicture( layoutBox, picture, )).toBe(true); }); }); describe('getTextWhichFitsinLayoutBox', () => { const getTextWhichFitsinLayoutBoxTest = ({ expectedText, getTextVerticalHeightResponses, font, inputText, parentHeight, }: { expectedText: string, getTextVerticalHeightResponses?: () => void, font: FontOptionsInterface, inputText: string, parentHeight: number, }) => { const layoutBox = { containedItem: { font, text: inputText, } as ProductViewItemInterface, dimensionsPercentages: { height: 1, }, } as LayoutBoxInterface; if (getTextVerticalHeightResponses) { getTextVerticalHeightResponses(); } expect( LayoutBoxModel.getTextWhichFitsinLayoutBox( layoutBox, parentHeight, ), ).toBe(expectedText); }; // tslint:disable-next-line test('If text is null it returns null', () => { getTextWhichFitsinLayoutBoxTest({ expectedText: null, font: undefined, inputText: null, parentHeight: undefined, }); }); // tslint:disable-next-line test('If font is null it returns the text passed to it', () => { getTextWhichFitsinLayoutBoxTest({ expectedText: 'abc', font: null, inputText: 'abc', parentHeight: undefined, }); }); // tslint:disable-next-line test('If the initial text fits in the layout box it returns the text', () => { getTextWhichFitsinLayoutBoxTest({ expectedText: 'abc', font: {} as FontOptionsInterface, getTextVerticalHeightResponses: () => { TextLogicFunctions.getTextVerticalHeight = jest.fn() .mockReturnValue(99); }, inputText: 'abc', parentHeight: 100, }); }); // tslint:disable-next-line test('If the initial text fits in the text after one line is removed it returns the text', () => { getTextWhichFitsinLayoutBoxTest({ expectedText: 'abc\notherline', font: {} as FontOptionsInterface, getTextVerticalHeightResponses: () => { TextLogicFunctions.getTextVerticalHeight = jest.fn() .mockReturnValueOnce(101) .mockReturnValue(100); }, inputText: 'abc\notherline', parentHeight: 100, }); }); // tslint:disable-next-line test('If the initial text fits in the text after two lines are removed it returns the text with one line removed', () => { getTextWhichFitsinLayoutBoxTest({ expectedText: 'abc\notherline', font: {} as FontOptionsInterface, getTextVerticalHeightResponses: () => { TextLogicFunctions.getTextVerticalHeight = jest.fn() .mockReturnValueOnce(101) .mockReturnValueOnce(101) .mockReturnValue(100); }, inputText: 'abc\notherline\nlastline', parentHeight: 100, }); }); }); describe('initialise', () => { // tslint:disable-next-line test('It should create a copy of the layoutBox', () => { const layoutBox = {} as LayoutBoxInterface; const clonedLayoutBox = LayoutBoxModel.initialise(layoutBox); expect(clonedLayoutBox) .not.toBe(layoutBox); }); // tslint:disable-next-line test('It should add an id field to the layoutBox', () => { const layoutBox = {} as LayoutBoxInterface; const clonedLayoutBox = LayoutBoxModel.initialise(layoutBox); expect(clonedLayoutBox.id) .not.toBeFalsy(); expect(typeof clonedLayoutBox.id) .toBe('string'); }); });