// @ts-check
require('./helper')
const expect = require('expect')
-const { parseURL } = require('../src/history/utils')
+const { parseURL, stringifyURL } = require('../src/history/utils')
-describe('URL parsing', () => {
+describe('parseURL', () => {
it('works with no query no hash', () => {
expect(parseURL('/foo')).toEqual({
fullPath: '/foo',
})
})
})
+
+describe('stringifyURL', () => {
+ it('stringifies a path', () => {
+ expect(
+ stringifyURL({
+ path: '/some-path',
+ })
+ ).toBe('/some-path')
+ })
+
+ it('stringifies a query with arrays', () => {
+ expect(
+ stringifyURL({
+ path: '/path',
+ query: {
+ foo: ['a1', 'a2'],
+ bar: 'b',
+ },
+ })
+ ).toBe('/path?foo=a1&foo=a2&bar=b')
+ })
+
+ it('stringifies a query', () => {
+ expect(
+ stringifyURL({
+ path: '/path',
+ query: {
+ foo: 'a',
+ bar: 'b',
+ },
+ })
+ ).toBe('/path?foo=a&bar=b')
+ })
+
+ it('stringifies a hash', () => {
+ expect(
+ stringifyURL({
+ path: '/path',
+ hash: '#hey',
+ })
+ ).toBe('/path#hey')
+ })
+
+ it('stringifies a query and a hash', () => {
+ expect(
+ stringifyURL({
+ path: '/path',
+ query: {
+ foo: 'a',
+ bar: 'b',
+ },
+ hash: '#hey',
+ })
+ ).toBe('/path?foo=a&bar=b#hey')
+ })
+})
module.exports = {
preset: 'ts-jest',
+ collectCoverageFrom: ['src/**/*.ts'],
testMatch: [
'**/__tests__/**/*.spec.[j]s?(x)',
// '**/__tests__/**/*.spec.[jt]s?(x)',
+++ /dev/null
-import { RouteQuery } from '../types'
-
-// TODO: merge with existing function from history/base.ts and more to
-// history utils
-export function stringifyQuery(query: RouteQuery | void): string {
- if (!query) return ''
-
- let search = '?'
- for (const key in query) {
- // TODO: handle arrays
- search += `${key}=${query[key]}`
- }
-
- // no query means empty string
- return search === '?' ? '' : ''
-}