2017-01-09 23:23:54 +00:00
|
|
|
require('~/lib/utils/common_utils');
|
2016-11-17 12:05:32 +00: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 22:35:58 +00:00
|
|
|
// The JavaScript test suite is executed at '/' which will lead to an absolute url
|
|
|
|
// starting with '/'.
|
2017-01-13 16:04:41 +00:00
|
|
|
expect(gl.utils.parseUrl('" test="asf"').pathname).toContain('/%22%20test=%22asf%22');
|
2016-11-17 12:05:32 +00:00
|
|
|
});
|
|
|
|
});
|
2016-12-14 20:17:28 +00:00
|
|
|
|
2016-11-17 12:05:32 +00: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 20:17:28 +00: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 16:16:26 +00:00
|
|
|
describe('gl.utils.handleLocationHash', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
window.history.pushState({}, null, '#definição');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('decodes hash parameter', () => {
|
|
|
|
spyOn(window.document, 'getElementById').and.callThrough();
|
|
|
|
gl.utils.handleLocationHash();
|
|
|
|
expect(window.document.getElementById).toHaveBeenCalledWith('definição');
|
|
|
|
expect(window.document.getElementById).toHaveBeenCalledWith('user-content-definição');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-12-14 20:17:28 +00:00
|
|
|
describe('gl.utils.getParameterByName', () => {
|
2017-01-13 17:51:19 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
window.history.pushState({}, null, '?scope=all&p=2');
|
|
|
|
});
|
|
|
|
|
2016-12-14 20:17:28 +00:00
|
|
|
it('should return valid parameter', () => {
|
2017-01-13 17:51:19 +00:00
|
|
|
const value = gl.utils.getParameterByName('scope');
|
|
|
|
expect(value).toBe('all');
|
2016-12-14 20:17:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return invalid parameter', () => {
|
|
|
|
const value = gl.utils.getParameterByName('fakeParameter');
|
|
|
|
expect(value).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2017-01-24 16:56:26 +00: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');
|
|
|
|
});
|
|
|
|
});
|
2016-11-17 12:05:32 +00:00
|
|
|
});
|
|
|
|
})();
|