2017-01-09 18:23:54 -05:00
|
|
|
require('~/lib/utils/common_utils');
|
2016-11-17 07:05:32 -05:00
|
|
|
|
|
|
|
(() => {
|
|
|
|
describe('common_utils', () => {
|
|
|
|
describe('gl.utils.parseUrl', () => {
|
|
|
|
it('returns an anchor tag with url', () => {
|
|
|
|
expect(gl.utils.parseUrl('/some/absolute/url').pathname).toContain('some/absolute/url');
|
|
|
|
});
|
|
|
|
it('url is escaped', () => {
|
|
|
|
// IE11 will return a relative pathname while other browsers will return a full pathname.
|
|
|
|
// parseUrl uses an anchor element for parsing an url. With relative urls, the anchor
|
|
|
|
// element will create an absolute url relative to the current execution context.
|
2017-01-09 17:35:58 -05:00
|
|
|
// The JavaScript test suite is executed at '/' which will lead to an absolute url
|
|
|
|
// starting with '/'.
|
2017-01-13 11:04:41 -05:00
|
|
|
expect(gl.utils.parseUrl('" test="asf"').pathname).toContain('/%22%20test=%22asf%22');
|
2016-11-17 07:05:32 -05:00
|
|
|
});
|
|
|
|
});
|
2016-12-14 15:17:28 -05:00
|
|
|
|
2016-11-17 07:05:32 -05:00
|
|
|
describe('gl.utils.parseUrlPathname', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(gl.utils, 'parseUrl').and.callFake(url => ({
|
|
|
|
pathname: url,
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
it('returns an absolute url when given an absolute url', () => {
|
|
|
|
expect(gl.utils.parseUrlPathname('/some/absolute/url')).toEqual('/some/absolute/url');
|
|
|
|
});
|
|
|
|
it('returns an absolute url when given a relative url', () => {
|
|
|
|
expect(gl.utils.parseUrlPathname('some/relative/url')).toEqual('/some/relative/url');
|
|
|
|
});
|
|
|
|
});
|
2016-12-14 15:17:28 -05:00
|
|
|
|
|
|
|
describe('gl.utils.getUrlParamsArray', () => {
|
|
|
|
it('should return params array', () => {
|
|
|
|
expect(gl.utils.getUrlParamsArray() instanceof Array).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove the question mark from the search params', () => {
|
|
|
|
const paramsArray = gl.utils.getUrlParamsArray();
|
|
|
|
expect(paramsArray[0][0] !== '?').toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-03 11:16:26 -05:00
|
|
|
describe('gl.utils.handleLocationHash', () => {
|
|
|
|
beforeEach(() => {
|
2017-02-07 13:09:26 -05:00
|
|
|
spyOn(window.document, 'getElementById').and.callThrough();
|
2017-02-03 11:16:26 -05:00
|
|
|
});
|
|
|
|
|
2017-02-07 13:09:26 -05:00
|
|
|
function expectGetElementIdToHaveBeenCalledWith(elementId) {
|
|
|
|
expect(window.document.getElementById).toHaveBeenCalledWith(elementId);
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:16:26 -05:00
|
|
|
it('decodes hash parameter', () => {
|
2017-02-07 13:09:26 -05:00
|
|
|
window.history.pushState({}, null, '#random-hash');
|
|
|
|
gl.utils.handleLocationHash();
|
|
|
|
|
|
|
|
expectGetElementIdToHaveBeenCalledWith('random-hash');
|
|
|
|
expectGetElementIdToHaveBeenCalledWith('user-content-random-hash');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('decodes cyrillic hash parameter', () => {
|
|
|
|
window.history.pushState({}, null, '#definição');
|
2017-02-03 11:16:26 -05:00
|
|
|
gl.utils.handleLocationHash();
|
2017-02-07 13:09:26 -05:00
|
|
|
|
|
|
|
expectGetElementIdToHaveBeenCalledWith('definição');
|
|
|
|
expectGetElementIdToHaveBeenCalledWith('user-content-definição');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('decodes encoded cyrillic hash parameter', () => {
|
|
|
|
window.history.pushState({}, null, '#defini%C3%A7%C3%A3o');
|
|
|
|
gl.utils.handleLocationHash();
|
|
|
|
|
|
|
|
expectGetElementIdToHaveBeenCalledWith('definição');
|
|
|
|
expectGetElementIdToHaveBeenCalledWith('user-content-definição');
|
2017-02-03 11:16:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-12-14 15:17:28 -05:00
|
|
|
describe('gl.utils.getParameterByName', () => {
|
2017-01-13 12:51:19 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
window.history.pushState({}, null, '?scope=all&p=2');
|
|
|
|
});
|
|
|
|
|
2016-12-14 15:17:28 -05:00
|
|
|
it('should return valid parameter', () => {
|
2017-01-13 12:51:19 -05:00
|
|
|
const value = gl.utils.getParameterByName('scope');
|
|
|
|
expect(value).toBe('all');
|
2016-12-14 15:17:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return invalid parameter', () => {
|
|
|
|
const value = gl.utils.getParameterByName('fakeParameter');
|
|
|
|
expect(value).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2017-01-24 11:56:26 -05:00
|
|
|
|
|
|
|
describe('gl.utils.normalizedHeaders', () => {
|
|
|
|
it('should upperCase all the header keys to keep them consistent', () => {
|
|
|
|
const apiHeaders = {
|
|
|
|
'X-Something-Workhorse': { workhorse: 'ok' },
|
|
|
|
'x-something-nginx': { nginx: 'ok' },
|
|
|
|
};
|
|
|
|
|
|
|
|
const normalized = gl.utils.normalizeHeaders(apiHeaders);
|
|
|
|
|
|
|
|
const WORKHORSE = 'X-SOMETHING-WORKHORSE';
|
|
|
|
const NGINX = 'X-SOMETHING-NGINX';
|
|
|
|
|
|
|
|
expect(normalized[WORKHORSE].workhorse).toBe('ok');
|
|
|
|
expect(normalized[NGINX].nginx).toBe('ok');
|
|
|
|
});
|
|
|
|
});
|
2017-02-07 11:09:40 -05:00
|
|
|
|
2017-02-16 07:02:55 -05:00
|
|
|
describe('gl.utils.parseIntPagination', () => {
|
|
|
|
it('should parse to integers all string values and return pagination object', () => {
|
|
|
|
const pagination = {
|
|
|
|
'X-PER-PAGE': 10,
|
|
|
|
'X-PAGE': 2,
|
|
|
|
'X-TOTAL': 30,
|
|
|
|
'X-TOTAL-PAGES': 3,
|
|
|
|
'X-NEXT-PAGE': 3,
|
|
|
|
'X-PREV-PAGE': 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
const expectedPagination = {
|
|
|
|
perPage: 10,
|
|
|
|
page: 2,
|
|
|
|
total: 30,
|
|
|
|
totalPages: 3,
|
|
|
|
nextPage: 3,
|
|
|
|
previousPage: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(gl.utils.parseIntPagination(pagination)).toEqual(expectedPagination);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-07 11:09:40 -05:00
|
|
|
describe('gl.utils.isMetaClick', () => {
|
|
|
|
it('should identify meta click on Windows/Linux', () => {
|
|
|
|
const e = {
|
|
|
|
metaKey: false,
|
|
|
|
ctrlKey: true,
|
|
|
|
which: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(gl.utils.isMetaClick(e)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should identify meta click on macOS', () => {
|
|
|
|
const e = {
|
|
|
|
metaKey: true,
|
|
|
|
ctrlKey: false,
|
|
|
|
which: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(gl.utils.isMetaClick(e)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should identify as meta click on middle-click or Mouse-wheel click', () => {
|
|
|
|
const e = {
|
|
|
|
metaKey: false,
|
|
|
|
ctrlKey: false,
|
|
|
|
which: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(gl.utils.isMetaClick(e)).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2016-11-17 07:05:32 -05:00
|
|
|
});
|
|
|
|
})();
|