@@ -64,7 +64,8 @@ describe('Grid Service', () => {
64
64
let service : GridService ;
65
65
let sharedService = new SharedService ( ) ;
66
66
let translate : TranslateService ;
67
- jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { enableAutoResize : true } as GridOption ) ;
67
+ const mockGridOptions = { enableAutoResize : true } as GridOption ;
68
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( mockGridOptions ) ;
68
69
69
70
beforeEach ( ( ) => {
70
71
TestBed . configureTestingModule ( {
@@ -79,7 +80,6 @@ describe('Grid Service', () => {
79
80
imports : [ TranslateModule . forRoot ( ) ]
80
81
} ) ;
81
82
translate = TestBed . get ( TranslateService ) ;
82
- sharedService = TestBed . get ( SharedService ) ;
83
83
service = TestBed . get ( GridService ) ;
84
84
service . init ( gridStub , dataviewStub ) ;
85
85
} ) ;
@@ -125,6 +125,32 @@ describe('Grid Service', () => {
125
125
expect ( ( ) => service . upsertItem ( null ) ) . toThrowError ( 'Calling Upsert of an item requires the item to include an "id" property' ) ;
126
126
} ) ;
127
127
128
+ it ( 'should throw an error when 1st argument for the item object is missing the Id defined by the "datasetIdPropertyName" property' , ( ) => {
129
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { enableAutoResize : true , datasetIdPropertyName : 'customId' } as GridOption ) ;
130
+ expect ( ( ) => service . upsertItem ( null ) ) . toThrowError ( 'Calling Upsert of an item requires the item to include an "customId" property' ) ;
131
+
132
+ // reset mock
133
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { } ) ;
134
+ } ) ;
135
+
136
+ it ( 'should expect the service to call the DataView "insertItem" when calling "addItem" with an item that has an Id defined by the "datasetIdPropertyName" property' , ( ) => {
137
+ mockGridOptions . datasetIdPropertyName = 'customId' ;
138
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( mockGridOptions ) ;
139
+ const mockItem = { customId : 0 , user : { firstName : 'John' , lastName : 'Doe' } } ;
140
+ const dataviewSpy = jest . spyOn ( dataviewStub , 'getRowById' ) . mockReturnValue ( undefined ) ;
141
+ const serviceSpy = jest . spyOn ( service , 'addItem' ) ;
142
+ const rxSpy = jest . spyOn ( service . onItemUpserted , 'next' ) ;
143
+
144
+ const upsertRow = service . upsertItem ( mockItem ) ;
145
+
146
+ expect ( upsertRow ) . toEqual ( { added : 0 , updated : undefined } ) ;
147
+ expect ( serviceSpy ) . toHaveBeenCalledTimes ( 1 ) ;
148
+ expect ( dataviewSpy ) . toHaveBeenCalledWith ( 0 ) ;
149
+ expect ( serviceSpy ) . toHaveBeenCalledWith ( mockItem , { highlightRow : true , position : 'top' , resortGrid : false , selectRow : false , triggerEvent : true } ) ;
150
+ expect ( rxSpy ) . toHaveBeenCalledWith ( mockItem ) ;
151
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { } ) ;
152
+ } ) ;
153
+
128
154
it ( 'should expect the service to call the "addItem" when calling "upsertItem" with the item not being found in the grid' , ( ) => {
129
155
const mockItem = { id : 0 , user : { firstName : 'John' , lastName : 'Doe' } } ;
130
156
const dataviewSpy = jest . spyOn ( dataviewStub , 'getRowById' ) . mockReturnValue ( undefined ) ;
@@ -303,6 +329,10 @@ describe('Grid Service', () => {
303
329
} ) ;
304
330
305
331
describe ( 'updateItem methods' , ( ) => {
332
+ beforeEach ( ( ) => {
333
+ jest . clearAllMocks ( ) ;
334
+ } ) ;
335
+
306
336
it ( 'should throw an error when 1st argument for the item object is missing' , ( ) => {
307
337
expect ( ( ) => service . updateItem ( null ) ) . toThrowError ( 'Calling Update of an item requires the item to include an "id" property' ) ;
308
338
} ) ;
@@ -412,6 +442,25 @@ describe('Grid Service', () => {
412
442
expect ( rxSpy ) . toHaveBeenCalledWith ( mockItem ) ;
413
443
} ) ;
414
444
445
+ it ( 'should expect the service to call the DataView "insertItem" when calling "addItem" with an item that has an Id defined by the "datasetIdPropertyName" property' , ( ) => {
446
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { ...mockGridOptions , datasetIdPropertyName : 'customId' } ) ;
447
+ const mockItem = { customId : 0 , user : { firstName : 'John' , lastName : 'Doe' } } ;
448
+ const getRowIdSpy = jest . spyOn ( dataviewStub , 'getRowById' ) . mockReturnValue ( mockItem . customId ) ;
449
+ const getRowIndexSpy = jest . spyOn ( dataviewStub , 'getIdxById' ) . mockReturnValue ( mockItem . customId ) ;
450
+ const updateSpy = jest . spyOn ( service , 'updateItemById' ) ;
451
+ const rxSpy = jest . spyOn ( service . onItemUpdated , 'next' ) ;
452
+
453
+ service . updateItem ( mockItem ) ;
454
+
455
+ expect ( updateSpy ) . toHaveBeenCalledTimes ( 1 ) ;
456
+ expect ( getRowIdSpy ) . toHaveBeenCalledWith ( 0 ) ;
457
+ expect ( getRowIndexSpy ) . toHaveBeenCalledWith ( 0 ) ;
458
+ expect ( updateSpy ) . toHaveBeenCalledWith ( mockItem . customId , mockItem , { highlightRow : true , selectRow : false , scrollRowIntoView : false , triggerEvent : true } ) ;
459
+ expect ( rxSpy ) . toHaveBeenCalledWith ( mockItem ) ;
460
+ delete mockGridOptions . datasetIdPropertyName ;
461
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( mockGridOptions ) ;
462
+ } ) ;
463
+
415
464
it ( 'should throw an error when calling "updateItemById" without a valid "id"' , ( ) => {
416
465
const mockItem = { id : 0 , user : { firstName : 'John' , lastName : 'Doe' } } ;
417
466
expect ( ( ) => service . updateItemById ( undefined , mockItem ) ) . toThrowError ( 'Cannot update a row without a valid "id"' ) ;
@@ -422,6 +471,14 @@ describe('Grid Service', () => {
422
471
jest . spyOn ( dataviewStub , 'getRowById' ) . mockReturnValue ( undefined ) ;
423
472
expect ( ( ) => service . updateItemById ( 5 , mockItem ) ) . toThrowError ( 'The item to update in the grid was not found with id: 5' ) ;
424
473
} ) ;
474
+
475
+ it ( 'should throw an error when 1st argument for the item object is missing the Id defined by the "datasetIdPropertyName" property' , ( ) => {
476
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { enableAutoResize : true , datasetIdPropertyName : 'customId' } as GridOption ) ;
477
+ expect ( ( ) => service . updateItem ( null ) ) . toThrowError ( 'Calling Update of an item requires the item to include an "customId" property' ) ;
478
+
479
+ // reset mock
480
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { } ) ;
481
+ } ) ;
425
482
} ) ;
426
483
427
484
describe ( 'addItem methods' , ( ) => {
@@ -603,6 +660,37 @@ describe('Grid Service', () => {
603
660
expect ( selectSpy ) . toHaveBeenCalledWith ( 0 ) ;
604
661
expect ( rxSpy ) . toHaveBeenCalledWith ( mockItem ) ;
605
662
} ) ;
663
+
664
+ it ( 'should expect the service to call the DataView "insertItem" when calling "addItem" with an item that has an Id defined by the "datasetIdPropertyName" property' , ( ) => {
665
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { ...mockGridOptions , datasetIdPropertyName : 'customId' } ) ;
666
+ const mockItem = { customId : 0 , user : { firstName : 'John' , lastName : 'Doe' } } ;
667
+
668
+ // datasetIdPropertyName: 'customId'
669
+ const addSpy = jest . spyOn ( dataviewStub , 'insertItem' ) ;
670
+ const selectSpy = jest . spyOn ( gridStub , 'setSelectedRows' ) ;
671
+ const scrollSpy = jest . spyOn ( gridStub , 'scrollRowIntoView' ) ;
672
+ const rxSpy = jest . spyOn ( service . onItemAdded , 'next' ) ;
673
+
674
+ service . addItem ( mockItem ) ;
675
+
676
+ expect ( addSpy ) . toHaveBeenCalledTimes ( 1 ) ;
677
+ expect ( addSpy ) . toHaveBeenCalledWith ( 0 , mockItem ) ;
678
+ expect ( selectSpy ) . not . toHaveBeenCalled ( ) ;
679
+ expect ( scrollSpy ) . toHaveBeenCalledWith ( 0 ) ;
680
+ expect ( rxSpy ) . toHaveBeenCalledWith ( mockItem ) ;
681
+ delete mockGridOptions . datasetIdPropertyName ;
682
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( mockGridOptions ) ;
683
+ } ) ;
684
+
685
+ it ( 'should throw an error when 1st argument for the item object is missing the Id defined by the "datasetIdPropertyName" property' , ( ) => {
686
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { enableAutoResize : true , datasetIdPropertyName : 'customId' } as GridOption ) ;
687
+ expect ( ( ) => service . addItem ( null ) ) . toThrowError ( 'Adding an item requires the item to include an "customId" property' ) ;
688
+ expect ( ( ) => service . addItem ( { user : 'John' } ) ) . toThrowError ( 'Adding an item requires the item to include an "customId" property' ) ;
689
+
690
+ // reset mock
691
+ delete mockGridOptions . datasetIdPropertyName ;
692
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( mockGridOptions ) ;
693
+ } ) ;
606
694
} ) ;
607
695
608
696
describe ( 'deleteItem methods' , ( ) => {
@@ -747,6 +835,32 @@ describe('Grid Service', () => {
747
835
const output = service . deleteItemByIds ( 5 , { triggerEvent : true } ) ;
748
836
expect ( output ) . toEqual ( [ ] ) ;
749
837
} ) ;
838
+
839
+ it ( 'should expect the service to call the DataView "insertItem" when calling "addItem" with an item that has an Id defined by the "datasetIdPropertyName" property' , ( ) => {
840
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { ...mockGridOptions , datasetIdPropertyName : 'customId' } ) ;
841
+ const mockItem = { customId : 4 , user : { firstName : 'John' , lastName : 'Doe' } } ;
842
+ const deleteSpy = jest . spyOn ( dataviewStub , 'deleteItem' ) ;
843
+ const rxSpy = jest . spyOn ( service . onItemDeleted , 'next' ) ;
844
+
845
+ const output = service . deleteItemById ( mockItem . customId ) ;
846
+
847
+ expect ( output ) . toEqual ( 4 ) ;
848
+ expect ( deleteSpy ) . toHaveBeenCalledTimes ( 1 ) ;
849
+ expect ( deleteSpy ) . toHaveBeenCalledWith ( mockItem . customId ) ;
850
+ expect ( rxSpy ) . toHaveBeenLastCalledWith ( mockItem . customId ) ;
851
+ delete mockGridOptions . datasetIdPropertyName ;
852
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( mockGridOptions ) ;
853
+ } ) ;
854
+
855
+ it ( 'should throw an error when 1st argument for the item object is missing the Id defined by the "datasetIdPropertyName" property' , ( ) => {
856
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( { enableAutoResize : true , datasetIdPropertyName : 'customId' } as GridOption ) ;
857
+ expect ( ( ) => service . deleteItem ( null ) ) . toThrowError ( 'Deleting an item requires the item to include an "customId" property' ) ;
858
+ expect ( ( ) => service . deleteItem ( { user : 'John' } ) ) . toThrowError ( 'Deleting an item requires the item to include an "customId" property' ) ;
859
+
860
+ // reset mock
861
+ delete mockGridOptions . datasetIdPropertyName ;
862
+ jest . spyOn ( gridStub , 'getOptions' ) . mockReturnValue ( mockGridOptions ) ;
863
+ } ) ;
750
864
} ) ;
751
865
752
866
describe ( 'clearAllFiltersAndSorts method' , ( ) => {
0 commit comments