moy-router
Version:
Give a solution for moy-dom router management.
93 lines (86 loc) • 2.28 kB
JavaScript
import createHistory from 'src/createHistory'
beforeEach(() => {
let eventHandler = jest.fn()
window.addEventListener = jest.fn(
(type, fn) => type === 'popstate' && (eventHandler = fn)
)
window.history.back = jest.fn(() => eventHandler({
state: {
route: {
pathname: '/murakami/back',
},
},
}))
window.history.go = jest.fn(index => eventHandler({
state: {
route: {
pathname: `/murakami/go/${index}`,
},
},
}))
window.history.forward = jest.fn(() => eventHandler({
state: {
route: {
pathname: '/murakami/forward',
},
},
}))
window.history.replaceState = jest.fn()
window.history.pushState = jest.fn()
})
describe('test for createHistory', () => {
test('createHistory function', () => {
const history = createHistory({})
expect(window.addEventListener).toHaveBeenCalledTimes(1)
})
test('createHistory back', () => {
const current = {}
expect(current.route).toBe(undefined)
const history = createHistory(current)
history.back()
expect(current.route).toEqual({
pathname: '/murakami/back',
})
})
test('createHistory go', () => {
const current = {}
expect(current.route).toBe(undefined)
const history = createHistory(current)
history.go(1)
expect(current.route).toEqual({
pathname: '/murakami/go/1',
})
})
test('createHistory forward', () => {
const current = {}
expect(current.route).toBe(undefined)
const history = createHistory(current)
history.forward()
expect(current.route).toEqual({
pathname: '/murakami/forward',
})
})
test('createHistory replace', () => {
const current = {}
expect(current.route).toBe(undefined)
const history = createHistory(current)
history.replace({
pathname: '/murakami/replace',
})
expect(current.route).toEqual({
pathname: '/murakami/replace',
})
})
test('createHistory push', () => {
const current = {}
window.eventHandler = e => current
expect(current.route).toBe(undefined)
const history = createHistory(current)
history.push({
pathname: '/murakami/push',
})
expect(current.route).toEqual({
pathname: '/murakami/push',
})
})
})