import { getQuery, hump2Underline, stringifyQuery, underline2HumpObjectKey } from '..'; describe('getQuery', () => { it('works ok', () => { expect(getQuery('http://localhost/home')).toEqual({}); expect(getQuery('http://localhost/home?a=1&b=2')).toEqual({ a: '1', b: '2' }); expect(getQuery('http://localhost/home?a=1&b=2#c=3')).toEqual({ a: '1', b: '2' }); expect(getQuery('http://localhost/home?a=1&b=2#c=3?d=4')).toEqual({ a: '1', b: '2' }); expect(getQuery('?a=1&b=2#c=3?d=4')).toEqual({ a: '1', b: '2' }); expect(getQuery('a=1&b=2#c=3?d=4')).toEqual({ a: '1', b: '2' }); expect( getQuery( 'http://localhost/home?a=1&context_id=123-123-123-123123123&entrance_id=ei12312&sub_entranceid=se123&from_access_id=fa123&from_element_id=fe123', ), ).toEqual({ a: '1', context_id: '123-123-123-123123123', entrance_id: 'ei12312', sub_entranceid: 'se123', from_access_id: 'fa123', from_element_id: 'fe123', }); }); }); describe('stringifyQuery', () => { it('works ok', () => { expect(stringifyQuery({})).toEqual(''); expect(stringifyQuery({ a: undefined, b: null })).toEqual(''); expect(stringifyQuery({ a: undefined, b: 1, c: null })).toEqual('b=1'); expect(stringifyQuery({ a: undefined, b: 1, c: null, d: '' })).toEqual('b=1&d='); expect(stringifyQuery({ a: 1 })).toEqual('a=1'); expect(stringifyQuery({ a: 1, b: 2 })).toEqual('a=1&b=2'); expect(stringifyQuery({ a: 1, b: 2, c: 3 })).toEqual('a=1&b=2&c=3'); expect(stringifyQuery({ aB: 'aB', Bc: 'Bc', CD: 'CD', de: 'de' }, true)).toEqual('a_b=aB&Bc=Bc&CD=CD&de=de'); expect(stringifyQuery({ aB: 'aB', Bc: 'Bc', CD: 'CD', de: 'de' }, false)).toEqual('aB=aB&Bc=Bc&CD=CD&de=de'); }); }); describe('hump2Underline', () => { it('works ok', () => { expect(hump2Underline('Abc')).toBe('Abc'); expect(hump2Underline('ABc')).toBe('ABc'); expect(hump2Underline('AbC')).toBe('AbC'); expect(hump2Underline('aBc')).toBe('a_bc'); expect(hump2Underline('aBcD')).toBe('a_bc_d'); }); }); describe('underline2HumpObjectKey', () => { it('works ok', () => { expect((underline2HumpObjectKey as any)?.(undefined)).toBeUndefined(); expect((underline2HumpObjectKey as any)?.(1)).toBe(1); expect(underline2HumpObjectKey({})).toStrictEqual({}); expect(underline2HumpObjectKey({ a_b: 'a_b' })).toStrictEqual({ aB: 'a_b' }); expect(underline2HumpObjectKey({ a_b: 'a_b', a_c: 'a_c' })).toStrictEqual({ aB: 'a_b', aC: 'a_c' }); expect(underline2HumpObjectKey({ Ab: 'a_b', a_c: 'a_c', aD: 'a_d' })).toStrictEqual({ Ab: 'a_b', aC: 'a_c', aD: 'a_d', }); expect(underline2HumpObjectKey({ entrance_id: '123123', contextId: '114514', Feed_id: '1' })).toStrictEqual({ entranceId: '123123', contextId: '114514', Feed_id: '1', }); expect(underline2HumpObjectKey({ entranceId: '123123', entrance_id: '114514' })).toStrictEqual({ entranceId: '114514', }); expect(underline2HumpObjectKey({ entrance_id: '114514', entranceId: '123123' })).toStrictEqual({ entranceId: '123123', }); }); });