Skip to content

Commit

Permalink
Tests for getting / restoring selections across iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Oct 13, 2016
1 parent acc235c commit f6ee84e
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/renderers/dom/client/__tests__/ReactInputSelection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('ReactInputSelection', () => {
var instance = ReactTestUtils.renderIntoDocument(element);
return ReactDOM.findDOMNode(instance);
};
var makeGetSelection = (win = window) => () => ({
anchorNode: win.document.activeElement,
focusNode: win.document.activeElement,
anchorOffset: win.document.activeElement && win.document.activeElement.selectionStart,
focusOffset: win.document.activeElement && win.document.activeElement.selectionEnd,
});

beforeEach(() => {
jest.resetModuleRegistry();
Expand Down Expand Up @@ -108,7 +114,7 @@ describe('ReactInputSelection', () => {
});

it('gets selection on inputs in iframes', () => {
var iframe = document.createElement('iframe');
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const input = document.createElement('input');
input.value = textValue;
Expand All @@ -135,7 +141,7 @@ describe('ReactInputSelection', () => {
});

it('sets selection on inputs in iframes', () => {
var iframe = document.createElement('iframe');
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const input = document.createElement('input');
input.value = textValue;
Expand All @@ -150,6 +156,9 @@ describe('ReactInputSelection', () => {

describe('getSelectionInformation/restoreSelection', () => {
it('gets and restores selection for inputs that get remounted', () => {
// Stub out window getSelection
window.getSelection =
window.getSelection || makeGetSelection(window);
var input = document.createElement('input');
input.value = textValue;
document.body.appendChild(input);
Expand All @@ -174,5 +183,45 @@ describe('ReactInputSelection', () => {

document.body.removeChild(input);
});

it('gets and restores selection for inputs in an iframe that get remounted', () => {
var iframe = document.createElement('iframe');
iframe.setAttribute('tabIndex', 0);
document.body.appendChild(iframe);
var iframeDoc = iframe.contentDocument;
// Stub out window and iframe getSelection
window.getSelection =
window.getSelection || makeGetSelection(window);
iframeDoc.defaultView.getSelection =
iframeDoc.defaultView.getSelection || makeGetSelection(iframeDoc.defaultView);

iframe.focus();
var input = document.createElement('input');
input.value = textValue;
iframeDoc.body.appendChild(input);
// Focus iframe first to get around jsdom limitations
iframe.focus();
input.focus();
input.selectionStart = 1;
input.selectionEnd = 10;
var selectionInfo = ReactInputSelection.getSelectionInformation();
expect(selectionInfo.focusedElement).toBe(input);
expect(selectionInfo.activeElements[0].selectionRange).toEqual({start: 1, end: 10});
expect(document.activeElement).toBe(iframe);
expect(iframeDoc.activeElement).toBe(input);

input.setSelectionRange(0, 0);
iframeDoc.body.removeChild(input);
expect(iframeDoc.activeElement).not.toBe(input);
expect(input.selectionStart).not.toBe(1);
expect(input.selectionEnd).not.toBe(10);
iframeDoc.body.appendChild(input);
ReactInputSelection.restoreSelection(selectionInfo);
expect(iframeDoc.activeElement).toBe(input);
expect(input.selectionStart).toBe(1);
expect(input.selectionEnd).toBe(10);

document.body.removeChild(iframe);
});
});
});

0 comments on commit f6ee84e

Please sign in to comment.