Skip to content

Commit

Permalink
feat(region): Move test to actions-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Sep 4, 2020
1 parent b892e43 commit e1b5175
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
47 changes: 47 additions & 0 deletions src/store/highlight/__tests__/actions-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import state from '../__mocks__/highlightState';
import { mockContainerRect, mockDOMRect, mockRange } from '../__mocks__/data';
import { setSelectionAction } from '../actions';

describe('store/highlight/actions', () => {
describe('setSelectionAction', () => {
const arg = {
containerRect: mockContainerRect,
location: 1,
range: mockRange,
};

test('should prepare correct argument', () => {
expect(setSelectionAction(arg)).toEqual({
payload: state.selection,
type: 'SET_SELECTION',
});
});

test('should prepare correct argument 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 newArg = {
...arg,
range,
};

expect(setSelectionAction(newArg)).toEqual({
payload: state.selection,
type: 'SET_SELECTION',
});
});
});
});
24 changes: 1 addition & 23 deletions 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, mockDOMRect, mockRange } from '../__mocks__/data';
import { mockContainerRect, mockRange } from '../__mocks__/data';
import { resetCreatorAction } from '../../creator';
import { setIsPromotingAction, setSelectionAction } from '../actions';

Expand All @@ -27,28 +27,6 @@ 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 e1b5175

Please sign in to comment.