Skip to content

Commit 6bc746a

Browse files
committed
Tests for getting / restoring selections across iframes
1 parent b0c7a4b commit 6bc746a

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

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

+51-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ describe('ReactInputSelection', () => {
2222
var instance = ReactTestUtils.renderIntoDocument(element);
2323
return ReactDOM.findDOMNode(instance);
2424
};
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+
});
2531

2632
beforeEach(() => {
2733
jest.resetModuleRegistry();
@@ -108,7 +114,7 @@ describe('ReactInputSelection', () => {
108114
});
109115

110116
it('gets selection on inputs in iframes', () => {
111-
var iframe = document.createElement('iframe');
117+
const iframe = document.createElement('iframe');
112118
document.body.appendChild(iframe);
113119
const input = document.createElement('input');
114120
input.value = textValue;
@@ -135,7 +141,7 @@ describe('ReactInputSelection', () => {
135141
});
136142

137143
it('sets selection on inputs in iframes', () => {
138-
var iframe = document.createElement('iframe');
144+
const iframe = document.createElement('iframe');
139145
document.body.appendChild(iframe);
140146
const input = document.createElement('input');
141147
input.value = textValue;
@@ -150,6 +156,9 @@ describe('ReactInputSelection', () => {
150156

151157
describe('getSelectionInformation/restoreSelection', () => {
152158
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);
153162
var input = document.createElement('input');
154163
input.value = textValue;
155164
document.body.appendChild(input);
@@ -173,6 +182,46 @@ describe('ReactInputSelection', () => {
173182
expect(input.selectionEnd).toBe(10);
174183

175184
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;
176225
});
177226
});
178227
});

0 commit comments

Comments
 (0)