Commit b618f3fc authored by Miguel Rincon's avatar Miguel Rincon

Refactor to remove parseQueryStringIntoObject

This refactor removes parseQueryStringIntoObject in favor of using
queryToObject from our URL utils.
parent 6bf50afb
import $ from 'jquery';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { parseQueryStringIntoObject } from '~/lib/utils/common_utils';
import { queryToObject } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
export default class GpgBadges {
......@@ -27,7 +28,7 @@ export default class GpgBadges {
return Promise.reject(new Error(__('Missing commit signatures endpoint!')));
}
const params = parseQueryStringIntoObject(tag.serialize());
const params = queryToObject(tag.serialize());
return axios
.get(endpoint, { params })
.then(({ data }) => {
......
......@@ -418,28 +418,6 @@ export const parseIntPagination = (paginationInformation) => ({
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
* into query-param string
......
......@@ -17,19 +17,23 @@ describe('GpgBadges', () => {
};
const dummyUrl = `${TEST_HOST}/dummy/signatures`;
beforeEach(() => {
mock = new MockAdapter(axios);
const setForm = ({ utf8 = '', search = '' } = {}) => {
setFixtures(`
<form
class="commits-search-form js-signature-container" data-signatures-path="${dummyUrl}" action="${dummyUrl}"
method="get">
<input name="utf8" type="hidden" value="">
<input type="search" name="search" id="commits-search"class="form-control search-text-input input-short">
<input name="utf8" type="hidden" value="${utf8}">
<input type="search" name="search" value="${search}" id="commits-search"class="form-control search-text-input input-short">
</form>
<div class="parent-container">
<div class="js-loading-gpg-badge" data-commit-sha="${dummyCommitSha}"></div>
</div>
`);
};
beforeEach(() => {
mock = new MockAdapter(axios);
setForm();
});
afterEach(() => {
......@@ -62,6 +66,44 @@ describe('GpgBadges', () => {
.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) => {
mock.onGet(dummyUrl).replyOnce(200);
......
......@@ -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', () => {
it('returns empty string when `param` is undefined, null or empty string', () => {
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