|
| 1 | +import {NgsgItemDirective} from './ngsg-item.directive'; |
| 2 | +import createSpyObj = jasmine.createSpyObj; |
| 3 | + |
| 4 | +import {NgsgSortService} from './ngsg-sort.service'; |
| 5 | +import {NgsgSelectionService} from './ngsg-selection.service'; |
| 6 | +import {NgsgReflectService} from './ngsg-reflect.service'; |
| 7 | +import {NgsgStoreService} from './ngsg-store.service'; |
| 8 | +import {NgsgEventsService} from './ngsg-events.service'; |
| 9 | +import createSpy = jasmine.createSpy; |
| 10 | +import {NgsgElementsHelper} from './ngsg-elements.helper'; |
| 11 | + |
| 12 | +describe('NgsgItemDirective', () => { |
| 13 | + |
| 14 | + let sut: NgsgItemDirective; |
| 15 | + |
| 16 | + const elementRef = {nativeElement: {}}; |
| 17 | + const ngsgSortService = createSpyObj<NgsgSortService>('ngsgSortService', ['initSort', 'sort', 'endSort']); |
| 18 | + const ngsgSelectionService = createSpyObj<NgsgSelectionService>('ngsgSelectionService', |
| 19 | + ['selectElementIfNoSelection', 'updateSelectedDragItem']); |
| 20 | + const ngsgReflectService = createSpyObj<NgsgReflectService>('ngsgReflectService', ['reflectChanges']); |
| 21 | + const ngsgStore = createSpyObj<NgsgStoreService>('ngsgStore', |
| 22 | + ['initState', 'hasSelectedItems', 'resetSelectedItems']); |
| 23 | + const ngsgEventService = new NgsgEventsService(); |
| 24 | + |
| 25 | + 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, {}); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should set the draggable attribute on the elment', () => { |
| 49 | + sut.ngAfterViewInit(); |
| 50 | + expect((elementRef.nativeElement as any).draggable).toBeTruthy(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not set selectedElements if the event did not occur on the host', () => { |
| 54 | + const event = { |
| 55 | + target: { |
| 56 | + matches: () => false |
| 57 | + } |
| 58 | + }; |
| 59 | + sut.dragStart(event); |
| 60 | + expect(ngsgSelectionService.selectElementIfNoSelection).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should call selectionService selectElementIfNoSelection if the event occured on the host', () => { |
| 64 | + const sortGroup = 'test-group'; |
| 65 | + sut.ngSortGridGroup = sortGroup; |
| 66 | + const event = { |
| 67 | + target: { |
| 68 | + matches: () => true |
| 69 | + } |
| 70 | + }; |
| 71 | + sut.dragStart(event); |
| 72 | + expect(ngsgSelectionService.selectElementIfNoSelection).toHaveBeenCalledWith(sortGroup, event.target); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should init the sort for the current group', () => { |
| 76 | + const sortGroup = 'test-group'; |
| 77 | + sut.ngSortGridGroup = sortGroup; |
| 78 | + const event = { |
| 79 | + target: { |
| 80 | + matches: () => true |
| 81 | + } |
| 82 | + }; |
| 83 | + sut.dragStart(event); |
| 84 | + expect(ngsgSortService.initSort).toHaveBeenCalledWith(sortGroup); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not sort the items if the event did not occured in the group', () => { |
| 88 | + ngsgStore.hasSelectedItems.and.returnValue(false); |
| 89 | + const event = {}; |
| 90 | + sut.dragEnter(event); |
| 91 | + expect(ngsgSortService.sort).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should not sort the items if the event did not occured on the host', () => { |
| 95 | + ngsgStore.hasSelectedItems.and.returnValue(false); |
| 96 | + const event = { |
| 97 | + target: { |
| 98 | + matches: () => false |
| 99 | + } |
| 100 | + }; |
| 101 | + sut.dragEnter(event); |
| 102 | + expect(ngsgSortService.sort).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should sort the items if the event occured on the host and on the correct group', () => { |
| 106 | + ngsgStore.hasSelectedItems.and.returnValue(true); |
| 107 | + const event = { |
| 108 | + target: { |
| 109 | + matches: () => true |
| 110 | + } |
| 111 | + }; |
| 112 | + sut.dragEnter(event); |
| 113 | + expect(ngsgSortService.sort).toHaveBeenCalledWith(event.target); |
| 114 | + }); |
| 115 | + |
| 116 | + it('must call event preventDefault', () => { |
| 117 | + const preventDefaultSpy = createSpy(); |
| 118 | + const event = {preventDefault: preventDefaultSpy}; |
| 119 | + sut.dragOver(event); |
| 120 | + expect(preventDefaultSpy).toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('must return false on dragOver', () => { |
| 124 | + const actual = sut.dragOver({}); |
| 125 | + expect(actual).toBeFalsy(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should not call endSort if the group does not contain selectedItems', () => { |
| 129 | + ngsgStore.hasSelectedItems.and.returnValue(false); |
| 130 | + sut.drop({}); |
| 131 | + expect(ngsgSortService.endSort).not.toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should sort if the group contains selectedItems', () => { |
| 135 | + ngsgStore.hasSelectedItems.and.returnValue(true); |
| 136 | + sut.drop({}); |
| 137 | + expect(ngsgSortService.endSort).toHaveBeenCalled(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should get the reflected changes from the reflection service and emit them', done => { |
| 141 | + const group = 'test-group'; |
| 142 | + const event = {target: 'some target'}; |
| 143 | + const reflectedChanges = ['item two', 'item one', 'item three']; |
| 144 | + |
| 145 | + ngsgStore.hasSelectedItems.and.returnValue(true); |
| 146 | + ngsgReflectService.reflectChanges.and.returnValue(reflectedChanges); |
| 147 | + sut.ngSortGridGroup = group; |
| 148 | + |
| 149 | + sut.sorted.subscribe(changes => { |
| 150 | + expect(reflectedChanges).toEqual(changes); |
| 151 | + done(); |
| 152 | + }); |
| 153 | + sut.drop(event); |
| 154 | + expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, event.target); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should reset the selected items on drop', () => { |
| 158 | + const event = {target: 'some target'}; |
| 159 | + sut.drop(event); |
| 160 | + expect(ngsgStore.resetSelectedItems).toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should stream the dropped event on the eventservice', done => { |
| 164 | + const event = {target: 'some target'}; |
| 165 | + ngsgEventService.dropped$.subscribe(() => done()); |
| 166 | + sut.drop(event); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should call the selctionservice with the host if the event occured on the host', () => { |
| 170 | + const group = 'test-group'; |
| 171 | + const event = {target: {matches: () => true}}; |
| 172 | + sut.ngSortGridGroup = group; |
| 173 | + |
| 174 | + sut.clicked(event); |
| 175 | + expect(ngsgSelectionService.updateSelectedDragItem).toHaveBeenCalledWith(group, event.target, true); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should call the selctionservice with the host, even if the event did not occure on it', () => { |
| 179 | + const group = 'test-group'; |
| 180 | + const event = {target: {matches: () => false}}; |
| 181 | + const host = 'Some element' as any; |
| 182 | + NgsgElementsHelper.findHost = () => host; |
| 183 | + sut.ngSortGridGroup = group; |
| 184 | + |
| 185 | + sut.clicked(event); |
| 186 | + expect(ngsgSelectionService.updateSelectedDragItem).toHaveBeenCalledWith(group, host, true); |
| 187 | + }); |
| 188 | + |
| 189 | +}); |
0 commit comments