11import * as React from 'react' ;
2- import { shallow } from 'enzyme ' ;
2+ import { render } from '../../../../test-utils/testing-library ' ;
33import withAnnotatorContext , { WithAnnotatorContextProps } from '../withAnnotatorContext' ;
44import { Action } from '../types' ;
55
@@ -13,18 +13,59 @@ describe('elements/common/annotator-context/withAnnotatorContext', () => {
1313 className ?: string | undefined ;
1414 } ;
1515
16- // eslint-disable-next-line @typescript-eslint/no-unused-vars
17- type WrappedComponentProps < T > = ComponentProps & WithAnnotatorContextProps ; // T is supposed to be allowed component props
16+ type WrappedComponentProps = ComponentProps & WithAnnotatorContextProps ;
1817
19- const Component = ( props : WrappedComponentProps < HTMLDivElement > ) => < div { ... props } /> ;
18+ beforeEach ( ( ) => jest . resetAllMocks ( ) ) ;
2019
21- const WrappedComponent = withAnnotatorContext ( Component ) ;
20+ test ( 'should apply the annotator context to the wrapped component as a prop' , ( ) => {
21+ const annotatorState = {
22+ annotation : { foo : 'bar' } ,
23+ action : Action . CREATE_START ,
24+ } ;
25+ const mockEmitActiveAnnotationChangeEvent = jest . fn ( ) ;
26+ const mockEmitAnnotationRemoveEvent = jest . fn ( ) ;
27+ const mockEmitAnnotationReplyCreateEvent = jest . fn ( ) ;
28+ const mockEmitAnnotationReplyDeleteEvent = jest . fn ( ) ;
29+ const mockMmitAnnotationReplyUpdateEvent = jest . fn ( ) ;
30+ const mockEmitAnnotationUpdateEvent = jest . fn ( ) ;
31+ const mockGetAnnotationsMatchPath = jest . fn ( ) ;
32+ const mockGetAnnotationsPath = jest . fn ( ) ;
2233
23- const getWrapper = ( props ?: WrappedComponentProps < HTMLDivElement > ) => shallow ( < WrappedComponent { ...props } /> ) ;
34+ mockContext . mockReturnValue ( {
35+ state : annotatorState ,
36+ emitActiveAnnotationChangeEvent : mockEmitActiveAnnotationChangeEvent ,
37+ emitAnnotationRemoveEvent : mockEmitAnnotationRemoveEvent ,
38+ emitAnnotationReplyCreateEvent : mockEmitAnnotationReplyCreateEvent ,
39+ emitAnnotationReplyDeleteEvent : mockEmitAnnotationReplyDeleteEvent ,
40+ emitAnnotationReplyUpdateEvent : mockMmitAnnotationReplyUpdateEvent ,
41+ emitAnnotationUpdateEvent : mockEmitAnnotationUpdateEvent ,
42+ getAnnotationsMatchPath : mockGetAnnotationsMatchPath ,
43+ getAnnotationsPath : mockGetAnnotationsPath ,
44+ } ) ;
2445
25- beforeEach ( ( ) => jest . resetAllMocks ( ) ) ;
46+ const MockComponent = jest . fn < JSX . Element | null , [ WrappedComponentProps ] > ( ( ) => null ) ;
47+ const WrappedWithMockComponent = withAnnotatorContext ( MockComponent ) ;
2648
27- test ( 'should apply the annotator context to the wrapped component as a prop' , ( ) => {
49+ render ( < WrappedWithMockComponent /> ) ;
50+
51+ expect ( MockComponent ) . toHaveBeenCalledTimes ( 1 ) ;
52+ const props = MockComponent . mock . calls [ 0 ] [ 0 ] ;
53+
54+ expect ( props . annotatorState ) . toEqual ( {
55+ annotation : { foo : 'bar' } ,
56+ action : Action . CREATE_START ,
57+ } ) ;
58+ expect ( props . emitActiveAnnotationChangeEvent ) . toBe ( mockEmitActiveAnnotationChangeEvent ) ;
59+ expect ( props . emitAnnotationRemoveEvent ) . toBe ( mockEmitAnnotationRemoveEvent ) ;
60+ expect ( props . emitAnnotationReplyCreateEvent ) . toBe ( mockEmitAnnotationReplyCreateEvent ) ;
61+ expect ( props . emitAnnotationReplyDeleteEvent ) . toBe ( mockEmitAnnotationReplyDeleteEvent ) ;
62+ expect ( props . emitAnnotationReplyUpdateEvent ) . toBe ( mockMmitAnnotationReplyUpdateEvent ) ;
63+ expect ( props . emitAnnotationUpdateEvent ) . toBe ( mockEmitAnnotationUpdateEvent ) ;
64+ expect ( props . getAnnotationsMatchPath ) . toBe ( mockGetAnnotationsMatchPath ) ;
65+ expect ( props . getAnnotationsPath ) . toBe ( mockGetAnnotationsPath ) ;
66+ } ) ;
67+
68+ test ( 'should apply the annotator context to the wrapped component without router props when routerDisabled is true' , ( ) => {
2869 const annotatorState = {
2970 annotation : { foo : 'bar' } ,
3071 action : Action . CREATE_START ,
@@ -50,22 +91,77 @@ describe('elements/common/annotator-context/withAnnotatorContext', () => {
5091 getAnnotationsPath : mockGetAnnotationsPath ,
5192 } ) ;
5293
53- const wrapper = getWrapper ( ) ;
54- const wrappedComponent = wrapper . dive ( ) . find ( Component ) ;
55- const props = wrappedComponent . props ( ) as WrappedComponentProps < HTMLDivElement > ;
94+ const MockComponent = jest . fn < JSX . Element | null , [ WrappedComponentProps ] > ( ( ) => null ) ;
95+ const WrappedWithMockComponent = withAnnotatorContext ( MockComponent ) ;
96+
97+ render ( < WrappedWithMockComponent routerDisabled = { true } /> ) ;
98+
99+ expect ( MockComponent ) . toHaveBeenCalledTimes ( 1 ) ;
100+ const props = MockComponent . mock . calls [ 0 ] [ 0 ] ;
56101
57- expect ( wrappedComponent . exists ( ) ) . toBeTruthy ( ) ;
58102 expect ( props . annotatorState ) . toEqual ( {
59103 annotation : { foo : 'bar' } ,
60104 action : Action . CREATE_START ,
61105 } ) ;
62- expect ( props . emitActiveAnnotationChangeEvent ) . toEqual ( mockEmitActiveAnnotationChangeEvent ) ;
63- expect ( props . emitAnnotationRemoveEvent ) . toEqual ( mockEmitAnnotationRemoveEvent ) ;
64- expect ( props . emitAnnotationReplyCreateEvent ) . toEqual ( mockEmitAnnotationReplyCreateEvent ) ;
65- expect ( props . emitAnnotationReplyDeleteEvent ) . toEqual ( mockEmitAnnotationReplyDeleteEvent ) ;
66- expect ( props . emitAnnotationReplyUpdateEvent ) . toEqual ( mockMmitAnnotationReplyUpdateEvent ) ;
67- expect ( props . emitAnnotationUpdateEvent ) . toEqual ( mockEmitAnnotationUpdateEvent ) ;
68- expect ( props . getAnnotationsMatchPath ) . toEqual ( mockGetAnnotationsMatchPath ) ;
69- expect ( props . getAnnotationsPath ) . toEqual ( mockGetAnnotationsPath ) ;
106+ expect ( props . emitActiveAnnotationChangeEvent ) . toBe ( mockEmitActiveAnnotationChangeEvent ) ;
107+ expect ( props . emitAnnotationRemoveEvent ) . toBe ( mockEmitAnnotationRemoveEvent ) ;
108+ expect ( props . emitAnnotationReplyCreateEvent ) . toBe ( mockEmitAnnotationReplyCreateEvent ) ;
109+ expect ( props . emitAnnotationReplyDeleteEvent ) . toBe ( mockEmitAnnotationReplyDeleteEvent ) ;
110+ expect ( props . emitAnnotationReplyUpdateEvent ) . toBe ( mockMmitAnnotationReplyUpdateEvent ) ;
111+ expect ( props . emitAnnotationUpdateEvent ) . toBe ( mockEmitAnnotationUpdateEvent ) ;
112+
113+ // Router-related props should not be passed when routerDisabled is true
114+ expect ( props . getAnnotationsMatchPath ) . toBeUndefined ( ) ;
115+ expect ( props . getAnnotationsPath ) . toBeUndefined ( ) ;
116+ } ) ;
117+
118+ test ( 'should apply the annotator context to the wrapped component without router props when routerDisabled in feature flags is true' , ( ) => {
119+ const annotatorState = {
120+ annotation : { foo : 'bar' } ,
121+ action : Action . CREATE_START ,
122+ } ;
123+ const mockEmitActiveAnnotationChangeEvent = jest . fn ( ) ;
124+ const mockEmitAnnotationRemoveEvent = jest . fn ( ) ;
125+ const mockEmitAnnotationReplyCreateEvent = jest . fn ( ) ;
126+ const mockEmitAnnotationReplyDeleteEvent = jest . fn ( ) ;
127+ const mockMmitAnnotationReplyUpdateEvent = jest . fn ( ) ;
128+ const mockEmitAnnotationUpdateEvent = jest . fn ( ) ;
129+ const mockGetAnnotationsMatchPath = jest . fn ( ) ;
130+ const mockGetAnnotationsPath = jest . fn ( ) ;
131+
132+ mockContext . mockReturnValue ( {
133+ state : annotatorState ,
134+ emitActiveAnnotationChangeEvent : mockEmitActiveAnnotationChangeEvent ,
135+ emitAnnotationRemoveEvent : mockEmitAnnotationRemoveEvent ,
136+ emitAnnotationReplyCreateEvent : mockEmitAnnotationReplyCreateEvent ,
137+ emitAnnotationReplyDeleteEvent : mockEmitAnnotationReplyDeleteEvent ,
138+ emitAnnotationReplyUpdateEvent : mockMmitAnnotationReplyUpdateEvent ,
139+ emitAnnotationUpdateEvent : mockEmitAnnotationUpdateEvent ,
140+ getAnnotationsMatchPath : mockGetAnnotationsMatchPath ,
141+ getAnnotationsPath : mockGetAnnotationsPath ,
142+ } ) ;
143+
144+ const MockComponent = jest . fn < JSX . Element | null , [ WrappedComponentProps ] > ( ( ) => null ) ;
145+ const WrappedWithMockComponent = withAnnotatorContext ( MockComponent ) ;
146+
147+ render ( < WrappedWithMockComponent features = { { routerDisabled : { value : true } } } /> ) ;
148+
149+ expect ( MockComponent ) . toHaveBeenCalledTimes ( 1 ) ;
150+ const props = MockComponent . mock . calls [ 0 ] [ 0 ] ;
151+
152+ expect ( props . annotatorState ) . toEqual ( {
153+ annotation : { foo : 'bar' } ,
154+ action : Action . CREATE_START ,
155+ } ) ;
156+ expect ( props . emitActiveAnnotationChangeEvent ) . toBe ( mockEmitActiveAnnotationChangeEvent ) ;
157+ expect ( props . emitAnnotationRemoveEvent ) . toBe ( mockEmitAnnotationRemoveEvent ) ;
158+ expect ( props . emitAnnotationReplyCreateEvent ) . toBe ( mockEmitAnnotationReplyCreateEvent ) ;
159+ expect ( props . emitAnnotationReplyDeleteEvent ) . toBe ( mockEmitAnnotationReplyDeleteEvent ) ;
160+ expect ( props . emitAnnotationReplyUpdateEvent ) . toBe ( mockMmitAnnotationReplyUpdateEvent ) ;
161+ expect ( props . emitAnnotationUpdateEvent ) . toBe ( mockEmitAnnotationUpdateEvent ) ;
162+
163+ // Router-related props should not be passed when routerDisabled is true
164+ expect ( props . getAnnotationsMatchPath ) . toBeUndefined ( ) ;
165+ expect ( props . getAnnotationsPath ) . toBeUndefined ( ) ;
70166 } ) ;
71167} ) ;
0 commit comments