Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct typedef of Route #3901

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export interface Route {
path: string
name?: string | null
hash: string
query: Dictionary<string | (string | null)[]>
query: Dictionary<string | null | (string | null)[]>
params: Dictionary<string>
fullPath: string
matched: RouteRecord[]
Expand Down
103 changes: 103 additions & 0 deletions types/test/route-query.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import VueRouter from '../index'
import { Route } from '../index'

const component = { template: '<div>test</div>' }

const router = new VueRouter({
routes: [
{ path: '/:id', component }
]
})

describe('Route query types', () => {
it('should handle string query parameter', () => {
const route: Route = {
path: '/test',
query: { foo: 'bar' },
params: {},
fullPath: '/test?foo=bar',
name: null,
hash: '',
matched: [],
redirectedFrom: undefined,
meta: {}
}

expect(typeof route.query.foo).toBe('string')
})

it('should handle null query parameter', () => {
const route: Route = {
path: '/test',
query: { foo: null },
params: {},
fullPath: '/test?foo',
name: null,
hash: '',
matched: [],
redirectedFrom: undefined,
meta: {}
}

expect(route.query.foo).toBeNull()
})

it('should handle array of strings query parameter', () => {
const route: Route = {
path: '/test',
query: { foo: ['bar', 'baz'] },
params: {},
fullPath: '/test?foo=bar&foo=baz',
name: null,
hash: '',
matched: [],
redirectedFrom: undefined,
meta: {}
}

expect(Array.isArray(route.query.foo)).toBe(true)
expect(route.query.foo).toEqual(['bar', 'baz'])
})

it('should handle array with null query parameter', () => {
const route: Route = {
path: '/test',
query: { foo: ['bar', null] },
params: {},
fullPath: '/test?foo=bar&foo',
name: null,
hash: '',
matched: [],
redirectedFrom: undefined,
meta: {}
}

expect(Array.isArray(route.query.foo)).toBe(true)
expect(route.query.foo).toEqual(['bar', null])
})

it('should handle mixed query parameters', () => {
const route: Route = {
path: '/test',
query: {
string: 'value',
nullValue: null,
stringArray: ['one', 'two'],
mixedArray: ['three', null]
},
params: {},
fullPath: '/test?string=value&nullValue&stringArray=one&stringArray=two&mixedArray=three&mixedArray',
name: null,
hash: '',
matched: [],
redirectedFrom: undefined,
meta: {}
}

expect(typeof route.query.string).toBe('string')
expect(route.query.nullValue).toBeNull()
expect(Array.isArray(route.query.stringArray)).toBe(true)
expect(Array.isArray(route.query.mixedArray)).toBe(true)
expect(route.query.mixedArray).toEqual(['three', null])
})
})