diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /spec/frontend/lib | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'spec/frontend/lib')
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 115 |
1 files changed, 41 insertions, 74 deletions
diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index 66d0faa95e7..c8ac7ffc9d9 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -1,3 +1,4 @@ +import setWindowLocation from 'helpers/set_window_location_helper'; import { TEST_HOST } from 'helpers/test_constants'; import * as urlUtils from '~/lib/utils/url_utility'; @@ -16,24 +17,11 @@ const shas = { ], }; -const setWindowLocation = (value) => { - Object.defineProperty(window, 'location', { - writable: true, - value, - }); -}; +beforeEach(() => { + setWindowLocation(TEST_HOST); +}); describe('URL utility', () => { - let originalLocation; - - beforeAll(() => { - originalLocation = window.location; - }); - - afterAll(() => { - window.location = originalLocation; - }); - describe('webIDEUrl', () => { afterEach(() => { gon.relative_url_root = ''; @@ -68,14 +56,7 @@ describe('URL utility', () => { describe('getParameterValues', () => { beforeEach(() => { - setWindowLocation({ - href: 'https://gitlab.com?test=passing&multiple=1&multiple=2', - // make our fake location act like real window.location.toString - // URL() (used in getParameterValues) does this if passed an object - toString() { - return this.href; - }, - }); + setWindowLocation('https://gitlab.com?test=passing&multiple=1&multiple=2'); }); it('returns empty array for no params', () => { @@ -330,9 +311,7 @@ describe('URL utility', () => { describe('doesHashExistInUrl', () => { beforeEach(() => { - setWindowLocation({ - hash: 'https://gitlab.com/gitlab-org/gitlab-test/issues/1#note_1', - }); + setWindowLocation('#note_1'); }); it('should return true when the given string exists in the URL hash', () => { @@ -442,10 +421,7 @@ describe('URL utility', () => { describe('getBaseURL', () => { beforeEach(() => { - setWindowLocation({ - protocol: 'https:', - host: 'gitlab.com', - }); + setWindowLocation('https://gitlab.com'); }); it('returns correct base URL', () => { @@ -637,10 +613,7 @@ describe('URL utility', () => { ${'http:'} | ${'ws:'} ${'https:'} | ${'wss:'} `('returns "$expectation" with "$protocol" protocol', ({ protocol, expectation }) => { - setWindowLocation({ - protocol, - host: 'example.com', - }); + setWindowLocation(`${protocol}//example.com`); expect(urlUtils.getWebSocketProtocol()).toEqual(expectation); }); @@ -648,10 +621,7 @@ describe('URL utility', () => { describe('getWebSocketUrl', () => { it('joins location host to path', () => { - setWindowLocation({ - protocol: 'http:', - host: 'example.com', - }); + setWindowLocation('http://example.com'); const path = '/lorem/ipsum?a=bc'; @@ -700,21 +670,23 @@ describe('URL utility', () => { describe('queryToObject', () => { it.each` - case | query | options | result - ${'converts query'} | ${'?one=1&two=2'} | ${undefined} | ${{ one: '1', two: '2' }} - ${'converts query without ?'} | ${'one=1&two=2'} | ${undefined} | ${{ one: '1', two: '2' }} - ${'removes undefined values'} | ${'?one=1&two=2&three'} | ${undefined} | ${{ one: '1', two: '2' }} - ${'overwrites values with same key and does not change key'} | ${'?one[]=1&one[]=2&two=2&two=3'} | ${undefined} | ${{ 'one[]': '2', two: '3' }} - ${'gathers values with the same array-key, strips `[]` from key'} | ${'?one[]=1&one[]=2&two=2&two=3'} | ${{ gatherArrays: true }} | ${{ one: ['1', '2'], two: '3' }} - ${'overwrites values with the same array-key name'} | ${'?one=1&one[]=2&two=2&two=3'} | ${{ gatherArrays: true }} | ${{ one: ['2'], two: '3' }} - ${'overwrites values with the same key name'} | ${'?one[]=1&one=2&two=2&two=3'} | ${{ gatherArrays: true }} | ${{ one: '2', two: '3' }} - ${'ignores plus symbols'} | ${'?search=a+b'} | ${{ legacySpacesDecode: true }} | ${{ search: 'a+b' }} - ${'ignores plus symbols in keys'} | ${'?search+term=a'} | ${{ legacySpacesDecode: true }} | ${{ 'search+term': 'a' }} - ${'ignores plus symbols when gathering arrays'} | ${'?search[]=a+b'} | ${{ gatherArrays: true, legacySpacesDecode: true }} | ${{ search: ['a+b'] }} - ${'replaces plus symbols with spaces'} | ${'?search=a+b'} | ${undefined} | ${{ search: 'a b' }} - ${'replaces plus symbols in keys with spaces'} | ${'?search+term=a'} | ${undefined} | ${{ 'search term': 'a' }} - ${'replaces plus symbols when gathering arrays'} | ${'?search[]=a+b'} | ${{ gatherArrays: true }} | ${{ search: ['a b'] }} - ${'replaces plus symbols when gathering arrays for values with same key'} | ${'?search[]=a+b&search[]=c+d'} | ${{ gatherArrays: true }} | ${{ search: ['a b', 'c d'] }} + case | query | options | result + ${'converts query'} | ${'?one=1&two=2'} | ${undefined} | ${{ one: '1', two: '2' }} + ${'converts query without ?'} | ${'one=1&two=2'} | ${undefined} | ${{ one: '1', two: '2' }} + ${'removes undefined values'} | ${'?one=1&two=2&three'} | ${undefined} | ${{ one: '1', two: '2' }} + ${'overwrites values with same key and does not change key'} | ${'?one[]=1&one[]=2&two=2&two=3'} | ${undefined} | ${{ 'one[]': '2', two: '3' }} + ${'gathers values with the same array-key, strips `[]` from key'} | ${'?one[]=1&one[]=2&two=2&two=3'} | ${{ gatherArrays: true }} | ${{ one: ['1', '2'], two: '3' }} + ${'overwrites values with the same array-key name'} | ${'?one=1&one[]=2&two=2&two=3'} | ${{ gatherArrays: true }} | ${{ one: ['2'], two: '3' }} + ${'overwrites values with the same key name'} | ${'?one[]=1&one=2&two=2&two=3'} | ${{ gatherArrays: true }} | ${{ one: '2', two: '3' }} + ${'ignores plus symbols'} | ${'?search=a+b'} | ${{ legacySpacesDecode: true }} | ${{ search: 'a+b' }} + ${'ignores plus symbols in keys'} | ${'?search+term=a'} | ${{ legacySpacesDecode: true }} | ${{ 'search+term': 'a' }} + ${'ignores plus symbols when gathering arrays'} | ${'?search[]=a+b'} | ${{ gatherArrays: true, legacySpacesDecode: true }} | ${{ search: ['a+b'] }} + ${'replaces plus symbols with spaces'} | ${'?search=a+b'} | ${undefined} | ${{ search: 'a b' }} + ${'replaces plus symbols in keys with spaces'} | ${'?search+term=a'} | ${undefined} | ${{ 'search term': 'a' }} + ${'preserves square brackets in array params'} | ${'?search[]=a&search[]=b'} | ${{ gatherArrays: true }} | ${{ search: ['a', 'b'] }} + ${'decodes encoded square brackets in array params'} | ${'?search%5B%5D=a&search%5B%5D=b'} | ${{ gatherArrays: true }} | ${{ search: ['a', 'b'] }} + ${'replaces plus symbols when gathering arrays'} | ${'?search[]=a+b'} | ${{ gatherArrays: true }} | ${{ search: ['a b'] }} + ${'replaces plus symbols when gathering arrays for values with same key'} | ${'?search[]=a+b&search[]=c+d'} | ${{ gatherArrays: true }} | ${{ search: ['a b', 'c d'] }} `('$case', ({ query, options, result }) => { expect(urlUtils.queryToObject(query, options)).toEqual(result); }); @@ -724,32 +696,32 @@ describe('URL utility', () => { const { getParameterByName } = urlUtils; it('should return valid parameter', () => { - setWindowLocation({ search: '?scope=all&p=2' }); + setWindowLocation('?scope=all&p=2'); expect(getParameterByName('p')).toEqual('2'); expect(getParameterByName('scope')).toBe('all'); }); it('should return invalid parameter', () => { - setWindowLocation({ search: '?scope=all&p=2' }); + setWindowLocation('?scope=all&p=2'); expect(getParameterByName('fakeParameter')).toBe(null); }); it('should return a parameter with spaces', () => { - setWindowLocation({ search: '?search=my terms' }); + setWindowLocation('?search=my terms'); expect(getParameterByName('search')).toBe('my terms'); }); it('should return a parameter with encoded spaces', () => { - setWindowLocation({ search: '?search=my%20terms' }); + setWindowLocation('?search=my%20terms'); expect(getParameterByName('search')).toBe('my terms'); }); it('should return a parameter with plus signs as spaces', () => { - setWindowLocation({ search: '?search=my+terms' }); + setWindowLocation('?search=my+terms'); expect(getParameterByName('search')).toBe('my terms'); }); @@ -842,18 +814,20 @@ describe('URL utility', () => { }); describe('urlIsDifferent', () => { + const current = 'http://current.test/'; + beforeEach(() => { - setWindowLocation('current'); + setWindowLocation(current); }); it('should compare against the window location if no compare value is provided', () => { expect(urlUtils.urlIsDifferent('different')).toBeTruthy(); - expect(urlUtils.urlIsDifferent('current')).toBeFalsy(); + expect(urlUtils.urlIsDifferent(current)).toBeFalsy(); }); it('should use the provided compare value', () => { - expect(urlUtils.urlIsDifferent('different', 'current')).toBeTruthy(); - expect(urlUtils.urlIsDifferent('current', 'current')).toBeFalsy(); + expect(urlUtils.urlIsDifferent('different', current)).toBeTruthy(); + expect(urlUtils.urlIsDifferent(current, current)).toBeFalsy(); }); }); @@ -944,9 +918,8 @@ describe('URL utility', () => { it.each([[httpProtocol], [httpsProtocol]])( 'when no url passed, returns correct protocol for %i from window location', (protocol) => { - setWindowLocation({ - protocol, - }); + setWindowLocation(`${protocol}//test.host`); + expect(urlUtils.getHTTPProtocol()).toBe(protocol.slice(0, -1)); }, ); @@ -979,10 +952,8 @@ describe('URL utility', () => { describe('getURLOrigin', () => { it('when no url passed, returns correct origin from window location', () => { - const origin = 'https://foo.bar'; - - setWindowLocation({ origin }); - expect(urlUtils.getURLOrigin()).toBe(origin); + setWindowLocation('https://user:pass@origin.test:1234/foo/bar?foo=1#bar'); + expect(urlUtils.getURLOrigin()).toBe('https://origin.test:1234'); }); it.each` @@ -1032,10 +1003,6 @@ describe('URL utility', () => { // eslint-disable-next-line no-script-url const javascriptUrl = 'javascript:alert(1)'; - beforeEach(() => { - setWindowLocation({ origin: TEST_HOST }); - }); - it.each` url | expected ${TEST_HOST} | ${true} |