import 'reflect-metadata'; import { ProductShippingLocationsAndPricesContentComponent, } from './product-shipping-locations-and-prices-content.component'; import { CatalogShippingInfoResponseInterface } from './../../models/index'; const initProductShippingLocationsAndPricesContentComponent = () => new ProductShippingLocationsAndPricesContentComponent(); describe('defaults', () => { test('Sets a default for trackedText', () => { expect( initProductShippingLocationsAndPricesContentComponent().trackedText, ).toBe('Tracked'); }); }); describe('showContent', () => { const initShowContentData = () => { const mockShippingTemplates = new Map( [ ['foo', false], ['bar', true], ], ); const mockShippingCountries = new Map( [ ['UK', false], ['US', false], ], ); const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingTemplates = mockShippingTemplates; productShippingLocationsAndPricesContent.shippingCountries = mockShippingCountries; return { mockShippingCountries, mockShippingTemplates, productShippingLocationsAndPricesContent, }; }; // tslint:disable-next-line test('Returns true if there are both valid shippingTemplates and shippingCountries', () => { const { productShippingLocationsAndPricesContent, } = initShowContentData(); expect(productShippingLocationsAndPricesContent.showContent) .toBe(true); }); test('Returns false if there are no shippingTemplates supplied', () => { const { productShippingLocationsAndPricesContent, } = initShowContentData(); productShippingLocationsAndPricesContent.shippingTemplates = undefined; expect(productShippingLocationsAndPricesContent.showContent) .toBe(false); }); test('Returns false if shippingTemplates is empty', () => { const { productShippingLocationsAndPricesContent, } = initShowContentData(); productShippingLocationsAndPricesContent.shippingTemplates = new Map(); expect(productShippingLocationsAndPricesContent.showContent) .toBe(false); }); test('Returns false if there are no shippingCountries supplied', () => { const { productShippingLocationsAndPricesContent, } = initShowContentData(); productShippingLocationsAndPricesContent.shippingCountries = undefined; expect(productShippingLocationsAndPricesContent.showContent) .toBe(false); }); test('Returns false if shippingCountries is empty', () => { const { productShippingLocationsAndPricesContent, } = initShowContentData(); productShippingLocationsAndPricesContent.shippingCountries = new Map(); expect(productShippingLocationsAndPricesContent.showContent) .toBe(false); }); }); describe('get shippingTemplatesAsOptions', () => { const initGetShippingTemplatesAsOptionsData = () => { const mockShippingTemplates = new Map( [ ['foo', false], ['bar', true], ], ); const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingTemplates = mockShippingTemplates; return { mockShippingTemplates, productShippingLocationsAndPricesContent, }; }; // tslint:disable-next-line test('Returns the components shippingTemplates input as dropdown options', () => { const { productShippingLocationsAndPricesContent, } = initGetShippingTemplatesAsOptionsData(); const result = productShippingLocationsAndPricesContent .shippingTemplatesAsOptions; expect(result).toEqual([{ label: 'foo', value: 'foo', }, { label: 'bar', value: 'bar', }]); }); }); describe('shippingTemplatesOptionsSelectedValue', () => { // tslint:disable-next-line test('Gets the first value from the shippingTemplates which is true', () => { const mockShippingTemplates = new Map( [ ['foo', false], ['bar', true], ['c', true], ], ); const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingTemplates = mockShippingTemplates; const result = productShippingLocationsAndPricesContent .shippingTemplatesOptionsSelectedValue; expect(result).toBe('bar'); }); }); describe('get shippingCountriesAsArray', () => { const initGetShippingCountriesAsArrayData = () => { const mockShippingCountries = new Map( [ ['UK', false], ['US', false], ], ); const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingCountries = mockShippingCountries; return { mockShippingCountries, productShippingLocationsAndPricesContent, }; }; // tslint:disable-next-line test('Returns the components shippingCountries input as dropdown options', () => { const { productShippingLocationsAndPricesContent, } = initGetShippingCountriesAsArrayData(); const result = productShippingLocationsAndPricesContent.shippingCountriesAsOptions; expect(result).toEqual([{ label: 'UK', value: 'UK', }, { label: 'US', value: 'US', }]); }); }); describe('shippingCountriesOptionsSelectedValue', () => { // tslint:disable-next-line test('Gets the first value from the shippingCountries which is true', () => { const mockShippingCountries = new Map( [ ['foo', false], ['bar', true], ['c', true], ], ); const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingCountries = mockShippingCountries; const result = productShippingLocationsAndPricesContent .shippingCountriesOptionsSelectedValue; expect(result).toBe('bar'); }); }); describe('getCarrierMethod', () => { test('Adds tracked to the carrier method if its a tracked method', () => { const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.trackedText = 'Tracked'; const result = productShippingLocationsAndPricesContent.getCarrierMethod({ carrierMethod: 'International', tracked: true, } as CatalogShippingInfoResponseInterface); expect(result).toBe('International Tracked'); }); // tslint:disable-next-line test('Does not add tracked to the carrier method if is not a tracked method', () => { const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.trackedText = 'Tracked'; const result = productShippingLocationsAndPricesContent.getCarrierMethod({ carrierMethod: 'International', tracked: false, } as CatalogShippingInfoResponseInterface); expect(result).toBe('International'); }); // tslint:disable-next-line test('Does not add tracked to the carrier method if is tracked but already contains the tracked text', () => { const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.trackedText = 'Tracked'; const result = productShippingLocationsAndPricesContent.getCarrierMethod({ carrierMethod: 'International Tracked', tracked: true, } as CatalogShippingInfoResponseInterface); expect(result).toBe('International Tracked'); }); }); describe('getCostForCurrentCurrency', () => { const initGetCostForCurrentCurrencyData = () => { const mockShippingTemplates = new Map( [ ['foo', false], ['bar', true], ], ); const mockShippingCountries = new Map( [ ['UK', false], ['US', false], ], ); const mockShippingInfo = [ { additionalItemCostExcludingVat: [ {currency: 'CHF', value: 1.97}, {currency: 'NZD', value: 2.94}, {currency: 'AUD', value: 2.71}, {currency: 'GBP', value: 1.5}, ], carrierMethod: 'Second Class', carrierName: 'Royal Mail', country: [{countryCode3: 'GBR', name: 'United Kingdom'}], maxDeliveryTimeWorkingDays: 5, region: 'United Kingdom', shippingTime: '2-5 Working Days', tierCostExcludingVat: [ {currency: 'CHF', value: 3.28}, {currency: 'NZD', value: 4.9}, {currency: 'AUD', value: 4.53}, {currency: 'GBP', value: 3}, ], tracked: null, }, { additionalItemCostExcludingVat: [ {currency: 'CHF', value: 1.97}, {currency: 'NZD', value: 2.94}, {currency: 'AUD', value: 2.71}, {currency: 'GBP', value: 1.5}, ], carrierMethod: 'Standard', carrierName: 'P2P / Trakpak', country: [ {countryCode3: 'SVN', name: 'Slovenia'}, {countryCode3: 'ROU', name: 'Romania'}, ], maxDeliveryTimeWorkingDays: 8, region: 'EU1', shippingTime: '3-8 Working Days', tierCostExcludingVat: [ {currency: 'CHF', value: 3.93}, {currency: 'NZD', value: 5.88}, {currency: 'AUD', value: 5.43}, {currency: 'GBP', value: 3.25}, ], tracked: null, }, ]; const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingTemplates = mockShippingTemplates; productShippingLocationsAndPricesContent.shippingCountries = mockShippingCountries; return { mockShippingCountries, mockShippingTemplates, productShippingLocationsAndPricesContent, }; }; // tslint:disable-next-line test('Returns undefined if there are no values in the supplied argument', () => { const { productShippingLocationsAndPricesContent, } = initGetCostForCurrentCurrencyData(); const result = productShippingLocationsAndPricesContent.getCostForCurrentCurrency( [], ); expect(result).toBeUndefined(); }); // tslint:disable-next-line test('Returns correct price if there are values', () => { const { productShippingLocationsAndPricesContent, } = initGetCostForCurrentCurrencyData(); const mockCostsByCurrency = [ { currency: 'GBP', value: 4.99 }, { currency: 'NZD', value: 10.99 }, ]; const result = productShippingLocationsAndPricesContent.getCostForCurrentCurrency( mockCostsByCurrency, ); expect(result).toBe(4.99); }); }); describe('changeSelectedTemplate', () => { const initChangeSelectedTemplateData = () => { const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.onChangeShippingTemplate.emit = jest.fn(); return { productShippingLocationsAndPricesContent, }; }; test('Calls onChangeShippingTemplate.emit with the provided value', () => { const { productShippingLocationsAndPricesContent, } = initChangeSelectedTemplateData(); productShippingLocationsAndPricesContent.changeSelectedTemplate('foo'); expect( productShippingLocationsAndPricesContent .onChangeShippingTemplate .emit, ).toHaveBeenCalledWith('foo'); }); }); describe('changeSelectedCountry', () => { const initChangeSelectedCountryData = () => { const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.onChangeShippingCountry.emit = jest.fn(); return { productShippingLocationsAndPricesContent, }; }; test('Calls onChangeShippingCountry.emit with the provided value', () => { const { productShippingLocationsAndPricesContent, } = initChangeSelectedCountryData(); productShippingLocationsAndPricesContent.changeSelectedCountry('foo'); expect( productShippingLocationsAndPricesContent .onChangeShippingCountry .emit, ).toHaveBeenCalledWith('foo'); }); }); describe('hasManufacturingTime', () => { // tslint:disable-next-line test('Returns true if one bit of shipping info has a value for manufacturingTime', () => { const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingInfo = [ {}, { manufacturingTime: '10 days', }, ] as CatalogShippingInfoResponseInterface[]; const result = productShippingLocationsAndPricesContent.hasManufacturingTime(); expect(result).toBe(true); }); // tslint:disable-next-line test('Returns false if no of shipping info has a value for manufacturingTime', () => { const productShippingLocationsAndPricesContent = initProductShippingLocationsAndPricesContentComponent(); productShippingLocationsAndPricesContent.shippingInfo = [ {}, {}, ] as CatalogShippingInfoResponseInterface[]; const result = productShippingLocationsAndPricesContent.hasManufacturingTime(); expect(result).toBe(false); }); });