From e1b5175f08f15e2e07460a862e112dd4c6107e85 Mon Sep 17 00:00:00 2001 From: Mingze Xiao Date: Fri, 4 Sep 2020 10:42:09 -0700 Subject: [PATCH] feat(region): Move test to actions-test --- src/store/highlight/__tests__/actions-test.ts | 47 +++++++++++++++++++ src/store/highlight/__tests__/reducer-test.ts | 24 +--------- 2 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 src/store/highlight/__tests__/actions-test.ts diff --git a/src/store/highlight/__tests__/actions-test.ts b/src/store/highlight/__tests__/actions-test.ts new file mode 100644 index 000000000..8ad1467d9 --- /dev/null +++ b/src/store/highlight/__tests__/actions-test.ts @@ -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', + }); + }); + }); +}); diff --git a/src/store/highlight/__tests__/reducer-test.ts b/src/store/highlight/__tests__/reducer-test.ts index d93fabe5d..899729dcc 100644 --- a/src/store/highlight/__tests__/reducer-test.ts +++ b/src/store/highlight/__tests__/reducer-test.ts @@ -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'; @@ -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', () => {