-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[Enterprise Search] Add parseQueryParams helper #83750
Merged
scottybollinger
merged 5 commits into
elastic:master
from
scottybollinger:scottybollinger/query-params
Nov 19, 2020
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3a8a22e
[Enterprise Search] Add parseQueryParams helper
scottybollinger e5f0d95
Remove mock
scottybollinger 94fc89e
Actually test functionality of query-string
scottybollinger 065bc5c
Add test for array
scottybollinger 53dca23
Better test name
scottybollinger 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
7 changes: 7 additions & 0 deletions
7
x-pack/plugins/enterprise_search/public/applications/shared/query_params/index.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,7 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { parseQueryParams } from './query_params'; |
19 changes: 19 additions & 0 deletions
19
...ck/plugins/enterprise_search/public/applications/shared/query_params/query_params.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,19 @@ | ||
/* | ||
* 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 queryString from 'query-string'; | ||
|
||
import { parseQueryParams } from './'; | ||
|
||
describe('parseQueryParams', () => { | ||
it('should call queryString parse method', () => { | ||
const parseMock = jest.fn(); | ||
jest.spyOn(queryString, 'parse').mockImplementation(parseMock); | ||
parseQueryParams('?foo=bar'); | ||
|
||
expect(queryString.parse).toHaveBeenCalledWith('?foo=bar', { arrayFormat: 'bracket' }); | ||
}); | ||
cee-chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
10 changes: 10 additions & 0 deletions
10
x-pack/plugins/enterprise_search/public/applications/shared/query_params/query_params.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,10 @@ | ||
/* | ||
* 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 queryString from 'query-string'; | ||
|
||
export const parseQueryParams = (search: string) => | ||
queryString.parse(search, { arrayFormat: 'bracket' }); |
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.
you shouldn't need both
jest.spyOn
andjest.mock
+import up above.I'm a little confused also, why mock queryString? Does it not return an expected
{ foo: 'bar' }
obj when callingparseQueryParams('?foo=bar')
?Unless there's a strange issue with calling the lib directly, I'd prefer to leave it unmocked here if possible
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.
When I don't mock it I get this error:
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.
Right, but I'm saying don't bother spying on it or checking
toHaveBeenCalledWith
, just evaluate the outcome since it's fairly straightforward.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.
Got it! Thanks for the help. TIL actually testing the output of a library in a test 😂
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.
Haha, it doesn't always work out with the more complicated libraries but in the case it's definitely worth doing. Also you totally beat my other comment, jinx :D