Skip to content

Commit

Permalink
Chore: Tests and updated comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Press committed Nov 28, 2018
1 parent 91922c1 commit bd3fa37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/doc/DocAnnotator.js
Expand Up @@ -535,7 +535,10 @@ class DocAnnotator extends Annotator {
onSelectionChange(event: Event) {
const selection = window.getSelection();
const selectionNode = selection.anchorNode;
// Check parent node in IE, which will return a text node instead of a div when accessing the anchorNode
// If the selection is not contained within the annotated element, don't do anything to avoid conflicting
// with default selection behavior outside of annotations.
// In IE 11, we need to check the parent of the selection, as the anchor node is a TextElement that cannot
// be found by it's parent divs.
if (
!this.annotatedElement.contains(selectionNode) ||
!this.annotatedElement.contains(selectionNode.parentNode)
Expand Down
24 changes: 23 additions & 1 deletion src/doc/__tests__/DocAnnotator-test.js
Expand Up @@ -802,18 +802,34 @@ describe('doc/DocAnnotator', () => {
stopPropagation: jest.fn()
};

annotator.annotatedElement = {
contains: jest.fn().mockReturnValue(true)
};

docUtil.isValidSelection = jest.fn().mockReturnValue(true);
annotator.lastSelection = {};

annotator.highlighter = { removeAllHighlights: jest.fn() };
annotator.resetHighlightSelection = jest.fn();

window.getSelection = jest.fn();
window.getSelection = jest.fn().mockReturnValue({
anchorNode: {
parentNode: 'Node'
}
});
util.getPageInfo = jest.fn().mockReturnValue({ page: 1 });
util.findClosestElWithClass = jest.fn().mockReturnValue(null);
annotator.isCreatingHighlight = false;
});

it('should do nothing if the selection is not contained in the annotatedElement', () => {
annotator.annotatedElement.contains.mockReturnValue(false);
annotator.onSelectionChange(event);

expect(event.preventDefault).not.toBeCalled();
expect(event.stopPropagation).not.toBeCalled();
});

it('should reset the selectionEndTimeout', () => {
annotator.selectionEndTimeout = 1;
docUtil.isValidSelection = jest.fn().mockReturnValue(false);
Expand Down Expand Up @@ -862,6 +878,9 @@ describe('doc/DocAnnotator', () => {
const selection = {
rangeCount: 10,
isCollapsed: false,
anchorNode: {
parentNode: 'Node'
},
toString: () => 'asdf'
};
window.getSelection = jest.fn().mockReturnValue(selection);
Expand All @@ -878,6 +897,9 @@ describe('doc/DocAnnotator', () => {
const selection = {
rangeCount: 10,
isCollapsed: false,
anchorNode: {
parentNode: 'Node'
},
toString: jest.fn().mockReturnValue('asdf')
};
annotator.modeControllers = {
Expand Down

0 comments on commit bd3fa37

Please sign in to comment.