@@ -10,39 +10,34 @@ import createSpy = jasmine.createSpy;
1010import { NgsgElementsHelper } from './ngsg-elements.helper' ;
1111
1212describe ( 'NgsgItemDirective' , ( ) => {
13-
1413 let sut : NgsgItemDirective ;
1514
1615 const elementRef = { nativeElement : { } } ;
1716 const ngsgSortService = createSpyObj < NgsgSortService > ( 'ngsgSortService' , [ 'initSort' , 'sort' , 'endSort' ] ) ;
18- const ngsgSelectionService = createSpyObj < NgsgSelectionService > ( 'ngsgSelectionService' ,
19- [ 'selectElementIfNoSelection' , 'updateSelectedDragItem' ] ) ;
17+ const ngsgSelectionService = createSpyObj < NgsgSelectionService > ( 'ngsgSelectionService' , [
18+ 'selectElementIfNoSelection' ,
19+ 'updateSelectedDragItem'
20+ ] ) ;
2021 const ngsgReflectService = createSpyObj < NgsgReflectService > ( 'ngsgReflectService' , [ 'reflectChanges' ] ) ;
21- const ngsgStore = createSpyObj < NgsgStoreService > ( 'ngsgStore' ,
22- [ 'initState' , 'hasSelectedItems' , 'resetSelectedItems' ] ) ;
22+ const ngsgStore = createSpyObj < NgsgStoreService > ( 'ngsgStore' , [
23+ 'initState' ,
24+ 'hasSelectedItems' ,
25+ 'resetSelectedItems' ,
26+ 'hasGroup' ,
27+ 'hasItems' ,
28+ 'setItems'
29+ ] ) ;
2330 const ngsgEventService = new NgsgEventsService ( ) ;
2431
2532 beforeEach ( ( ) => {
26- sut = new NgsgItemDirective ( elementRef , ngsgSortService , ngsgSelectionService ,
27- ngsgReflectService , ngsgStore , ngsgEventService ) ;
28- } ) ;
29-
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 , { } ) ;
33+ sut = new NgsgItemDirective (
34+ elementRef ,
35+ ngsgSortService ,
36+ ngsgSelectionService ,
37+ ngsgReflectService ,
38+ ngsgStore ,
39+ ngsgEventService
40+ ) ;
4641 } ) ;
4742
4843 it ( 'should set the draggable attribute on the elment' , ( ) => {
@@ -133,6 +128,7 @@ describe('NgsgItemDirective', () => {
133128
134129 it ( 'should sort if the group contains selectedItems' , ( ) => {
135130 ngsgStore . hasSelectedItems . and . returnValue ( true ) ;
131+ ngsgStore . hasItems . and . returnValue ( true ) ;
136132 sut . drop ( { target : { matches : ( ) => true } } ) ;
137133 expect ( ngsgSortService . endSort ) . toHaveBeenCalled ( ) ;
138134 } ) ;
@@ -169,6 +165,7 @@ describe('NgsgItemDirective', () => {
169165 const reflectedChanges = [ 'item two' , 'item one' , 'item three' ] ;
170166
171167 ngsgStore . hasSelectedItems . and . returnValue ( true ) ;
168+ ngsgStore . hasItems . and . returnValue ( true ) ;
172169 ngsgReflectService . reflectChanges . and . returnValue ( reflectedChanges ) ;
173170 sut . ngSortGridGroup = group ;
174171
@@ -212,4 +209,58 @@ describe('NgsgItemDirective', () => {
212209 expect ( ngsgSelectionService . updateSelectedDragItem ) . toHaveBeenCalledWith ( group , host , true ) ;
213210 } ) ;
214211
212+ it ( `should init the state with empty items if group has yet not been
213+ initialized and the currentValue is null` , ( ) => {
214+ const group = 'test-group' ;
215+ const changes = {
216+ ngSortGridItems : {
217+ currentValue : null
218+ }
219+ } as any ;
220+ sut . ngSortGridGroup = group ;
221+ ngsgStore . hasGroup . and . returnValue ( false ) ;
222+
223+ sut . ngOnChanges ( changes ) ;
224+ expect ( ngsgStore . initState ) . toHaveBeenCalledWith ( group , [ ] ) ;
225+ } ) ;
226+
227+ it ( 'should init the state with items from the currentValue if group has yet not been initialized' , ( ) => {
228+ const group = 'test-group' ;
229+ const changes = {
230+ ngSortGridItems : {
231+ currentValue : null
232+ }
233+ } as any ;
234+ sut . ngSortGridGroup = group ;
235+ ngsgStore . hasGroup . and . returnValue ( false ) ;
236+
237+ sut . ngOnChanges ( changes ) ;
238+ expect ( ngsgStore . initState ) . toHaveBeenCalledWith ( group , [ ] ) ;
239+ } ) ;
240+
241+ it ( 'should set the items if the group has allready been initialized' , ( ) => {
242+ const group = 'test-group' ;
243+ const items = [ 'Item one' , 'item two' ] ;
244+ const changes = {
245+ ngSortGridItems : {
246+ currentValue : items
247+ }
248+ } as any ;
249+ sut . ngSortGridGroup = group ;
250+ ngsgStore . hasGroup . and . returnValue ( true ) ;
251+
252+ sut . ngOnChanges ( changes ) ;
253+ expect ( ngsgStore . setItems ) . toHaveBeenCalledWith ( group , items ) ;
254+ } ) ;
255+
256+ it ( 'should log a warning message if you drop and you did not provide any items' , ( ) => {
257+ const expectedWarniningMessage =
258+ `Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
259+ otherwhise the ordered items can not be emitted in the (sorted) event` ;
260+ const consoleWarnSpy = spyOn ( console , 'warn' ) ;
261+ ngsgStore . hasItems . and . returnValue ( false ) ;
262+
263+ sut . drop ( event ) ;
264+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith ( expectedWarniningMessage ) ;
265+ } ) ;
215266} ) ;
0 commit comments