@@ -19,32 +19,14 @@ describe('NgsgItemDirective', () => {
1919 [ 'selectElementIfNoSelection' , 'updateSelectedDragItem' ] ) ;
2020 const ngsgReflectService = createSpyObj < NgsgReflectService > ( 'ngsgReflectService' , [ 'reflectChanges' ] ) ;
2121 const ngsgStore = createSpyObj < NgsgStoreService > ( 'ngsgStore' ,
22- [ 'initState' , 'hasSelectedItems' , 'resetSelectedItems' ] ) ;
22+ [ 'initState' , 'hasSelectedItems' , 'resetSelectedItems' , 'hasGroup' , 'hasItems' , 'setItems' ] ) ;
2323 const ngsgEventService = new NgsgEventsService ( ) ;
2424
2525 beforeEach ( ( ) => {
2626 sut = new NgsgItemDirective ( elementRef , ngsgSortService , ngsgSelectionService ,
2727 ngsgReflectService , ngsgStore , ngsgEventService ) ;
2828 } ) ;
2929
30- it ( 'should log a warning if we do not pass in sort grid items' , ( ) => {
31- const consoleWarnSpy = spyOn ( global . console , 'warn' ) ;
32- sut . ngOnInit ( ) ;
33- expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
34- `Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
35- otherwhise the ordered items will not be emitted in the (sorted) event` ) ;
36- } ) ;
37-
38- it ( 'should init the store with the sortGridGroup, the ngSortGridItems and the classes' , ( ) => {
39- const sortGridGroup = 'sortgridgroup' ;
40- const sortGridItems = [ 'item one' , 'item two' , 'item three' ] as any ;
41- sut . ngSortGridItems = sortGridItems ;
42- sut . ngSortGridGroup = sortGridGroup ;
43-
44- sut . ngOnInit ( ) ;
45- expect ( ngsgStore . initState ) . toHaveBeenCalledWith ( sortGridGroup , sortGridItems , { } ) ;
46- } ) ;
47-
4830 it ( 'should set the draggable attribute on the elment' , ( ) => {
4931 sut . ngAfterViewInit ( ) ;
5032 expect ( ( elementRef . nativeElement as any ) . draggable ) . toBeTruthy ( ) ;
@@ -133,6 +115,7 @@ describe('NgsgItemDirective', () => {
133115
134116 it ( 'should sort if the group contains selectedItems' , ( ) => {
135117 ngsgStore . hasSelectedItems . and . returnValue ( true ) ;
118+ ngsgStore . hasItems . and . returnValue ( true ) ;
136119 sut . drop ( { target : { matches : ( ) => true } } ) ;
137120 expect ( ngsgSortService . endSort ) . toHaveBeenCalled ( ) ;
138121 } ) ;
@@ -169,6 +152,7 @@ describe('NgsgItemDirective', () => {
169152 const reflectedChanges = [ 'item two' , 'item one' , 'item three' ] ;
170153
171154 ngsgStore . hasSelectedItems . and . returnValue ( true ) ;
155+ ngsgStore . hasItems . and . returnValue ( true ) ;
172156 ngsgReflectService . reflectChanges . and . returnValue ( reflectedChanges ) ;
173157 sut . ngSortGridGroup = group ;
174158
@@ -212,4 +196,58 @@ describe('NgsgItemDirective', () => {
212196 expect ( ngsgSelectionService . updateSelectedDragItem ) . toHaveBeenCalledWith ( group , host , true ) ;
213197 } ) ;
214198
199+ it ( `should init the state with empty items if group has yet not been
200+ initialized and the currentValue is null` , ( ) => {
201+ const group = 'test-group' ;
202+ const changes = {
203+ ngSortGridItems : {
204+ currentValue : null
205+ }
206+ } as any ;
207+ sut . ngSortGridGroup = group ;
208+ ngsgStore . hasGroup . and . returnValue ( false ) ;
209+
210+ sut . ngOnChanges ( changes ) ;
211+ expect ( ngsgStore . initState ) . toHaveBeenCalledWith ( group , [ ] ) ;
212+ } ) ;
213+
214+ it ( 'should init the state with items from the currentValue if group has yet not been initialized' , ( ) => {
215+ const group = 'test-group' ;
216+ const changes = {
217+ ngSortGridItems : {
218+ currentValue : null
219+ }
220+ } as any ;
221+ sut . ngSortGridGroup = group ;
222+ ngsgStore . hasGroup . and . returnValue ( false ) ;
223+
224+ sut . ngOnChanges ( changes ) ;
225+ expect ( ngsgStore . initState ) . toHaveBeenCalledWith ( group , [ ] ) ;
226+ } ) ;
227+
228+ it ( 'should set the items if the group has allready been initialized' , ( ) => {
229+ const group = 'test-group' ;
230+ const items = [ 'Item one' , 'item two' ] ;
231+ const changes = {
232+ ngSortGridItems : {
233+ currentValue : items
234+ }
235+ } as any ;
236+ sut . ngSortGridGroup = group ;
237+ ngsgStore . hasGroup . and . returnValue ( true ) ;
238+
239+ sut . ngOnChanges ( changes ) ;
240+ expect ( ngsgStore . setItems ) . toHaveBeenCalledWith ( group , items ) ;
241+ } ) ;
242+
243+ it ( 'should log a warning message if you drop and you did not provide any items' , ( ) => {
244+ const expectedWarniningMessage = `Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
245+ otherwhise the ordered items can not be emitted in the (sorted) event` ;
246+ const consoleWarnSpy = spyOn ( console , 'warn' ) ;
247+ ngsgStore . hasItems . and . returnValue ( false ) ;
248+
249+ sut . drop ( event ) ;
250+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( expectedWarniningMessage ) ;
251+ } ) ;
252+
215253} ) ;
0 commit comments