UNPKG

@cc-heart/utils

Version:

🔧 javascript common tools collection

66 lines (55 loc) • 2.09 kB
import { convertParamsRouterToRegExp, convertQueryString, } from '../../../lib/url' describe('convertQueryString', () => { test('should return an empty object if no query string is provided', () => { expect(convertQueryString('http://example.com')).toEqual({}) }) test('should return an object with query parameters', () => { const url = 'http://example.com/?foo=bar&baz=qux' const expected = { foo: 'bar', baz: 'qux' } expect(convertQueryString(url)).toEqual(expected) }) test('should handle URL-encoded values', () => { const url = 'http://example.com/?q=hello%20world' const expected = { q: 'hello world' } expect(convertQueryString(url)).toEqual(expected) }) test('should handle empty keys', () => { const url = 'http://example.com/?=empty&foo=bar' const expected = { '': 'empty', foo: 'bar' } expect(convertQueryString(url)).toEqual(expected) }) test('should handle empty values', () => { const url = 'http://example.com/?foo=&bar=' const expected = { foo: '', bar: '' } expect(convertQueryString(url)).toEqual(expected) }) }) describe('convertParamsRouterToRegExp', () => { test('should return an null if no path is provided', () => { expect(convertParamsRouterToRegExp('')).toBeNull() }) test('should return //path/.*?/index/ string if path is /path/:id/index', () => { expect(convertParamsRouterToRegExp('/path/:id/index')).toEqual([ /\/path\/.*?\/index/, ['id'], ]) }) test('should return /path/.*?/.*? string if path is /path/:id/:uid', () => { expect(convertParamsRouterToRegExp('/path/:id/:uid')).toEqual([ /\/path\/.*?\/.*?/, ['id', 'uid'], ]) }) test('should return /path/.*?/.*?/index/ string if path is /path/:id/:uid/index', () => { expect(convertParamsRouterToRegExp('/path/:id/:uid/index')).toEqual([ /\/path\/.*?\/.*?\/index/, ['id', 'uid'], ]) }) test('should return null string if path is /path/index/layout', () => { expect(convertParamsRouterToRegExp('/path/index/layout')).toBeNull() }) })