import 'reflect-metadata'; import { QueryStringService, } from './../query-string/query-string.service'; const mockConvertedPrimitive = 5; jest.mock('./../../functions/index', () => ({ convertStringToPrimitive: jest.fn() .mockReturnValue(mockConvertedPrimitive), })); import { convertStringToPrimitive, } from './../../functions/index'; import { ReplacePropsFromQueryStringService, } from './replace-props-from-query-string.service'; const initReplacePropsFromQueryStringService = ( queryStringService?: QueryStringService, ) => { return new ReplacePropsFromQueryStringService( queryStringService, ); }; describe('replacePropsWithMatchingQueryStringProps', () => { const initReplacePropsWithMatchingQueryStringPropsData = () => { const mockConvertedQueryStringKey = 'test-query-string-key'; const mockQueryStringKeyValue = 'value'; const queryStringService = {} as QueryStringService; queryStringService.convertPropertyNameToQueryStringKey = jest.fn().mockReturnValue(mockConvertedQueryStringKey); queryStringService.getQueryStringKey = jest.fn() .mockReturnValue(mockQueryStringKeyValue); const replacePropsFromQueryStringService = initReplacePropsFromQueryStringService( queryStringService, ); (convertStringToPrimitive as any).mockClear(); return { mockConvertedQueryStringKey, mockQueryStringKeyValue, queryStringService, replacePropsFromQueryStringService, }; }; test('Returns null if the object is null', () => { const { replacePropsFromQueryStringService, } = initReplacePropsWithMatchingQueryStringPropsData(); const result = replacePropsFromQueryStringService .replacePropsWithMatchingQueryStringProps(null); expect(result).toBeNull(); }); // tslint:disable-next-line test('Calls queryStringService.convertPropertyNameToQueryStringKey with each of the object keys', () => { const { queryStringService, replacePropsFromQueryStringService, } = initReplacePropsWithMatchingQueryStringPropsData(); replacePropsFromQueryStringService .replacePropsWithMatchingQueryStringProps({ someProp1: 'prop1Val', someProp2: 'prop2Val', }); expect(queryStringService.convertPropertyNameToQueryStringKey) .toHaveBeenCalledWith('someProp1'); expect(queryStringService.convertPropertyNameToQueryStringKey) .toHaveBeenCalledWith('someProp2'); }); // tslint:disable-next-line test('Calls queryStringService.getQueryStringKey with the result of queryStringService.convertPropertyNameToQueryStringKey', () => { const { mockConvertedQueryStringKey, queryStringService, replacePropsFromQueryStringService, } = initReplacePropsWithMatchingQueryStringPropsData(); replacePropsFromQueryStringService .replacePropsWithMatchingQueryStringProps({ someProp1: 'prop1Val', someProp2: 'prop2Val', }); expect(queryStringService.getQueryStringKey) .toHaveBeenCalledWith(mockConvertedQueryStringKey); expect(queryStringService.getQueryStringKey).toHaveBeenCalledTimes(2); }); // tslint:disable-next-line test('Calls convertStringToPrimitive with the queryStringValue and type of the original value', () => { const { mockQueryStringKeyValue, replacePropsFromQueryStringService, } = initReplacePropsWithMatchingQueryStringPropsData(); replacePropsFromQueryStringService .replacePropsWithMatchingQueryStringProps({ someProp1: 'prop1Val', someProp2: true, }); expect(convertStringToPrimitive).toHaveBeenCalledWith( mockQueryStringKeyValue, 'string', ); expect(convertStringToPrimitive).toHaveBeenCalledWith( mockQueryStringKeyValue, 'boolean', ); }); // tslint:disable-next-line test('Replaces any matching properties with those from the query string', () => { const { replacePropsFromQueryStringService, } = initReplacePropsWithMatchingQueryStringPropsData(); const result = replacePropsFromQueryStringService .replacePropsWithMatchingQueryStringProps({ someProp1: 'prop1Val', someProp2: true, }); expect(result.someProp1).toBe(mockConvertedPrimitive); expect(result.someProp2).toBe(mockConvertedPrimitive); }); // tslint:disable-next-line test('Replaces inner objects with matching values from the query string', () => { const { replacePropsFromQueryStringService, } = initReplacePropsWithMatchingQueryStringPropsData(); (convertStringToPrimitive as any).mockImplementation((value, type) => { return type === 'object' ? value : 'someVal'; }); const result = replacePropsFromQueryStringService .replacePropsWithMatchingQueryStringProps({ innerObj: { someProp1: 'prop1', someProp2: 'prop2', }, }); expect(result).toEqual({ innerObj: { someProp1: 'someVal', someProp2: 'someVal', }, }); }); });