diff --git a/x-pack/legacy/plugins/ml/public/application/util/url_state.test.ts b/x-pack/legacy/plugins/ml/public/application/util/url_state.test.ts index b39ece61beeb1..91bbef2dba6c2 100644 --- a/x-pack/legacy/plugins/ml/public/application/util/url_state.test.ts +++ b/x-pack/legacy/plugins/ml/public/application/util/url_state.test.ts @@ -4,7 +4,20 @@ * you may not use this file except in compliance with the Elastic License. */ -import { getUrlState } from './url_state'; +import { renderHook, act } from '@testing-library/react-hooks'; +import { getUrlState, useUrlState } from './url_state'; + +const mockHistoryPush = jest.fn(); + +jest.mock('react-router-dom', () => ({ + useHistory: () => ({ + push: mockHistoryPush, + }), + useLocation: () => ({ + search: + "?_a=(mlExplorerFilter:(),mlExplorerSwimlane:(viewByFieldName:action),query:(query_string:(analyze_wildcard:!t,query:'*')))&_g=(ml:(jobIds:!(dec-2)),refreshInterval:(display:Off,pause:!f,value:0),time:(from:'2019-01-01T00:03:40.000Z',mode:absolute,to:'2019-08-30T11:55:07.000Z'))&savedSearchId=571aaf70-4c88-11e8-b3d7-01146121b73d", + }), +})); describe('getUrlState', () => { test('properly decode url with _g and _a', () => { @@ -44,3 +57,25 @@ describe('getUrlState', () => { }); }); }); + +describe('useUrlState', () => { + beforeEach(() => { + mockHistoryPush.mockClear(); + }); + + test('pushes a properly encoded search string to history', () => { + const { result } = renderHook(() => useUrlState('_a')); + + act(() => { + const [, setUrlState] = result.current; + setUrlState({ + query: {}, + }); + }); + + expect(mockHistoryPush).toHaveBeenCalledWith({ + search: + '_a=%28mlExplorerFilter%3A%28%29%2CmlExplorerSwimlane%3A%28viewByFieldName%3Aaction%29%2Cquery%3A%28%29%29&_g=%28ml%3A%28jobIds%3A%21%28dec-2%29%29%2CrefreshInterval%3A%28display%3AOff%2Cpause%3A%21f%2Cvalue%3A0%29%2Ctime%3A%28from%3A%272019-01-01T00%3A03%3A40.000Z%27%2Cmode%3Aabsolute%2Cto%3A%272019-08-30T11%3A55%3A07.000Z%27%29%29&savedSearchId=%27571aaf70-4c88-11e8-b3d7-01146121b73d%27', + }); + }); +});