Skip to content

Commit b10a04f

Browse files
author
pynnl
committed
add test: optional, redirect, active
1 parent 27e282f commit b10a04f

File tree

7 files changed

+112
-42
lines changed

7 files changed

+112
-42
lines changed

cypress/integration/main.spec.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ it('optional', () => {
8484
checkRoutes(arr, 3)
8585
})
8686

87+
it('redirect', () => {
88+
let arr = [1, 2, 3]
89+
90+
cy.visit('#/redirect')
91+
checkRoutes(arr)
92+
93+
cy.visit('#/redirect/route4')
94+
checkRoutes(arr, 1)
95+
96+
cy.visit('#/redirect/route5')
97+
checkRoutes(arr, 2)
98+
99+
cy.visit('#/redirect-whatever')
100+
checkRoutes(arr, 3)
101+
})
102+
87103
it('params', () => {
88104
let arr = [1, 2, 3]
89105
let checkProp = (id, name, wildcard) => {
@@ -109,6 +125,46 @@ it('params', () => {
109125
checkRoutes(arr, 3)
110126

111127
cy.visit('#/params/123')
112-
checkProp(undefined, undefined, '/123')
128+
checkProp(null, null, '/123')
113129
checkRoutes(arr, 4)
114130
})
131+
132+
it('query', () => {
133+
let checkProp = (id, name) => {
134+
checkProperty('id', id)
135+
checkProperty('name', name)
136+
checkProperty('whatever')
137+
}
138+
139+
cy.visit('#/query?')
140+
checkProp()
141+
142+
cy.visit('#/query?id=1')
143+
checkProp(1)
144+
145+
cy.visit('#/query?name=John')
146+
checkProp(null, 'John')
147+
148+
cy.visit('#/query?id=1&name=John')
149+
checkProp(1, 'John')
150+
})
151+
152+
it('active', () => {
153+
let arr = [1, 2, 3]
154+
155+
cy.visit('#/active')
156+
checkProperty('href', '#/active')
157+
checkRoutes(arr)
158+
159+
cy.visit('#/active/route1')
160+
checkProperty('href', '#/active/route1')
161+
checkRoutes(arr, 1)
162+
163+
cy.visit('#/active/route2')
164+
checkProperty('href', '#/active/route2')
165+
checkRoutes(arr, 2)
166+
167+
cy.visit('#/active/route2/route3')
168+
checkProperty('href', '#/active/route2/route3')
169+
checkRoutes(arr, 2, 3)
170+
})

test/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import RootRoutes, { schema as _RootRoutes } from './tests/RootRoutes.svelte'
44
import NestedRoutes, { schema as _NestedRoutes } from './tests/NestedRoutes.svelte'
55
import Wildcard, { schema as _Wildcard } from './tests/Wildcard.svelte'
66
import Optional, { schema as _Optional } from './tests/Optional.svelte'
7+
import Redirect, { schema as _Redirect } from './tests/Redirect.svelte'
78
import Params, { schema as _Params } from './tests/Params.svelte'
9+
import Query from './tests/Query.svelte'
10+
import Active, { schema as _Active } from './tests/Active.svelte'
811

912
routes.set({
1013
'/': {
@@ -13,7 +16,10 @@ routes.set({
1316
'nested-routes': { $$component: NestedRoutes, ..._NestedRoutes },
1417
'wildcard': { $$component: Wildcard, ..._Wildcard },
1518
'optional': { $$component: Optional, ..._Optional },
16-
'params': { $$component: Params, ..._Params }
19+
'redirect': { $$component: Redirect, ..._Redirect },
20+
'params': { $$component: Params, ..._Params },
21+
'query': Query,
22+
'active': { $$component: Active, ..._Active }
1723
}
1824
})
1925

test/src/tests/Active.svelte

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script context='module'>
2+
import Router, { routes, active } from '../../../src'
3+
import Route1 from '../components/Route1.svelte'
4+
import Route2 from '../components/Route2.svelte'
5+
import Route3 from '../components/Route3.svelte'
6+
import Navigator from '../components/Navigator.svelte'
7+
import Property from '../components/Property.svelte'
8+
9+
export let schema = {
10+
'/route1': Route1,
11+
'/route2': {
12+
$$component: Route2,
13+
'/route3': Route3
14+
}
15+
}
16+
</script>
17+
18+
<Navigator route='{$routes[`/`][`active`]}'/>
19+
<div>
20+
<Property key='href' value={$active.$$href}/>
21+
<Router deep={true}/>
22+
</div>

test/src/tests/Optional.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export let schema = {
1212
}
1313
</script>
1414

15-
<Navigator route='{$routes[`/`][`wildcard`]}'/>
15+
<Navigator route='{$routes[`/`][`optional`]}'/>
1616
<Router/>

test/src/tests/Params.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<script context='module'>
2-
import Router, { params } from '../../../src'
2+
import Router, { routes, params } from '../../../src'
33
import Route1 from '../components/Route1.svelte'
44
import Route2 from '../components/Route2.svelte'
55
import Route3 from '../components/Route3.svelte'
66
import Route4 from '../components/Route4.svelte'
7+
import Navigator from '../components/Navigator.svelte'
78
import Property from '../components/Property.svelte'
89
910
export let schema = {
@@ -14,6 +15,7 @@ export let schema = {
1415
}
1516
</script>
1617

18+
<Navigator route='{$routes[`/`][`params`]}'/>
1719
<div>
1820
{#each Object.entries($params) as e}
1921
<Property key={e[0]} value={e[1]}/>

test/src/tests/Query.svelte

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
<script>
2-
import { query } from '../../../src'
3-
import Link from '../components/Link.svelte'
4-
5-
let to = [
6-
'?a&b',
7-
'?a=1&b=c'
8-
]
1+
<script context='module'>
2+
import Router, { query } from '../../../src'
3+
import Property from '../components/Property.svelte'
94
</script>
105

116
<div>
12-
{#each to as e}
13-
<Link href='{`#/query` + e}' label={e}/>
7+
{#each Object.entries($query) as e}
8+
<Property key={e[0]} value={e[1]}/>
149
{/each}
10+
<Router/>
1511
</div>
16-
<pre>
17-
{JSON.stringify($query, null, 2)}
18-
</pre>
19-
20-
<style>
21-
pre {
22-
margin: 10px;
23-
font-size: 20px;
24-
}
25-
</style>

test/src/tests/Redirect.svelte

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
<script>
2-
import Link from '../components/Link.svelte'
1+
<script context='module'>
2+
import Router, { routes } from '../../../src'
3+
import Route1 from '../components/Route1.svelte'
4+
import Route2 from '../components/Route2.svelte'
5+
import Route3 from '../components/Route3.svelte'
6+
import Navigator from '../components/Navigator.svelte'
37
4-
let to = [
5-
'/a-link',
6-
'what-e-ver'
7-
]
8+
export let schema = {
9+
'/route1': Route1,
10+
'/route2': Route2,
11+
'/route3': Route3,
12+
'/route4': '/redirect/route1',
13+
'/route5': { $$redirect: '/redirect/route2' },
14+
'*': '/redirect/route3'
15+
}
816
</script>
917

10-
<div>All routes not matched will be redirected here</div>
11-
<div>
12-
{#each to as e}
13-
<Link href='{`#` + e}' label={e}/>
14-
{/each}
15-
</div>
16-
17-
<style>
18-
div {
19-
font-size: 20px;
20-
}
21-
</style>
18+
<Navigator route='{$routes[`/`][`redirect`]}'/>
19+
<Router/>

0 commit comments

Comments
 (0)