Skip to content

Commit

Permalink
fix(range): Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Sep 3, 2020
1 parent 0dcb97d commit bdee4d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/store/highlight/__mocks__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export const mockDOMRect: DOMRect = {
y: 200,
};

const mockTextNode = document.createTextNode('test');

export const mockRange: Range = ({
endContainer: mockTextNode,
getBoundingClientRect: () => mockDOMRect,
getClientRects: () => [mockDOMRect],
startContainer: mockTextNode,
} as unknown) as Range;
24 changes: 23 additions & 1 deletion src/store/highlight/__tests__/reducer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import reducer from '../reducer';
import state from '../__mocks__/highlightState';
import { Annotation, NewAnnotation } from '../../../@types';
import { createAnnotationAction } from '../../annotations';
import { mockContainerRect, mockRange } from '../__mocks__/data';
import { mockContainerRect, mockDOMRect, mockRange } from '../__mocks__/data';
import { resetCreatorAction } from '../../creator';
import { setIsPromotingAction, setSelectionAction } from '../actions';

Expand All @@ -27,6 +27,28 @@ describe('store/highlight/reducer', () => {

expect(newState.selection).toEqual({ ...state.selection, location: 2 });
});

test('should set selection in IE/Edge', () => {
jest.spyOn(document, 'createNodeIterator').mockReturnValueOnce(({
nextNode: jest.fn().mockReturnValueOnce(mockRange.startContainer),
} as unknown) as NodeIterator);
jest.spyOn(document, 'createRange').mockReturnValueOnce({
...new Range(),
getBoundingClientRect: jest.fn().mockReturnValueOnce(mockDOMRect),
selectNodeContents: jest.fn(),
setEnd: jest.fn(),
setStart: jest.fn(),
});
const range = {
...mockRange,
getClientRects: () => ({ length: 0 } as DOMRectList),
};

const payload = { containerRect: mockContainerRect, location: 1, range };
const newState = reducer(state, setSelectionAction(payload));

expect(newState.selection).toEqual(state.selection);
});
});

describe('createAnnotationAction', () => {
Expand Down

0 comments on commit bdee4d0

Please sign in to comment.