@@ -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
+ // Mock window getSelection if needed
160
+ var originalGetSelection = window . getSelection ;
161
+ window . getSelection = window . getSelection || makeGetSelection ( window ) ;
153
162
var input = document . createElement ( 'input' ) ;
154
163
input . value = textValue ;
155
164
document . body . appendChild ( input ) ;
@@ -173,6 +182,46 @@ describe('ReactInputSelection', () => {
173
182
expect ( input . selectionEnd ) . toBe ( 10 ) ;
174
183
175
184
document . body . removeChild ( input ) ;
185
+ window . getSelection = originalGetSelection ;
186
+ } ) ;
187
+
188
+ it ( 'gets and restores selection for inputs in an iframe that get remounted' , ( ) => {
189
+ var iframe = document . createElement ( 'iframe' ) ;
190
+ document . body . appendChild ( iframe ) ;
191
+ var iframeDoc = iframe . contentDocument ;
192
+ var iframeWin = iframeDoc . defaultView ;
193
+ // Mock window and iframe getSelection if needed
194
+ var originalGetSelection = window . getSelection ;
195
+ var originalIframeGetSelection = iframeWin . getSelection ;
196
+ window . getSelection = window . getSelection || makeGetSelection ( window ) ;
197
+ iframeWin . getSelection = iframeWin . getSelection || makeGetSelection ( iframeWin ) ;
198
+
199
+ var input = document . createElement ( 'input' ) ;
200
+ input . value = textValue ;
201
+ iframeDoc . body . appendChild ( input ) ;
202
+ input . focus ( ) ;
203
+ input . selectionStart = 1 ;
204
+ input . selectionEnd = 10 ;
205
+ var selectionInfo = ReactInputSelection . getSelectionInformation ( ) ;
206
+ expect ( selectionInfo . focusedElement ) . toBe ( input ) ;
207
+ expect ( selectionInfo . activeElements [ 0 ] . selectionRange ) . toEqual ( { start : 1 , end : 10 } ) ;
208
+ expect ( document . activeElement ) . toBe ( iframe ) ;
209
+ expect ( iframeDoc . activeElement ) . toBe ( input ) ;
210
+
211
+ input . setSelectionRange ( 0 , 0 ) ;
212
+ iframeDoc . body . removeChild ( input ) ;
213
+ expect ( iframeDoc . activeElement ) . not . toBe ( input ) ;
214
+ expect ( input . selectionStart ) . not . toBe ( 1 ) ;
215
+ expect ( input . selectionEnd ) . not . toBe ( 10 ) ;
216
+ iframeDoc . body . appendChild ( input ) ;
217
+ ReactInputSelection . restoreSelection ( selectionInfo ) ;
218
+ expect ( iframeDoc . activeElement ) . toBe ( input ) ;
219
+ expect ( input . selectionStart ) . toBe ( 1 ) ;
220
+ expect ( input . selectionEnd ) . toBe ( 10 ) ;
221
+
222
+ document . body . removeChild ( iframe ) ;
223
+ window . getSelection = originalGetSelection ;
224
+ iframeWin . getSelection = originalIframeGetSelection ;
176
225
} ) ;
177
226
} ) ;
178
227
} ) ;
0 commit comments