Commit a456fe1f authored by Andrew Fontaine's avatar Andrew Fontaine

Merge branch '328845-replace-parse-query-string-into-object' into 'master'

Refactor to remove parseQueryStringIntoObject

See merge request gitlab-org/gitlab!64305
parents 1dea3fe9 b618f3fc
import $ from 'jquery'; import $ from 'jquery';
import createFlash from '~/flash'; import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { parseQueryStringIntoObject } from '~/lib/utils/common_utils'; import { queryToObject } from '~/lib/utils/url_utility';
import { __ } from '~/locale'; import { __ } from '~/locale';
export default class GpgBadges { export default class GpgBadges {
...@@ -27,7 +28,7 @@ export default class GpgBadges { ...@@ -27,7 +28,7 @@ export default class GpgBadges {
return Promise.reject(new Error(__('Missing commit signatures endpoint!'))); return Promise.reject(new Error(__('Missing commit signatures endpoint!')));
} }
const params = parseQueryStringIntoObject(tag.serialize()); const params = queryToObject(tag.serialize());
return axios return axios
.get(endpoint, { params }) .get(endpoint, { params })
.then(({ data }) => { .then(({ data }) => {
......
...@@ -418,28 +418,6 @@ export const parseIntPagination = (paginationInformation) => ({ ...@@ -418,28 +418,6 @@ export const parseIntPagination = (paginationInformation) => ({
previousPage: parseInt(paginationInformation['X-PREV-PAGE'], 10), previousPage: parseInt(paginationInformation['X-PREV-PAGE'], 10),
}); });
/**
* Given a string of query parameters creates an object.
*
* @example
* `scope=all&page=2` -> { scope: 'all', page: '2'}
* `scope=all` -> { scope: 'all' }
* ``-> {}
* @param {String} query
* @returns {Object}
*/
export const parseQueryStringIntoObject = (query = '') => {
if (query === '') return {};
return query.split('&').reduce((acc, element) => {
const val = element.split('=');
Object.assign(acc, {
[val[0]]: decodeURIComponent(val[1]),
});
return acc;
}, {});
};
/** /**
* Converts object with key-value pairs * Converts object with key-value pairs
* into query-param string * into query-param string
......
...@@ -17,19 +17,23 @@ describe('GpgBadges', () => { ...@@ -17,19 +17,23 @@ describe('GpgBadges', () => {
}; };
const dummyUrl = `${TEST_HOST}/dummy/signatures`; const dummyUrl = `${TEST_HOST}/dummy/signatures`;
beforeEach(() => { const setForm = ({ utf8 = '', search = '' } = {}) => {
mock = new MockAdapter(axios);
setFixtures(` setFixtures(`
<form <form
class="commits-search-form js-signature-container" data-signatures-path="${dummyUrl}" action="${dummyUrl}" class="commits-search-form js-signature-container" data-signatures-path="${dummyUrl}" action="${dummyUrl}"
method="get"> method="get">
<input name="utf8" type="hidden" value=""> <input name="utf8" type="hidden" value="${utf8}">
<input type="search" name="search" id="commits-search"class="form-control search-text-input input-short"> <input type="search" name="search" value="${search}" id="commits-search"class="form-control search-text-input input-short">
</form> </form>
<div class="parent-container"> <div class="parent-container">
<div class="js-loading-gpg-badge" data-commit-sha="${dummyCommitSha}"></div> <div class="js-loading-gpg-badge" data-commit-sha="${dummyCommitSha}"></div>
</div> </div>
`); `);
};
beforeEach(() => {
mock = new MockAdapter(axios);
setForm();
}); });
afterEach(() => { afterEach(() => {
...@@ -62,6 +66,44 @@ describe('GpgBadges', () => { ...@@ -62,6 +66,44 @@ describe('GpgBadges', () => {
.catch(done.fail); .catch(done.fail);
}); });
it('fetches commit signatures', async () => {
mock.onGet(dummyUrl).replyOnce(200);
await GpgBadges.fetch();
expect(mock.history.get).toHaveLength(1);
expect(mock.history.get[0]).toMatchObject({
params: { search: '', utf8: '' },
url: dummyUrl,
});
});
it('fetches commit signatures with search parameters with spaces', async () => {
mock.onGet(dummyUrl).replyOnce(200);
setForm({ search: 'my search' });
await GpgBadges.fetch();
expect(mock.history.get).toHaveLength(1);
expect(mock.history.get[0]).toMatchObject({
params: { search: 'my search', utf8: '' },
url: dummyUrl,
});
});
it('fetches commit signatures with search parameters with plus symbols', async () => {
mock.onGet(dummyUrl).replyOnce(200);
setForm({ search: 'my+search' });
await GpgBadges.fetch();
expect(mock.history.get).toHaveLength(1);
expect(mock.history.get[0]).toMatchObject({
params: { search: 'my+search', utf8: '' },
url: dummyUrl,
});
});
it('displays a loading spinner', (done) => { it('displays a loading spinner', (done) => {
mock.onGet(dummyUrl).replyOnce(200); mock.onGet(dummyUrl).replyOnce(200);
......
...@@ -175,18 +175,6 @@ describe('common_utils', () => { ...@@ -175,18 +175,6 @@ describe('common_utils', () => {
}); });
}); });
describe('parseQueryStringIntoObject', () => {
it('should return object with query parameters', () => {
expect(commonUtils.parseQueryStringIntoObject('scope=all&page=2')).toEqual({
scope: 'all',
page: '2',
});
expect(commonUtils.parseQueryStringIntoObject('scope=all')).toEqual({ scope: 'all' });
expect(commonUtils.parseQueryStringIntoObject()).toEqual({});
});
});
describe('objectToQueryString', () => { describe('objectToQueryString', () => {
it('returns empty string when `param` is undefined, null or empty string', () => { it('returns empty string when `param` is undefined, null or empty string', () => {
expect(commonUtils.objectToQueryString()).toBe(''); expect(commonUtils.objectToQueryString()).toBe('');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment