@@ -9,15 +9,19 @@ import renderStrict from "./utils/renderStrict.js";
9
9
describe ( "<Link> click events" , ( ) => {
10
10
const node = document . createElement ( "div" ) ;
11
11
12
- afterEach ( ( ) => {
13
- ReactDOM . unmountComponentAtNode ( node ) ;
12
+ let memoryHistory , pushSpy , replaceSpy ;
13
+
14
+ beforeEach ( ( ) => {
15
+ memoryHistory = createMemoryHistory ( ) ;
16
+ pushSpy = jest . spyOn ( memoryHistory , "push" ) ;
17
+ replaceSpy = jest . spyOn ( memoryHistory , "push" ) ;
14
18
} ) ;
15
19
16
- const memoryHistory = createMemoryHistory ( ) ;
17
- memoryHistory . push = jest . fn ( ) ;
20
+ afterEach ( ( ) => {
21
+ ReactDOM . unmountComponentAtNode ( node ) ;
18
22
19
- beforeEach ( ( ) => {
20
- memoryHistory . push . mockReset ( ) ;
23
+ pushSpy . mockRestore ( ) ;
24
+ replaceSpy . mockRestore ( ) ;
21
25
} ) ;
22
26
23
27
it ( "calls onClick eventhandler and history.push" , ( ) => {
@@ -40,15 +44,45 @@ describe("<Link> click events", () => {
40
44
} ) ;
41
45
42
46
expect ( clickHandler ) . toBeCalledTimes ( 1 ) ;
43
- expect ( memoryHistory . push ) . toBeCalledTimes ( 1 ) ;
44
- expect ( memoryHistory . push ) . toBeCalledWith ( to ) ;
47
+ expect ( pushSpy ) . toBeCalledTimes ( 1 ) ;
48
+ expect ( pushSpy ) . toBeCalledWith ( to ) ;
45
49
} ) ;
46
50
47
- it ( "calls onClick eventhandler and history.push with function `to` prop" , ( ) => {
48
- const memoryHistoryFoo = createMemoryHistory ( {
49
- initialEntries : [ "/foo" ]
51
+ it ( "calls history.replace on duplicate navigation" , ( ) => {
52
+ const clickHandler = jest . fn ( ) ;
53
+ const to = "/duplicate/path?the=query#the-hash" ;
54
+
55
+ renderStrict (
56
+ < Router history = { memoryHistory } >
57
+ < Link to = { to } onClick = { clickHandler } >
58
+ link
59
+ </ Link >
60
+ </ Router > ,
61
+ node
62
+ ) ;
63
+
64
+ const a = node . querySelector ( "a" ) ;
65
+ TestUtils . Simulate . click ( a , {
66
+ defaultPrevented : false ,
67
+ button : 0
50
68
} ) ;
51
- memoryHistoryFoo . push = jest . fn ( ) ;
69
+
70
+ TestUtils . Simulate . click ( a , {
71
+ defaultPrevented : false ,
72
+ button : 0
73
+ } ) ;
74
+
75
+ expect ( clickHandler ) . toBeCalledTimes ( 2 ) ;
76
+ expect ( pushSpy ) . toBeCalledTimes ( 1 ) ;
77
+ expect ( pushSpy ) . toBeCalledWith ( to ) ;
78
+ expect ( replaceSpy ) . toBeCalledTimes ( 1 ) ;
79
+ expect ( replaceSpy ) . toBeCalledWith ( to ) ;
80
+ } ) ;
81
+
82
+ it ( "calls onClick eventhandler and history.push with function `to` prop" , ( ) => {
83
+ // Make push a no-op so key IDs do not change
84
+ pushSpy . mockImplementation ( ) ;
85
+
52
86
const clickHandler = jest . fn ( ) ;
53
87
let to = null ;
54
88
const toFn = location => {
@@ -61,7 +95,7 @@ describe("<Link> click events", () => {
61
95
} ;
62
96
63
97
renderStrict (
64
- < Router history = { memoryHistoryFoo } >
98
+ < Router history = { memoryHistory } >
65
99
< Link to = { toFn } onClick = { clickHandler } >
66
100
link
67
101
</ Link >
@@ -76,8 +110,8 @@ describe("<Link> click events", () => {
76
110
} ) ;
77
111
78
112
expect ( clickHandler ) . toBeCalledTimes ( 1 ) ;
79
- expect ( memoryHistoryFoo . push ) . toBeCalledTimes ( 1 ) ;
80
- expect ( memoryHistoryFoo . push ) . toBeCalledWith ( to ) ;
113
+ expect ( pushSpy ) . toBeCalledTimes ( 1 ) ;
114
+ expect ( pushSpy ) . toBeCalledWith ( to ) ;
81
115
} ) ;
82
116
83
117
it ( "does not call history.push on right click" , ( ) => {
@@ -96,7 +130,7 @@ describe("<Link> click events", () => {
96
130
button : 1
97
131
} ) ;
98
132
99
- expect ( memoryHistory . push ) . toBeCalledTimes ( 0 ) ;
133
+ expect ( pushSpy ) . toBeCalledTimes ( 0 ) ;
100
134
} ) ;
101
135
102
136
it ( "does not call history.push on prevented event." , ( ) => {
@@ -115,7 +149,7 @@ describe("<Link> click events", () => {
115
149
button : 0
116
150
} ) ;
117
151
118
- expect ( memoryHistory . push ) . toBeCalledTimes ( 0 ) ;
152
+ expect ( pushSpy ) . toBeCalledTimes ( 0 ) ;
119
153
} ) ;
120
154
121
155
it ( "does not call history.push target not specifying 'self'" , ( ) => {
@@ -136,12 +170,10 @@ describe("<Link> click events", () => {
136
170
button : 0
137
171
} ) ;
138
172
139
- expect ( memoryHistory . push ) . toBeCalledTimes ( 0 ) ;
173
+ expect ( pushSpy ) . toBeCalledTimes ( 0 ) ;
140
174
} ) ;
141
175
142
176
it ( "prevents the default event handler if an error occurs" , ( ) => {
143
- const memoryHistory = createMemoryHistory ( ) ;
144
- memoryHistory . push = jest . fn ( ) ;
145
177
const error = new Error ( ) ;
146
178
const clickHandler = ( ) => {
147
179
throw error ;
@@ -173,6 +205,6 @@ describe("<Link> click events", () => {
173
205
console . error . mockRestore ( ) ;
174
206
expect ( clickHandler ) . toThrow ( error ) ;
175
207
expect ( mockPreventDefault ) . toHaveBeenCalled ( ) ;
176
- expect ( memoryHistory . push ) . toBeCalledTimes ( 0 ) ;
208
+ expect ( pushSpy ) . toBeCalledTimes ( 0 ) ;
177
209
} ) ;
178
210
} ) ;
0 commit comments