1
1
import { TestBed } from '@angular/core/testing' ;
2
2
import { TranslateService , TranslateModule } from '@ngx-translate/core' ;
3
- import { GridOption } from '../../models/gridOption.interface ' ;
3
+ import { Column , GridOption } from '../../models' ;
4
4
import { RowMoveManagerExtension } from '../rowMoveManagerExtension' ;
5
5
import { ExtensionUtility } from '../extensionUtility' ;
6
6
import { SharedService } from '../../services/shared.service' ;
@@ -17,6 +17,7 @@ const gridStub = {
17
17
const mockAddon = jest . fn ( ) . mockImplementation ( ( ) => ( {
18
18
init : jest . fn ( ) ,
19
19
destroy : jest . fn ( ) ,
20
+ getColumnDefinition : jest . fn ( ) ,
20
21
onBeforeMoveRows : new Slick . Event ( ) ,
21
22
onMoveRows : new Slick . Event ( ) ,
22
23
} ) ) ;
@@ -38,6 +39,9 @@ describe('rowMoveManagerExtension', () => {
38
39
const gridOptionsMock = {
39
40
enableRowMoveManager : true ,
40
41
rowMoveManager : {
42
+ cancelEditOnDrag : true ,
43
+ singleRowMove : true ,
44
+ disableRowSelection : true ,
41
45
onExtensionRegistered : jest . fn ( ) ,
42
46
onBeforeMoveRows : ( e , args : { insertBefore : number ; rows : number [ ] ; } ) => { } ,
43
47
onMoveRows : ( e , args : { insertBefore : number ; rows : number [ ] ; } ) => { } ,
@@ -53,13 +57,29 @@ describe('rowMoveManagerExtension', () => {
53
57
translate = TestBed . get ( TranslateService ) ;
54
58
} ) ;
55
59
56
- it ( 'should return null when either the grid object or the grid options is missing' , ( ) => {
60
+ it ( 'should return null after calling "create" method when either the column definitions or the grid options is missing' , ( ) => {
61
+ const output = extension . create ( [ ] as Column [ ] , null ) ;
62
+ expect ( output ) . toBeNull ( ) ;
63
+ } ) ;
64
+
65
+ it ( 'should return null after calling "loadAddonWhenNotExists" method when either the column definitions or the grid options is missing' , ( ) => {
66
+ const output = extension . loadAddonWhenNotExists ( [ ] as Column [ ] , null ) ;
67
+ expect ( output ) . toBeNull ( ) ;
68
+ } ) ;
69
+
70
+ it ( 'should return null after calling "register" method when either the grid object or the grid options is missing' , ( ) => {
57
71
const output = extension . register ( ) ;
58
72
expect ( output ) . toBeNull ( ) ;
59
73
} ) ;
60
74
61
- describe ( 'registered addon' , ( ) => {
75
+ describe ( 'create method' , ( ) => {
76
+ let columnsMock : Column [ ] ;
77
+
62
78
beforeEach ( ( ) => {
79
+ columnsMock = [
80
+ { id : 'field1' , field : 'field1' , width : 100 , cssClass : 'red' } ,
81
+ { id : 'field2' , field : 'field2' , width : 50 }
82
+ ] ;
63
83
jest . spyOn ( SharedService . prototype , 'grid' , 'get' ) . mockReturnValue ( gridStub ) ;
64
84
jest . spyOn ( SharedService . prototype , 'gridOptions' , 'get' ) . mockReturnValue ( gridOptionsMock ) ;
65
85
} ) ;
@@ -68,16 +88,108 @@ describe('rowMoveManagerExtension', () => {
68
88
jest . clearAllMocks ( ) ;
69
89
} ) ;
70
90
91
+ it ( 'should add a reserved column for icons in 1st column index' , ( ) => {
92
+ const instance = extension . loadAddonWhenNotExists ( columnsMock , gridOptionsMock ) ;
93
+ const spy = jest . spyOn ( instance , 'getColumnDefinition' ) . mockReturnValue ( { id : '_move' , field : 'move' } ) ;
94
+ extension . create ( columnsMock , gridOptionsMock ) ;
95
+
96
+ expect ( spy ) . toHaveBeenCalled ( ) ;
97
+ expect ( columnsMock ) . toEqual ( [
98
+ {
99
+ excludeFromColumnPicker : true ,
100
+ excludeFromExport : true ,
101
+ excludeFromGridMenu : true ,
102
+ excludeFromHeaderMenu : true ,
103
+ excludeFromQuery : true ,
104
+ field : 'move' ,
105
+ id : '_move'
106
+ } ,
107
+ { id : 'field1' , field : 'field1' , width : 100 , cssClass : 'red' } ,
108
+ { id : 'field2' , field : 'field2' , width : 50 } ,
109
+ ] ) ;
110
+ } ) ;
111
+
112
+ it ( 'should NOT add the move icon column if it already exist in the column definitions' , ( ) => {
113
+ columnsMock = [ {
114
+ id : '_move' , name : '' , field : 'move' , width : 40 ,
115
+ behavior : 'selectAndMove' , selectable : false , resizable : false , cssClass : '' ,
116
+ formatter : ( row , cell , value , columnDef , dataContext , grid ) => ( { addClasses : 'cell-reorder dnd' } )
117
+ } , ...columnsMock ] as Column [ ] ;
118
+ const instance = extension . loadAddonWhenNotExists ( columnsMock , gridOptionsMock ) ;
119
+ const spy = jest . spyOn ( instance , 'getColumnDefinition' ) . mockReturnValue ( { id : '_move' , field : 'move' } ) ;
120
+ extension . create ( columnsMock , gridOptionsMock ) ;
121
+
122
+ expect ( spy ) . toHaveBeenCalled ( ) ;
123
+ expect ( columnsMock ) . toEqual ( [
124
+ {
125
+ behavior : 'selectAndMove' ,
126
+ cssClass : '' ,
127
+ field : 'move' ,
128
+ formatter : expect . anything ( ) ,
129
+ id : '_move' ,
130
+ name : '' ,
131
+ resizable : false ,
132
+ selectable : false ,
133
+ width : 40 ,
134
+ excludeFromColumnPicker : true ,
135
+ excludeFromExport : true ,
136
+ excludeFromGridMenu : true ,
137
+ excludeFromHeaderMenu : true ,
138
+ excludeFromQuery : true ,
139
+ } ,
140
+ { id : 'field1' , field : 'field1' , width : 100 , cssClass : 'red' } ,
141
+ { id : 'field2' , field : 'field2' , width : 50 } ,
142
+ ] ) ;
143
+ } ) ;
144
+
145
+ it ( 'should expect the column to be at a different column index position when "columnIndexPosition" is defined' , ( ) => {
146
+ gridOptionsMock . rowMoveManager . columnIndexPosition = 2 ;
147
+ const instance = extension . loadAddonWhenNotExists ( columnsMock , gridOptionsMock ) ;
148
+ const spy = jest . spyOn ( instance , 'getColumnDefinition' ) . mockReturnValue ( { id : '_move' , field : 'move' } ) ;
149
+ extension . create ( columnsMock , gridOptionsMock ) ;
150
+
151
+ expect ( spy ) . toHaveBeenCalled ( ) ;
152
+ expect ( columnsMock ) . toEqual ( [
153
+ { id : 'field1' , field : 'field1' , width : 100 , cssClass : 'red' } ,
154
+ { id : 'field2' , field : 'field2' , width : 50 } ,
155
+ {
156
+ excludeFromColumnPicker : true ,
157
+ excludeFromExport : true ,
158
+ excludeFromGridMenu : true ,
159
+ excludeFromHeaderMenu : true ,
160
+ excludeFromQuery : true ,
161
+ field : 'move' ,
162
+ id : '_move'
163
+ } ,
164
+ ] ) ;
165
+ } ) ;
166
+ } ) ;
167
+
168
+ describe ( 'registered addon' , ( ) => {
169
+ let columnsMock : Column [ ] ;
170
+
171
+ beforeEach ( ( ) => {
172
+ columnsMock = [ { id : 'field1' , field : 'field1' , width : 100 , cssClass : 'red' } ] ;
173
+ jest . spyOn ( SharedService . prototype , 'grid' , 'get' ) . mockReturnValue ( gridStub ) ;
174
+ jest . spyOn ( SharedService . prototype , 'gridOptions' , 'get' ) . mockReturnValue ( gridOptionsMock ) ;
175
+ jest . clearAllMocks ( ) ;
176
+ } ) ;
71
177
it ( 'should register the addon' , ( ) => {
72
178
const onRegisteredSpy = jest . spyOn ( SharedService . prototype . gridOptions . rowMoveManager , 'onExtensionRegistered' ) ;
73
179
const pluginSpy = jest . spyOn ( SharedService . prototype . grid , 'registerPlugin' ) ;
74
180
75
- const instance = extension . register ( ) ;
181
+ const instance = extension . loadAddonWhenNotExists ( columnsMock , gridOptionsMock ) ;
182
+ extension . create ( columnsMock , gridOptionsMock ) ;
183
+ extension . register ( ) ;
76
184
const addonInstance = extension . getAddonInstance ( ) ;
77
185
78
186
expect ( instance ) . toBeTruthy ( ) ;
79
187
expect ( instance ) . toEqual ( addonInstance ) ;
80
188
expect ( mockAddon ) . toHaveBeenCalledWith ( {
189
+ cancelEditOnDrag : true ,
190
+ disableRowSelection : true ,
191
+ singleRowMove : true ,
192
+ columnIndexPosition : 2 ,
81
193
onExtensionRegistered : expect . anything ( ) ,
82
194
onBeforeMoveRows : expect . anything ( ) ,
83
195
onMoveRows : expect . anything ( ) ,
@@ -87,7 +199,8 @@ describe('rowMoveManagerExtension', () => {
87
199
} ) ;
88
200
89
201
it ( 'should dispose of the addon' , ( ) => {
90
- const instance = extension . register ( ) ;
202
+ const instance = extension . create ( columnsMock , gridOptionsMock ) ;
203
+ extension . register ( ) ;
91
204
const destroySpy = jest . spyOn ( instance , 'destroy' ) ;
92
205
93
206
extension . dispose ( ) ;
@@ -96,21 +209,23 @@ describe('rowMoveManagerExtension', () => {
96
209
} ) ;
97
210
98
211
it ( 'should provide addon options and expect them to be called in the addon constructor' , ( ) => {
99
- const optionMock = { cancelEditOnDrag : true } ;
212
+ const optionMock = { cancelEditOnDrag : true , singleRowMove : true , disableRowSelection : true } ;
100
213
const addonOptions = { ...gridOptionsMock , rowMoveManager : optionMock } ;
101
214
jest . spyOn ( SharedService . prototype , 'gridOptions' , 'get' ) . mockReturnValue ( addonOptions ) ;
102
215
216
+ const instance = extension . create ( columnsMock , gridOptionsMock ) ;
103
217
extension . register ( ) ;
104
218
105
- expect ( mockAddon ) . toHaveBeenCalledWith ( optionMock ) ;
219
+ expect ( mockAddon ) . toHaveBeenCalledWith ( gridOptionsMock . rowMoveManager ) ;
106
220
} ) ;
107
221
108
222
it ( 'should call internal event handler subscribe and expect the "onBeforeMoveRows" option to be called when addon notify is called' , ( ) => {
109
223
const handlerSpy = jest . spyOn ( extension . eventHandler , 'subscribe' ) ;
110
224
const onBeforeSpy = jest . spyOn ( SharedService . prototype . gridOptions . rowMoveManager , 'onBeforeMoveRows' ) ;
111
225
const onMoveSpy = jest . spyOn ( SharedService . prototype . gridOptions . rowMoveManager , 'onMoveRows' ) ;
112
226
113
- const instance = extension . register ( ) ;
227
+ const instance = extension . create ( columnsMock , gridOptionsMock ) ;
228
+ extension . register ( ) ;
114
229
instance . onBeforeMoveRows . notify ( { insertBefore : 3 , rows : [ 1 ] } , new Slick . EventData ( ) , gridStub ) ;
115
230
116
231
expect ( handlerSpy ) . toHaveBeenCalledTimes ( 2 ) ;
@@ -127,7 +242,8 @@ describe('rowMoveManagerExtension', () => {
127
242
const onBeforeSpy = jest . spyOn ( SharedService . prototype . gridOptions . rowMoveManager , 'onBeforeMoveRows' ) ;
128
243
const onMoveSpy = jest . spyOn ( SharedService . prototype . gridOptions . rowMoveManager , 'onMoveRows' ) ;
129
244
130
- const instance = extension . register ( ) ;
245
+ const instance = extension . create ( columnsMock , gridOptionsMock ) ;
246
+ extension . register ( ) ;
131
247
instance . onMoveRows . notify ( { insertBefore : 3 , rows : [ 1 ] } , new Slick . EventData ( ) , gridStub ) ;
132
248
133
249
expect ( handlerSpy ) . toHaveBeenCalledTimes ( 2 ) ;
0 commit comments