import 'reflect-metadata'; import { AutoNewLineService, } from './../auto-new-line/auto-new-line.service'; import { MaxTextLengthService, } from './max-text-length.service'; const initMaxTextLengthService = ( autoNewLineService?: AutoNewLineService, ) => { return new MaxTextLengthService( autoNewLineService, ); }; describe('getMaximumTextLength', () => { const initGetMaximumTextLengthData = () => { const autoNewLineService = {} as AutoNewLineService; autoNewLineService.getTextWidth = jest.fn() .mockReturnValueOnce(10) .mockReturnValueOnce(20); const maxTextLengthService = initMaxTextLengthService( autoNewLineService, ); return { autoNewLineService, maxTextLengthService, }; }; // tslint:disable-next-line test('Calls autoNewLineService.getTextWidth', () => { const { autoNewLineService, maxTextLengthService, } = initGetMaximumTextLengthData(); maxTextLengthService.getMaximumTextLength( '12px', 'Arial', 29, ); expect(autoNewLineService.getTextWidth).toHaveBeenCalledWith( 'W', '12px', 'Arial', ); }); // tslint:disable-next-line test('Returns the character length that will fit within the autoNewLineService.getTextWidth for each character of text', () => { const { maxTextLengthService, } = initGetMaximumTextLengthData(); const result = maxTextLengthService.getMaximumTextLength( '12px', 'Arial', 29, ); expect(result).toBe(2); }); test('Uses a different character if specified in the startup', () => { const { autoNewLineService, maxTextLengthService, } = initGetMaximumTextLengthData(); maxTextLengthService.getMaximumTextLength( '12px', 'Arial', 19, 'F', ); expect(autoNewLineService.getTextWidth).toHaveBeenCalledWith( 'F', '12px', 'Arial', ); }); });