import 'reflect-metadata'; import { requiredMocks, } from './../../../../test-mocks'; requiredMocks(jest); import { QueryStringService, } from './query-string.service'; import * as mock from './../../../mocks/index'; const initMockDocument = mock.document(jest); const initQueryStringService = ( window?: Window, document = initMockDocument(), ) => { return new QueryStringService( window, document, ); }; describe('getQueryStringKey', () => { // tslint:disable-next-line test('It returns null if there is no matching query string', () => { const window = { location: { search: '?someotherstring=test', }, } as Window; const queryStringService = initQueryStringService( window, ); const result = queryStringService.getQueryStringKey('notthere'); expect(result).toBeNull(); }); // tslint:disable-next-line test('It picks out the query string when there is a single variable', () => { const window = { location: { search: '?querystring=test', }, } as Window; const queryStringService = initQueryStringService( window, ); const result = queryStringService.getQueryStringKey('querystring'); expect(result).toBe('test'); }); // tslint:disable-next-line test('It picks out the query string and decodes it', () => { const window = { location: { search: '?querystring=%7Ba', }, } as Window; const queryStringService = initQueryStringService( window, ); const result = queryStringService.getQueryStringKey('querystring'); expect(result).toBe('{a'); }); // tslint:disable-next-line test('It picks out the query string when there is more than 1 variable', () => { const window = { location: { search: '?querystring=test&onewewant=hello&other=notthis', }, } as Window; const queryStringService = initQueryStringService( window, ); const result = queryStringService.getQueryStringKey('onewewant'); expect(result).toBe('hello'); }); }); describe('clearQueryStrings', () => { // tslint:disable-next-line test('If the url contains a query string it calls the window.history.replaceState with the url with the query string removed', () => { const window = { history: { replaceState: jest.fn() as any, }, location: { hash: '', origin: 'beforequerystring', pathname: '/pathname/', search: '?afterquery=test', }, } as Window; const document = { title: 'testtitle', } as Document; const queryStringService = initQueryStringService( window, document, ); queryStringService.clearQueryStrings(); expect( window.history.replaceState, ).toHaveBeenCalledWith( {}, 'testtitle', 'beforequerystring/pathname/', ); }); // tslint:disable-next-line test('If the there is no search in the window it does not call window.history.replaceState', () => { const window = { history: {}, location: { search: '', }, } as Window; window.history.replaceState = jest.fn(); const queryStringService = initQueryStringService( window, ); queryStringService.clearQueryStrings(); expect( window.history.replaceState, ).not.toHaveBeenCalled(); }); // tslint:disable-next-line test('It does not clear the part after the hash from the URL', () => { const window = { history: { replaceState: jest.fn() as any, }, location: { hash: '#/angularroute', origin: 'beforequerystring', pathname: '/pathname/', search: '?afterquery=test', }, } as Window; const document = { title: 'testtitle', } as Document; const queryStringService = initQueryStringService( window, document, ); queryStringService.clearQueryStrings(); expect( window.history.replaceState, ).toHaveBeenCalledWith( {}, 'testtitle', 'beforequerystring/pathname/#/angularroute', ); }); }); describe('convertPropertyNameToQueryStringKey', () => { // tslint:disable-next-line test('Converts a camelcase property to lowercase with hypens between the words', () => { const queryStringService = initQueryStringService(); const result = queryStringService.convertPropertyNameToQueryStringKey( 'somePropertyName', ); expect(result).toBe('some-property-name'); }); });