@@ -22,6 +22,12 @@ describe('ReactInputSelection', () => {
22
22
var instance = ReactTestUtils . renderIntoDocument ( element ) ;
23
23
return ReactDOM . findDOMNode ( instance ) ;
24
24
} ;
25
+ var makeGetSelection = ( win = window ) => ( ) => ( {
26
+ anchorNode : win . document . activeElement ,
27
+ focusNode : win . document . activeElement ,
28
+ anchorOffset : win . document . activeElement && win . document . activeElement . selectionStart ,
29
+ focusOffset : win . document . activeElement && win . document . activeElement . selectionEnd ,
30
+ } ) ;
25
31
26
32
beforeEach ( ( ) => {
27
33
jest . resetModuleRegistry ( ) ;
@@ -108,7 +114,7 @@ describe('ReactInputSelection', () => {
108
114
} ) ;
109
115
110
116
it ( 'gets selection on inputs in iframes' , ( ) => {
111
- var iframe = document . createElement ( 'iframe' ) ;
117
+ const iframe = document . createElement ( 'iframe' ) ;
112
118
document . body . appendChild ( iframe ) ;
113
119
const input = document . createElement ( 'input' ) ;
114
120
input . value = textValue ;
@@ -135,7 +141,7 @@ describe('ReactInputSelection', () => {
135
141
} ) ;
136
142
137
143
it ( 'sets selection on inputs in iframes' , ( ) => {
138
- var iframe = document . createElement ( 'iframe' ) ;
144
+ const iframe = document . createElement ( 'iframe' ) ;
139
145
document . body . appendChild ( iframe ) ;
140
146
const input = document . createElement ( 'input' ) ;
141
147
input . value = textValue ;
@@ -150,6 +156,9 @@ describe('ReactInputSelection', () => {
150
156
151
157
describe ( 'getSelectionInformation/restoreSelection' , ( ) => {
152
158
it ( 'gets and restores selection for inputs that get remounted' , ( ) => {
159
+ // Stub out window getSelection
160
+ window . getSelection =
161
+ window . getSelection || makeGetSelection ( window ) ;
153
162
var input = document . createElement ( 'input' ) ;
154
163
input . value = textValue ;
155
164
document . body . appendChild ( input ) ;
@@ -174,5 +183,45 @@ describe('ReactInputSelection', () => {
174
183
175
184
document . body . removeChild ( input ) ;
176
185
} ) ;
186
+
187
+ it ( 'gets and restores selection for inputs in an iframe that get remounted' , ( ) => {
188
+ var iframe = document . createElement ( 'iframe' ) ;
189
+ iframe . setAttribute ( 'tabIndex' , 0 ) ;
190
+ document . body . appendChild ( iframe ) ;
191
+ var iframeDoc = iframe . contentDocument ;
192
+ // Stub out window and iframe getSelection
193
+ window . getSelection =
194
+ window . getSelection || makeGetSelection ( window ) ;
195
+ iframeDoc . defaultView . getSelection =
196
+ iframeDoc . defaultView . getSelection || makeGetSelection ( iframeDoc . defaultView ) ;
197
+
198
+ iframe . focus ( ) ;
199
+ var input = document . createElement ( 'input' ) ;
200
+ input . value = textValue ;
201
+ iframeDoc . body . appendChild ( input ) ;
202
+ // Focus iframe first to get around jsdom limitations
203
+ iframe . focus ( ) ;
204
+ input . focus ( ) ;
205
+ input . selectionStart = 1 ;
206
+ input . selectionEnd = 10 ;
207
+ var selectionInfo = ReactInputSelection . getSelectionInformation ( ) ;
208
+ expect ( selectionInfo . focusedElement ) . toBe ( input ) ;
209
+ expect ( selectionInfo . activeElements [ 0 ] . selectionRange ) . toEqual ( { start : 1 , end : 10 } ) ;
210
+ expect ( document . activeElement ) . toBe ( iframe ) ;
211
+ expect ( iframeDoc . activeElement ) . toBe ( input ) ;
212
+
213
+ input . setSelectionRange ( 0 , 0 ) ;
214
+ iframeDoc . body . removeChild ( input ) ;
215
+ expect ( iframeDoc . activeElement ) . not . toBe ( input ) ;
216
+ expect ( input . selectionStart ) . not . toBe ( 1 ) ;
217
+ expect ( input . selectionEnd ) . not . toBe ( 10 ) ;
218
+ iframeDoc . body . appendChild ( input ) ;
219
+ ReactInputSelection . restoreSelection ( selectionInfo ) ;
220
+ expect ( iframeDoc . activeElement ) . toBe ( input ) ;
221
+ expect ( input . selectionStart ) . toBe ( 1 ) ;
222
+ expect ( input . selectionEnd ) . toBe ( 10 ) ;
223
+
224
+ document . body . removeChild ( iframe ) ;
225
+ } ) ;
177
226
} ) ;
178
227
} ) ;
0 commit comments