Skip to content

Commit 489866b

Browse files
committed
Tests for getting / restoring selections across iframes
1 parent 4ea5fb3 commit 489866b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/renderers/dom/client/__tests__/ReactInputSelection-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,39 @@ describe('ReactInputSelection', () => {
174174

175175
document.body.removeChild(input);
176176
});
177+
178+
it('gets and restores selection for inputs in an iframe that get remounted', () => {
179+
var iframe = document.createElement('iframe');
180+
iframe.setAttribute('tabIndex', 0);
181+
document.body.appendChild(iframe);
182+
iframe.focus();
183+
var iframeDoc = iframe.contentDocument;
184+
var input = document.createElement('input');
185+
input.value = textValue;
186+
iframeDoc.body.appendChild(input);
187+
// Focus iframe first to get around jsdom limitations
188+
iframe.focus();
189+
input.focus();
190+
input.selectionStart = 1;
191+
input.selectionEnd = 10;
192+
var selectionInfo = ReactInputSelection.getSelectionInformation();
193+
expect(selectionInfo.focusedElement).toBe(input);
194+
expect(selectionInfo.activeElements[0].selectionRange).toEqual({start: 1, end: 10});
195+
expect(document.activeElement).toBe(iframe);
196+
expect(iframeDoc.activeElement).toBe(input);
197+
198+
input.setSelectionRange(0, 0);
199+
iframeDoc.body.removeChild(input);
200+
expect(iframeDoc.activeElement).not.toBe(input);
201+
expect(input.selectionStart).not.toBe(1);
202+
expect(input.selectionEnd).not.toBe(10);
203+
iframeDoc.body.appendChild(input);
204+
ReactInputSelection.restoreSelection(selectionInfo);
205+
expect(iframeDoc.activeElement).toBe(input);
206+
expect(input.selectionStart).toBe(1);
207+
expect(input.selectionEnd).toBe(10);
208+
209+
document.body.removeChild(iframe);
210+
});
177211
});
178212
});

0 commit comments

Comments
 (0)