-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ML] Fix decoding in the URL state #54915
Merged
darnautov
merged 6 commits into
elastic:master
from
darnautov:ML-54885-fix-url-state-decode
Jan 16, 2020
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47506c1
[ML] condition for rison decode in getUrlState
darnautov d94e8dd
[ML] fix icon alignment
darnautov 2ac8133
[ML] use Set
darnautov 40d9646
[ML] add export, fix typo
darnautov 177baeb
[ML] setUrlState test
darnautov 22fd373
[ML] fields stats width
darnautov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
x-pack/legacy/plugins/ml/public/application/util/url_state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
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', () => { | ||
expect( | ||
getUrlState( | ||
"?_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" | ||
) | ||
).toEqual({ | ||
_a: { | ||
mlExplorerFilter: {}, | ||
mlExplorerSwimlane: { | ||
viewByFieldName: 'action', | ||
}, | ||
query: { | ||
query_string: { | ||
analyze_wildcard: true, | ||
query: '*', | ||
}, | ||
}, | ||
}, | ||
_g: { | ||
ml: { | ||
jobIds: ['dec-2'], | ||
}, | ||
refreshInterval: { | ||
display: 'Off', | ||
pause: false, | ||
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('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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't know if items which aren't
_a
or_g
are needed at all for the url state.but it might also do no harm collecting them. @walterra would need to confirm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment also setting the
urlState
expects all of url state so I suggest to keep it like that for now. I did it this way becauseuseLocation
is not mutable and I always needed the full most recent value for getting/settingsearch
.@darnautov Thanks for adding the test! Seems you forgot to commit to export
getUrlState
so the test fails on CI at the moment. Can you also add a test to check the other way around when setting the url state? I suspect we also need a check againstdecodedParams
when setting the state here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed I forgot to include export to my initial commit, added in 40d9646.
test for
setUrlState
: 177baeb