forked from supasate/connected-react-router
/
selectors.test.js
141 lines (127 loc) · 4.16 KB
/
selectors.test.js
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
import { createStore, combineReducers } from "redux"
import { createBrowserHistory } from 'history'
import { connectRouter, getLocation, createMatchSelector, getAction } from '../src'
import { onLocationChanged } from '../src/actions'
const push = pathname => onLocationChanged(
{
pathname,
search: '',
hash: '',
},
'PUSH'
)
describe("selectors", () => {
let store
beforeEach(() => {
const history = createBrowserHistory()
const reducer = combineReducers({
router: connectRouter(history)
})
store = createStore(reducer)
})
describe("when router not found under 'router' key", () => {
beforeEach(() => {
const reducer = combineReducers({
notTheRouter: connectRouter(history)
})
store = createStore(reducer)
})
it("throws helpful error", () => {
store.dispatch(push('/'))
const state = store.getState()
expect(() => getLocation(state)).toThrowError(/^Could not find router reducer in state tree, it must be mounted under "router"$/)
})
})
describe("when something else found under 'router' key", () => {
beforeEach(() => {
const reducer = combineReducers({
router: () => ({ some: 'thing' })
})
store = createStore(reducer)
})
it("throws helpful error", () => {
store.dispatch(push('/'))
const state = store.getState()
expect(() => getLocation(state)).toThrowError(/^Could not find router reducer in state tree, it must be mounted under "router"$/)
})
})
describe("getLocation", () => {
it("gets the location from the state", () => {
const location = { pathname: "/", hash: '', search: '' }
store.dispatch(push('/'))
const state = store.getState()
expect(getLocation(state)).toEqual(location)
})
})
describe("getAction", () => {
it("gets the action from the state", () => {
const action = "PUSH"
store.dispatch(push('/'))
const state = store.getState()
expect(getAction(state)).toBe(action)
})
})
describe("createMatchSelector", () => {
it("matches correctly if the router is initialized", () => {
const matchSelector = createMatchSelector("/")
store.dispatch(push('/test'))
const state = store.getState()
expect(matchSelector(state)).toEqual({
isExact: false,
params: {},
path: "/",
url: "/"
})
})
it("does not throw error if router has not yet initialized", () => {
const matchSelector = createMatchSelector("/")
const state = store.getState()
expect(() => matchSelector(state)).not.toThrow()
})
it("does not update if the match is the same", () => {
const matchSelector = createMatchSelector("/")
store.dispatch(push('test1'))
const match = matchSelector(store.getState())
store.dispatch(push('test2'))
const expectedMatch = matchSelector(store.getState())
expect(match).toBe(expectedMatch)
})
it("updates if the match is different", () => {
const matchSelector = createMatchSelector("/sushi/:type")
store.dispatch(push('/sushi/california'))
const californiaMatch = matchSelector(store.getState())
store.dispatch(push('/sushi/dynamite'))
const dynamiteMatch = matchSelector(store.getState())
expect(californiaMatch).not.toBe(dynamiteMatch)
expect(californiaMatch).toEqual({
isExact: true,
params: {type: 'california'},
path: '/sushi/:type',
url: '/sushi/california',
})
expect(dynamiteMatch).toEqual({
isExact: true,
params: {type: 'dynamite'},
path: '/sushi/:type',
url: '/sushi/dynamite',
})
})
it("updates if the exact match is different", () => {
const matchSelector = createMatchSelector({
path: "/sushi",
exact: true
})
store.dispatch(push('/sushi'))
const okMatch = matchSelector(store.getState())
store.dispatch(push('/sushi/dynamite'))
const koMatch = matchSelector(store.getState())
expect(okMatch).toEqual({
isExact: true,
params: {},
path: '/sushi',
url: '/sushi',
})
expect(koMatch).toBe(null)
})
})
})