forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouterContext-test.js
More file actions
148 lines (126 loc) · 4.27 KB
/
RouterContext-test.js
File metadata and controls
148 lines (126 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import expect from 'expect'
import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import match from '../match'
import { router as routerPropType } from '../PropTypes'
import RouterContext from '../RouterContext'
import { createRouterObject } from '../RouterUtils'
describe('RouterContext', () => {
let node, routes, context, history, transitionManager, router
let listenBeforeLeavingRouteSentinel, isActiveSentinel, createHrefSentinel
beforeEach(() => {
listenBeforeLeavingRouteSentinel = {}
isActiveSentinel = {}
createHrefSentinel = {}
node = document.createElement('div')
history = {
push: expect.createSpy(),
replace: expect.createSpy(),
createHref: expect.createSpy().andReturn(createHrefSentinel),
go: expect.createSpy(),
goBack: expect.createSpy(),
goForward: expect.createSpy()
}
transitionManager = {
listenBeforeLeavingRoute: expect.createSpy().andReturn(listenBeforeLeavingRouteSentinel),
isActive: expect.createSpy().andReturn(isActiveSentinel)
}
router = createRouterObject(history, transitionManager)
class Component extends React.Component {
constructor(props, ctx) {
super(props, ctx)
context = ctx
}
render() { return null }
}
Component.contextTypes = {
router: routerPropType.isRequired
}
routes = { path: '/', component: Component }
})
afterEach(() => unmountComponentAtNode(node))
function renderTest(done) {
match({ location: '/', routes }, (err, redirect, renderProps) => {
render(<RouterContext {...renderProps} history={history} router={router} />, node)
done()
})
}
describe('2.0', () => {
it('exports only `router` to context')
})
it('exports a `router` object to routing context', (done) => {
renderTest(() => {
expect(context.router).toExist()
done()
})
})
describe('some weird tests that test implementation and should probably go away', () => {
it('proxies calls to `push` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
context.router.push(...args)
expect(history.push).toHaveBeenCalledWith(...args)
done()
})
})
it('proxies calls to `replace` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
context.router.replace(...args)
expect(history.replace).toHaveBeenCalledWith(...args)
done()
})
})
it('proxies calls to `setRouteLeaveHook` to `props.transitionManager`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
const remove = context.router.setRouteLeaveHook(...args)
expect(transitionManager.listenBeforeLeavingRoute).toHaveBeenCalledWith(...args)
expect(remove).toBe(listenBeforeLeavingRouteSentinel)
done()
})
})
it('proxies calls to `isActive` to `props.transitionManager`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
const isActive = context.router.isActive(...args)
expect(transitionManager.isActive).toHaveBeenCalledWith(...args)
expect(isActive).toBe(isActiveSentinel)
done()
})
})
it('proxies calls to `createHref` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
const href = context.router.createHref(...args)
expect(history.createHref).toHaveBeenCalledWith(...args)
expect(href).toBe(createHrefSentinel)
done()
})
})
it('proxies calls to `go` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
context.router.go(...args)
expect(history.go).toHaveBeenCalledWith(...args)
done()
})
})
it('proxies calls to `goBack` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
context.router.goBack(...args)
expect(history.goBack).toHaveBeenCalledWith(...args)
done()
})
})
it('proxies calls to `goForward` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
renderTest(() => {
context.router.goForward(...args)
expect(history.goForward).toHaveBeenCalledWith(...args)
done()
})
})
})
})