diff options
Diffstat (limited to 'spec/frontend/lib/utils/url_utility_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index a13ac3778cf..869ae274a3f 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -161,6 +161,15 @@ describe('URL utility', () => { ); }); + it('sorts params in alphabetical order with sort option', () => { + expect(mergeUrlParams({ c: 'c', b: 'b', a: 'a' }, 'https://host/path', { sort: true })).toBe( + 'https://host/path?a=a&b=b&c=c', + ); + expect( + mergeUrlParams({ alpha: 'alpha' }, 'https://host/path?op=/&foo=bar', { sort: true }), + ).toBe('https://host/path?alpha=alpha&foo=bar&op=%2F'); + }); + describe('with spread array option', () => { const spreadArrayOptions = { spreadArrays: true }; @@ -616,6 +625,35 @@ describe('URL utility', () => { expect(urlUtils.queryToObject(searchQuery)).toEqual({ one: '1', two: '2' }); }); + + describe('with gatherArrays=false', () => { + it('overwrites values with the same array-key and does not change the key', () => { + const searchQuery = '?one[]=1&one[]=2&two=2&two=3'; + + expect(urlUtils.queryToObject(searchQuery)).toEqual({ 'one[]': '2', two: '3' }); + }); + }); + + describe('with gatherArrays=true', () => { + const options = { gatherArrays: true }; + it('gathers only values with the same array-key and strips `[]` from the key', () => { + const searchQuery = '?one[]=1&one[]=2&two=2&two=3'; + + expect(urlUtils.queryToObject(searchQuery, options)).toEqual({ one: ['1', '2'], two: '3' }); + }); + + it('overwrites values with the same array-key name', () => { + const searchQuery = '?one=1&one[]=2&two=2&two=3'; + + expect(urlUtils.queryToObject(searchQuery, options)).toEqual({ one: ['2'], two: '3' }); + }); + + it('overwrites values with the same key name', () => { + const searchQuery = '?one[]=1&one=2&two=2&two=3'; + + expect(urlUtils.queryToObject(searchQuery, options)).toEqual({ one: '2', two: '3' }); + }); + }); }); describe('objectToQuery', () => { |