moy-router
Version:
Give a solution for moy-dom router management.
213 lines (200 loc) • 4.11 kB
JavaScript
import {
identity,
reduce,
tap,
} from 'moy-fp'
import pathToRegExp from 'path-to-regexp'
import RouterView from 'src/components/RouterView'
let routes = [];
beforeEach(() => {
const keys = reduce(
(obj, key) => tap(
obj => obj[key] = []
)(obj)
)({})(['/murakami', '/user/:name'])
routes = [{
pathnameRegExp: pathToRegExp('/murakami', keys['/murakami'], {sensitive: true}),
paramKeys: keys['/murakami'],
component: identity,
}, {
pathnameRegExp: pathToRegExp('/user/:name', keys['/user/:name'], {sensitive: true}),
paramKeys: keys['/user/:name'],
component: identity,
}]
})
describe('test for RouterView', () => {
test('current.route without query, hash, and params', () => {
expect(
RouterView({
route: {
pathname: '/murakami',
},
}, routes)()
).toEqual({
route: {
pathname: '/murakami',
},
})
})
test('current.route only with query', () => {
expect(
RouterView({
route: {
pathname: '/murakami?a=1&b=2',
query: {
a: '1',
b: '2',
},
},
}, routes)()
).toEqual({
route: {
pathname: '/murakami?a=1&b=2',
query: {
a: '1',
b: '2',
},
},
})
})
test('current.route only with params implicitly', () => {
expect(
RouterView({
route: {
pathname: '/user/murakami',
},
}, routes)()
).toEqual({
route: {
pathname: '/user/murakami',
params: {
name: 'murakami'
},
},
})
})
test('current.route only with params explicitly', () => {
expect(
RouterView({
route: {
pathname: '/user/murakami',
params: {
name: 'murakami',
},
},
}, routes)()
).toEqual({
route: {
pathname: '/user/murakami',
params: {
name: 'murakami'
},
},
})
})
test('current.route only with hash', () => {
expect(
RouterView({
route: {
pathname: '/murakami#a',
hash: 'a',
},
}, routes)()
).toEqual({
route: {
pathname: '/murakami#a',
hash: 'a',
},
})
})
test('current.route with params and query', () => {
expect(
RouterView({
route: {
pathname: '/user/murakami?a=1&b=2',
query: {
a: '1',
b: '2',
},
},
}, routes)()
).toEqual({
route: {
pathname: '/user/murakami?a=1&b=2',
params: {
name: 'murakami',
},
query: {
a: '1',
b: '2',
},
},
})
})
test('current.route with params and hash', () => {
expect(
RouterView({
route: {
pathname: '/user/murakami#a',
hash: 'a',
},
}, routes)()
).toEqual({
route: {
pathname: '/user/murakami#a',
params: {
name: 'murakami',
},
hash: 'a',
},
})
})
test('current.route with query and hash', () => {
expect(
RouterView({
route: {
pathname: '/murakami?a=1&b=2#a',
query: {
a: '1',
b: '2',
},
hash: 'a',
},
}, routes)()
).toEqual({
route: {
pathname: '/murakami?a=1&b=2#a',
query: {
a: '1',
b: '2',
},
hash: 'a',
},
})
})
test('RouterView with props', () => {
expect(
RouterView({
route: {
pathname: '/murakami',
},
}, routes)({
occupation: 'front-end developer',
})
).toEqual({
occupation: 'front-end developer',
route: {
pathname: '/murakami',
},
})
})
test('current.route not natched routes', () => {
expect(
RouterView({
route: {
pathname: '/home',
},
}, routes)()
).toEqual('')
})
})