import { HttpClient } from '@angular/common/http'; import 'reflect-metadata'; import { requiredMocks, } from './../../../../test-mocks'; requiredMocks(jest); import { Uploader, UploadItem, } from 'angular2-http-file-upload'; import * as angular2HttpFileUpload from 'angular2-http-file-upload'; import { empty, of, } from 'rxjs'; import { PIG_FILL_MODES, PIGLayerImageInterface, PIGLayerInterface, PIGMaskInterface, PIGPrintGuideAreaInterface, } from './../../../models/index'; import { PIGService } from './pig.service'; const initPIGService = ( http?: HttpClient, ) => { return new PIGService(http); }; const productionPIGUrl = 'https://image.kite.ly'; const pigletUrl = 'https://piglet.kite.ly'; describe('upload', () => { const setUploadUrlTest = ({ expectedBaseUrl, isProduction, }: { expectedBaseUrl: string, isProduction: boolean, }) => { const upload = jest.fn(); const uploader = { upload, url: undefined, }; angular2HttpFileUpload.Uploader = jest.fn().mockImplementation(() => { return uploader; }); const pigService = initPIGService(); pigService.upload$({} as File, isProduction) .subscribe((resp) => { expect(uploader.url).toBe(expectedBaseUrl + '/upload'); }); }; // tslint:disable-next-line test('Sets uploader url to production url if no value set for isProduction', () => { setUploadUrlTest({ expectedBaseUrl: productionPIGUrl, isProduction: undefined, }); }); test('Sets uploader url to production url if isProduction is true', () => { setUploadUrlTest({ expectedBaseUrl: productionPIGUrl, isProduction: true, }); }); test('Sets uploader url to piglet url if isProduction is false', () => { setUploadUrlTest({ expectedBaseUrl: pigletUrl, isProduction: true, }); }); test('Calls uploader.upload', () => { const upload = jest.fn(); angular2HttpFileUpload.Uploader = jest.fn().mockImplementation(() => { return { upload, }; }); const pigService = initPIGService(); pigService.upload$({} as File).subscribe((resp) => { expect(upload).toHaveBeenCalled(); }); }); test('Outputs a value when progress upload triggers', () => { const uploader = { onProgressUpload: jest.fn(), upload: jest.fn(), }; angular2HttpFileUpload.Uploader = jest.fn().mockImplementation(() => { return uploader; }); const pigService = initPIGService(); pigService.upload$({} as File).subscribe((resp) => { expect(resp).toBe(10); }); uploader.onProgressUpload(undefined, 10); }); test('Outputs a value when success upload triggers', () => { const uploader = { onSuccessUpload: jest.fn(), upload: jest.fn(), }; angular2HttpFileUpload.Uploader = jest.fn().mockImplementation(() => { return uploader; }); const pigService = initPIGService(); const item = {} as UploadItem; pigService.upload$({} as File).subscribe((resp) => { expect(resp).toBe(item); }); uploader.onSuccessUpload(undefined, item, undefined, undefined); }); test('Outputs an error when uploader errors', () => { const uploader = { onErrorUpload: jest.fn(), upload: jest.fn(), }; angular2HttpFileUpload.Uploader = jest.fn().mockImplementation(() => { return uploader; }); const pigService = initPIGService(); const item = {} as UploadItem; pigService.upload$({} as File).subscribe((resp) => { return undefined; }, (error) => { expect(error).toBe('error'); }); uploader.onErrorUpload(item, 'error', undefined, undefined); }); }); describe('adjustMaskValues', () => { // tslint:disable-next-line test('Adjusts the mask points if there are negative points', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1180, }, { x: 10, y: 1180, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const expectedPoints = [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ]; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.points[0]) .toEqual(expectedPoints); }); // tslint:disable-next-line test('Does not adjust the mask points if there are not negative points', () => { const expectedPoints = [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ]; const imageMask = { bleed_area: { points: [ [...expectedPoints], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.points[0]) .toEqual(expectedPoints); }); // tslint:disable-next-line test('Saves original minimum X coordinate if there are negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1180, }, { x: 10, y: 1180, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMinX) .toBe(10); }); // tslint:disable-next-line test('Does not saves original minimum X coordinate if there are no negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMinX) .toBeUndefined(); }); // tslint:disable-next-line test('Saves original maximum X coordinate if there are any negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1180, }, { x: 10, y: 1180, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMaxX) .toBe(610); }); // tslint:disable-next-line test('Does not Save original maximum X coordinate if there are negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMaxX) .toBeUndefined(); }); // tslint:disable-next-line test('Saves original minimum Y coordinate if there are any negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1180, }, { x: 10, y: 1180, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMinY) .toBe(-20); }); // tslint:disable-next-line test('Does not save original minimum Y coordinate if there are not negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMinY) .toBeUndefined(); }); // tslint:disable-next-line test('Saves original maximum Y coordinate if there are any negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1180, }, { x: 10, y: 1180, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMaxY) .toBe(1180); }); // tslint:disable-next-line test('Does not save original maximum Y coordinates if there are no negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.originalMaxY) .toBeUndefined(); }); // tslint:disable-next-line test('Saves width of the mask bleed area if there are any negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1180, }, { x: 10, y: 1180, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.widthAdjustedForNegativeBleed) .toBe(600); }); // tslint:disable-next-line test('Sets the width to the variant width if there are not negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.widthAdjustedForNegativeBleed).toBe(600); }); // tslint:disable-next-line test('Saves height of the mask bleed area if there are any negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1180, }, { x: 10, y: 1180, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.heightAdjustedForNegativeBleed) .toBe(1200); }); // tslint:disable-next-line test('Sets the height to the variant height if there are not negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1200, }, { x: 0, y: 1200, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.heightAdjustedForNegativeBleed) .toBe(1200); }); // tslint:disable-next-line test('Saves the scale of the mask bleed area if there are any negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 10, y: -20, }, { x: 610, y: -20, }, { x: 610, y: 1480, }, { x: 10, y: 1480, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.maskScale) .toBe(0.8); }); // tslint:disable-next-line test('Does not save the scale of the mask bleed area if there are no negative coords', () => { const imageMask = { bleed_area: { points: [ [ { x: 0, y: 0, }, { x: 600, y: 0, }, { x: 600, y: 1500, }, { x: 0, y: 1500, }, ], ], } as PIGPrintGuideAreaInterface, mask: 'mask1', } as PIGMaskInterface; const productImageService = initPIGService(); const adjustedMask = productImageService.adjustMaskValues( imageMask, 600, 1200, ); expect(adjustedMask.bleed_area.maskScale) .toBeUndefined(); }); }); describe('processLayerComponents', () => { const initProcessLayerComponentsData = ({ isProduction = true, }: { isProduction?: boolean, } = {}) => { const layerResponse = { images: [{ background: 'background', color_marker: 'color_marker', color_overlay: true, foreground: { image: 'foreground', }, masks: [{ mask: 'test1', }, { mask: 'test2', }], name: 'varname', }], } as PIGLayerInterface; const pigService = initPIGService(); pigService.adjustMaskValues = jest.fn(); const expectedUrl = isProduction || isProduction === undefined ? productionPIGUrl : pigletUrl; const result = pigService.processLayerComponents( layerResponse, 'varname', isProduction, ); return { expectedUrl, layerResponse, result, }; }; // tslint:disable-next-line test('Sets the color_overlay to the value from the matching image variant', () => { const { result, } = initProcessLayerComponentsData(); expect(result.color_overlay).toBe(true); }); const correctlySetForegroundUrl = ({ isProduction, }: { isProduction: boolean, }) => { const { expectedUrl, result, } = initProcessLayerComponentsData({ isProduction, }); expect(result.foreground.image).toBe(`${expectedUrl}/foreground`); }; test('Correctly sets the foreground url for production PIG', () => { correctlySetForegroundUrl({ isProduction: true, }); }); test('Correctly sets the foreground url for staging PIG', () => { correctlySetForegroundUrl({ isProduction: false, }); }); // tslint:disable-next-line test('Sets foreground url to expected value with production PIG if isProduction is undefined', () => { correctlySetForegroundUrl({ isProduction: undefined, }); }); const correctlySetsBackgroundUrl = ({ isProduction, }: { isProduction: boolean, }) => { const { expectedUrl, result, } = initProcessLayerComponentsData({ isProduction, }); expect(result.background).toBe(`${expectedUrl}/background`); }; test('Correctly sets the background url for production PIG', () => { correctlySetsBackgroundUrl({ isProduction: true, }); }); test('Correctly sets the background url for staging PIG', () => { correctlySetsBackgroundUrl({ isProduction: true, }); }); // tslint:disable-next-line test('Sets background url to expected value with production PIG if isProduction is undefined', () => { correctlySetsBackgroundUrl({ isProduction: undefined, }); }); test('Correctly sets the color overlay for production PIG', () => { correctlySetsColorMarkerUrl({ isProduction: true, }); }); const correctlySetsColorMarkerUrl = ({ isProduction, }: { isProduction: boolean, }) => { const { expectedUrl, result, } = initProcessLayerComponentsData({ isProduction, }); expect(result.color_marker).toBe(`${expectedUrl}/color_marker`); }; test('Correctly sets the color marker url for production PIG', () => { correctlySetsColorMarkerUrl({ isProduction: true, }); }); test('Correctly sets the color marker url for staging PIG', () => { correctlySetsColorMarkerUrl({ isProduction: true, }); }); // tslint:disable-next-line test('Sets color marker url to expected value with production PIG if isProduction is undefined', () => { correctlySetsColorMarkerUrl({ isProduction: undefined, }); }); const correctlySetsMasksUrls = ({ isProduction, }: { isProduction: boolean, }) => { const { expectedUrl, result, } = initProcessLayerComponentsData({ isProduction, }); expect(result.masks[0].mask).toBe(`${expectedUrl}/test1`); expect(result.masks[1].mask).toBe(`${expectedUrl}/test2`); }; test('Correctly sets the masks urls for production PIG', () => { correctlySetsMasksUrls({ isProduction: true, }); }); test('Correctly sets the masks urls for staging PIG', () => { correctlySetsMasksUrls({ isProduction: true, }); }); // tslint:disable-next-line test('Sets masks urls to expected value with production PIG if isProduction is undefined', () => { correctlySetsMasksUrls({ isProduction: undefined, }); }); test('Returns a copy of the imageVariant', () => { const { layerResponse, result, } = initProcessLayerComponentsData({ isProduction: false, }); expect(result).not.toBe(layerResponse.images[0]); }); // tslint:disable-next-line test('Uses the first image variant which has masks if there is no matching one', () => { const layerResponse = { images: [{ name: 'varname', }, { masks: [{ mask: 'test1', }, { mask: 'test2', }], name: 'othervarname', }], } as PIGLayerInterface; const pigService = initPIGService(); pigService.adjustMaskValues = jest.fn(); const result = pigService.processLayerComponents( layerResponse, 'someOtherThing', true, ); expect(result.name).toBe(layerResponse.images[1].name); }); // tslint:disable-next-line test('Does not throw error if the specified product image has no masks defined', () => { const layerResponse = { images: [{ foreground: { image: 'testA', }, masks: [], name: 'varone', }, { foreground: { image: 'testB', }, name: 'vartwo', }, { foreground: { image: 'testC', }, name: 'varthree', }], } as PIGLayerInterface; const pigService = initPIGService(); pigService.adjustMaskValues = jest.fn(); expect(() => pigService.processLayerComponents( layerResponse, 'vartwo', )).not.toThrow(); }); }); describe('getLayerComponents$', () => { const initGetLayerComponents$Data = () => { const mockProcessedLayerComponents = { asset_size: {}, } as PIGLayerImageInterface; const layerResponse = { images: [{ name: 'othername', }], } as PIGLayerInterface; const response = {} as Response; response.json = jest.fn().mockReturnValue(layerResponse); const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(of(response)); const pigService = initPIGService( http, ); pigService.processLayerComponents = jest.fn().mockReturnValue(mockProcessedLayerComponents); return { http, layerResponse, mockProcessedLayerComponents, pigService, response, }; }; test('Returns the value from the cache if it exists', () => { const { pigService, } = initGetLayerComponents$Data(); pigService.cachedLayerComponents['templateid-varname'] = { asset_size: { width: 123, }, } as PIGLayerImageInterface; pigService.getLayerComponents$('templateid', 'varname') .subscribe((resp) => { expect(resp).toBe( pigService.cachedLayerComponents['templateid-varname'], ); }); }); test('Calls http get with the correct parameters', () => { const { http, pigService, } = initGetLayerComponents$Data(); pigService.getLayerComponents$('templateid', 'varname') .subscribe(() => { expect(http.get).toHaveBeenCalledWith( 'https://image.kite.ly/product/templateid/', ); }); }); test('Calls json on the response', () => { const { pigService, response, } = initGetLayerComponents$Data(); pigService.getLayerComponents$('templateid', 'varname', false) .subscribe(() => { expect(response.json).toHaveBeenCalled(); }); }); test('Calls processLayerComponents with the correct params', () => { const { pigService, layerResponse, } = initGetLayerComponents$Data(); pigService.getLayerComponents$('templateid', 'varname', false) .subscribe(() => { expect(pigService.processLayerComponents).toHaveBeenCalledWith( layerResponse, 'varname', false, ); }); }); test('Sets imageVariantName to "cover" by default', () => { const { pigService, layerResponse, } = initGetLayerComponents$Data(); pigService.getLayerComponents$('templateid', undefined, false) .subscribe(() => { expect(pigService.processLayerComponents).toHaveBeenCalledWith( layerResponse, 'cover', false, ); }); }); test('Adds the retrieved layer component to the cache', () => { const { pigService, mockProcessedLayerComponents, } = initGetLayerComponents$Data(); pigService.getLayerComponents$('templateid', 'varname', false) .subscribe(() => { expect(pigService.cachedLayerComponents['templateid-varname']) .toBe(mockProcessedLayerComponents); }); }); test('Returns the result of processLayerComponents', () => { const { pigService, mockProcessedLayerComponents, } = initGetLayerComponents$Data(); pigService.getLayerComponents$('templateid', 'varname', false) .subscribe((resp) => { expect(resp).toBe(mockProcessedLayerComponents); }); }); }); describe('getUrlWithFilters', () => { test('Return the url_preview if the filters are not defined', () => { const pigService = initPIGService(); const result = pigService.getUrlWithFilters( 'urlPreview', 'variantName', ); expect(result).toBe('urlPreview'); }); // tslint:disable-next-line test('Return the url_preview if the filters do not include the inverted flag', () => { const pigService = initPIGService(); const result = pigService.getUrlWithFilters( 'urlPreview', 'variantName', '', ); expect(result).toBe('urlPreview'); }); // tslint:disable-next-line test('Return the the pig url with the expected defaults if filters contains i', () => { const pigService = initPIGService(); const result = pigService.getUrlWithFilters( 'urlPreview', 'variantName', 'i,a', ); expect(result).toBe( 'https://image.kite.ly/render/4editor/variantName/' + 'cover/?image=urlPreview&filters=i', ); }); // tslint:disable-next-line test('Return the the pig url with the variantType if filters contains i', () => { const pigService = initPIGService(); const result = pigService.getUrlWithFilters( 'urlPreview', 'variantName', 'i,a', 'variantType', ); expect(result).toBe( 'https://image.kite.ly/render/4editor/variantName/' + 'variantType/?image=urlPreview&filters=i', ); }); // tslint:disable-next-line test('Return the the piglet url if isProduction is false and if filters contains i', () => { const pigService = initPIGService(); const result = pigService.getUrlWithFilters( 'urlPreview', 'variantName', 'i,a', undefined, false, ); expect(result).toBe( 'https://piglet.kite.ly/render/4editor/variantName/' + 'cover/?image=urlPreview&filters=i', ); }); test('Returns null if urlPreview is null', () => { const pigService = initPIGService(); const result = pigService.getUrlWithFilters( null, 'variantName', 'i,a', undefined, false, ); expect(result).toBeNull(); }); }); describe('getRenderedImage', () => { const setsDefaultsAsExpected = ({ isProduction, }: { isProduction: boolean, }) => { const pigService = initPIGService(); pigService.adjustMaskValues = jest.fn(); const expectedUrl = isProduction === undefined || isProduction ? productionPIGUrl : pigletUrl; const renderedImageUrl = pigService.getRenderedImage({ imageUrl: 'image', templateId: 'template', variantName: 'variantName', }, isProduction); expect(renderedImageUrl).toBe( `${expectedUrl}/render/?image=image` + `&product_id=template` + `&variant=variantName` + `&format=jpg` + `&debug=false` + `&background=FFFFFF` + `&size=452x628` + `&fill_mode=${PIG_FILL_MODES.FIT}` + `&padding=20` + `&scale=1` + `&rotate=0` + `&mirror=false` + `&translate=0,0`, ); }; test('Sets defaults as expected when isProduction', () => { setsDefaultsAsExpected({ isProduction: true, }); }); test('Sets defaults as expected when is not isProduction', () => { setsDefaultsAsExpected({ isProduction: false, }); }); test('Sets defaults as expected when isProduction is undefined', () => { setsDefaultsAsExpected({ isProduction: undefined, }); }); test('Lets you change the values', () => { const pigService = initPIGService(); pigService.adjustMaskValues = jest.fn(); const renderedImageUrl = pigService.getRenderedImage({ backgroundColor: 'backgroundColor', debug: true, fillMode: PIG_FILL_MODES.FILL, filters: 'filters', format: 'format', imageUrl: 'imageUrl', mirror: true, padding: 40, renderPrintImage: true, rotateDegrees: 10, scale: 2, size: { height: 100, width: 100, }, templateId: 'templateId', translateX: 11, translateY: 12, variantName: 'variantName', }); expect(renderedImageUrl).toBe( `${productionPIGUrl}/render/?image=imageUrl` + `&product_id=templateId` + `&variant=variantName` + `&format=format` + `&debug=true` + `&background=backgroundColor` + `&size=100x100` + `&fill_mode=${PIG_FILL_MODES.FILL}` + `&padding=40` + `&scale=2` + `&rotate=-10` + `&mirror=true` + `&translate=11,12` + `&filters=filters` + `&print_image=true`, ); }); test('Rounds the rotation degrees', () => { const pigService = initPIGService(); const renderedImageUrl = pigService.getRenderedImage({ backgroundColor: 'backgroundColor', debug: true, fillMode: PIG_FILL_MODES.FILL, filters: 'filters', format: 'format', imageUrl: 'imageUrl', mirror: true, padding: 40, renderPrintImage: true, rotateDegrees: 10.4, scale: 2, size: { height: 100, width: 100, }, templateId: 'templateId', translateX: 11, translateY: 12, variantName: 'variantName', }); expect(renderedImageUrl).toBe( `${productionPIGUrl}/render/?image=imageUrl` + `&product_id=templateId` + `&variant=variantName` + `&format=format` + `&debug=true` + `&background=backgroundColor` + `&size=100x100` + `&fill_mode=${PIG_FILL_MODES.FILL}` + `&padding=40` + `&scale=2` + `&rotate=-10` + `&mirror=true` + `&translate=11,12` + `&filters=filters` + `&print_image=true`, ); }); test('Does not include size if null', () => { const pigService = initPIGService(); const renderedImageUrl = pigService.getRenderedImage({ backgroundColor: 'backgroundColor', debug: true, fillMode: PIG_FILL_MODES.FILL, filters: 'filters', format: 'format', imageUrl: 'imageUrl', mirror: true, padding: 40, renderPrintImage: true, rotateDegrees: 10.4, scale: 2, size: null, templateId: 'templateId', translateX: 11, translateY: 12, variantName: 'variantName', }); expect(renderedImageUrl).toBe( `${productionPIGUrl}/render/?image=imageUrl` + `&product_id=templateId` + `&variant=variantName` + `&format=format` + `&debug=true` + `&background=backgroundColor` + `&fill_mode=${PIG_FILL_MODES.FILL}` + `&padding=40` + `&scale=2` + `&rotate=-10` + `&mirror=true` + `&translate=11,12` + `&filters=filters` + `&print_image=true`, ); }); }); describe('getUploadedImageMeta$', () => { const prodEndpoint = 'https://image.kite.ly'; const devEndpoint = 'https://piglet.kite.ly'; // tslint:disable-next-line test('Calls http with the correct parameters for production if there is no cached data', () => { const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(empty()); const pigService = initPIGService( http, ); pigService.uploadedImagesCache = {}; pigService.getUploadedImageMeta$( 'test.jpg', true, ).subscribe( // tslint:disable-next-line () => {}, ); expect(http.get) .toHaveBeenCalledWith(`${prodEndpoint}/image_meta/?image=test.jpg`); }); // tslint:disable-next-line test('Calls http with the correct parameters for dev if there is no cached data', () => { const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(empty()); const pigService = initPIGService( http, ); pigService.uploadedImagesCache = {}; pigService.getUploadedImageMeta$( 'test.jpg', false, ).subscribe( (resp) => resp, ); expect(http.get) .toHaveBeenCalledWith(`${devEndpoint}/image_meta/?image=test.jpg`); }); // tslint:disable-next-line test('Does not call http if there is cached data for the image', () => { const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(empty()); const pigService = initPIGService( http, ); pigService.uploadedImagesCache = { 'test.jpg': { height: 100, mode: 'RGB', width: 100, }, }; pigService.getUploadedImageMeta$( 'test.jpg', false, ).subscribe( (resp) => expect(resp) .toBe(pigService.uploadedImagesCache['test.jpg']), ); expect(http.get) .not.toHaveBeenCalled(); }); test('Calls http as if production if not specified', () => { const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(empty()); const pigService = initPIGService( http, ); pigService.getUploadedImageMeta$( 'test.jpg', undefined, ).subscribe( (resp) => resp, ); expect(http.get) .toHaveBeenCalledWith(`${prodEndpoint}/image_meta/?image=test.jpg`); }); test('Returns meta data object if successful', () => { const mockMeta = { height: 100, width: 100, }; const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(of({ json: jest.fn().mockReturnValue(mockMeta), })); const pigService = initPIGService( http, ); pigService.getUploadedImageMeta$( 'test.jpg', ).subscribe( (resp) => { expect(resp).toBe(mockMeta); }, ); }); test('Throws error message if response contains one', () => { const mockMeta = { error: 'foo', height: 100, width: 100, }; const http = {} as HttpClient; http.get = jest.fn().mockReturnValue(of({ json: jest.fn().mockReturnValue(mockMeta), })); const pigService = initPIGService( http, ); expect.assertions(1); pigService.getUploadedImageMeta$( 'test.jpg', ).subscribe( (resp) => { expect(resp).toBe(mockMeta); }, (err) => { expect(err) .toBe( `Could not get image meta information for 'test.jpg'`, ); }, ); }); });