Skip to content
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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
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);
Copy link
Member

@cee-chen cee-chen Nov 19, 2020

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 and jest.mock+import up above.

I'm a little confused also, why mock queryString? Does it not return an expected { foo: 'bar' } obj when calling parseQueryParams('?foo=bar')?

Unless there's a strange issue with calling the lib directly, I'd prefer to leave it unmocked here if possible

Copy link
Contributor Author

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:

expect(received).toHaveBeenCalledWith(...expected)

Matcher error: received value must be a mock or spy function

Copy link
Member

@cee-chen cee-chen Nov 19, 2020

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.

Copy link
Contributor Author

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 😂

Copy link
Member

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

parseQueryParams('?foo=bar');

expect(queryString.parse).toHaveBeenCalledWith('?foo=bar', { arrayFormat: 'bracket' });
});
cee-chen marked this conversation as resolved.
Show resolved Hide resolved
});
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' });