import 'reflect-metadata'; import { FrontCoverInterface, ProductViewInterface, SinglePageViewInterface, } from './index'; import { LayoutBoxInterface, } from '../index'; import ProductViewModel from './product-view.model'; describe('isFrontCover', () => { test('It returns false if argument is null', () => { const isFrontCover = ProductViewModel.isFrontCover(null); expect(isFrontCover).toBe(false); }); test('It returns false if argument has title as undefined', () => { const isFrontCover = ProductViewModel.isFrontCover({ title: undefined, } as FrontCoverInterface); expect(isFrontCover).toBe(false); }); test('It returns true if argument has title with a value', () => { const isFrontCover = ProductViewModel.isFrontCover({ title: {}, } as FrontCoverInterface); expect(isFrontCover).toBe(true); }); }); describe('isSinglePageView', () => { test('It returns false if argument is null', () => { const isSinglePage = ProductViewModel.isSinglePageView(null); expect(isSinglePage).toBe(false); }); // tslint:disable-next-line test('It returns false if argument has isSingleLeftPage as undefined', () => { const isSinglePage = ProductViewModel.isSinglePageView({ isSingleLeftPage: undefined, } as SinglePageViewInterface); expect(isSinglePage).toBe(false); }); // tslint:disable-next-line test('It returns true if argument has isSingleLeftPage with a value', () => { const isSinglePage = ProductViewModel.isSinglePageView({ isSingleLeftPage: true, } as SinglePageViewInterface); expect(isSinglePage).toBe(true); }); }); describe('isFirstPageView', () => { // tslint:disable-next-line test('It returns true if the productView has isSingleLeftPage === false', () => { const isFirstPage = ProductViewModel.isFirstPageView({ isSingleLeftPage: false, } as SinglePageViewInterface); expect(isFirstPage).toBe(true); }); // tslint:disable-next-line test('It returns false if the productView has isSingleLeftPage === true', () => { const isFirstPage = ProductViewModel.isFirstPageView({ isSingleLeftPage: true, } as SinglePageViewInterface); expect(isFirstPage).toBe(false); }); // tslint:disable-next-line test('It returns false if the productView has isSingleLeftPage not set', () => { const isFirstPage = ProductViewModel.isFirstPageView({ isSingleLeftPage: undefined, } as SinglePageViewInterface); expect(isFirstPage).toBe(false); }); }); describe('isLastPageView', () => { // tslint:disable-next-line test('It returns false if the productView has isSingleLeftPage === false', () => { const isLastPage = ProductViewModel.isLastPageView({ isSingleLeftPage: false, } as SinglePageViewInterface); expect(isLastPage).toBe(false); }); // tslint:disable-next-line test('It returns true if the productView has isSingleLeftPage === true', () => { const isLastPage = ProductViewModel.isLastPageView({ isSingleLeftPage: true, } as SinglePageViewInterface); expect(isLastPage).toBe(true); }); // tslint:disable-next-line test('It returns false if the productView has isSingleLeftPage not set', () => { const isLastPage = ProductViewModel.isLastPageView({ isSingleLeftPage: undefined, } as SinglePageViewInterface); expect(isLastPage).toBe(false); }); }); describe('getTotalNumberOfPages', () => { // tslint:disable-next-line test('It returns the total number of normal pages multiplied by 2', () => { const productViews = [ {}, {}, {}, ] as ProductViewInterface[]; ProductViewModel.isFrontCover = jest.fn() .mockReturnValue(false); ProductViewModel.isSinglePageView = jest.fn() .mockReturnValue(false); expect( ProductViewModel.getTotalNumberOfPages( productViews, ), ).toBe(6); }); // tslint:disable-next-line test('If there are only single pages it returns the length of the single pages', () => { const productViews = [ {}, {}, {}, ] as ProductViewInterface[]; ProductViewModel.isFrontCover = jest.fn() .mockReturnValue(false); ProductViewModel.isSinglePageView = jest.fn() .mockReturnValue(true); expect( ProductViewModel.getTotalNumberOfPages( productViews, ), ).toBe(3); }); }); describe('findWithId', () => { // tslint:disable-next-line test('Returns the product view with the matching id', () => { const productViews = [{ id: 1, }, { id: 2, }, { id: 3, }] as ProductViewInterface[]; const result = ProductViewModel.findWithId( productViews, 2, ); expect(result).toBe(productViews[1]); }); // tslint:disable-next-line test('Returns undefined if there is no product with a matching id', () => { const productViews = [{ id: 1, }, { id: 2, }, { id: 3, }] as ProductViewInterface[]; const result = ProductViewModel.findWithId( productViews, 10, ); expect(result).toBeUndefined(); }); }); describe('findByContainedLayoutBox', () => { test('Returns the productView that contains the layoutBox', () => { const layoutBoxToFind = { id: 'my-id' } as LayoutBoxInterface; const matchProductView = { layoutBoxes: [ layoutBoxToFind ] }; const productViews = [ { layoutBoxes: [{ id: 'no-match' }] }, { layoutBoxes: [{ id: 'still-no' }] }, matchProductView, ] as ProductViewInterface[]; expect(ProductViewModel.findByContainedLayoutBox( productViews, layoutBoxToFind, )).toBe(matchProductView); }); test('Returns undefined if no productView contains the layoutBox', () => { const layoutBoxToFind = { id: 'my-id' } as LayoutBoxInterface; const productViews = [ { layoutBoxes: [{ id: 'no-match' }] }, { layoutBoxes: [{ id: 'still-no' }] }, ] as ProductViewInterface[]; expect(ProductViewModel.findByContainedLayoutBox( productViews, layoutBoxToFind, )).toBe(undefined); }); });