Skip to content

Commit 403e4d6

Browse files
committed
Tests for getting / restoring selections across iframes
1 parent a8fd2db commit 403e4d6

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

Diff for: 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+
// Stub out window getSelection
160+
window.getSelection =
161+
window.getSelection || makeGetSelection(window);
153162
var input = document.createElement('input');
154163
input.value = textValue;
155164
document.body.appendChild(input);
@@ -174,5 +183,45 @@ describe('ReactInputSelection', () => {
174183

175184
document.body.removeChild(input);
176185
});
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+
});
177226
});
178227
});

0 commit comments

Comments
 (0)