Skip to content

Commit 68ff9e1

Browse files
committed
test(unittest): add unit tests
1 parent 25a488d commit 68ff9e1

File tree

7 files changed

+376
-8
lines changed

7 files changed

+376
-8
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {NgsgClassService} from './ngsg-class.service';
2+
import createSpy = jasmine.createSpy;
3+
4+
describe('NgsgClassService', () => {
5+
6+
let sut: NgsgClassService;
7+
8+
beforeEach(() => sut = new NgsgClassService());
9+
10+
it('should add the placeholder class', () => {
11+
const addClassSpy = createSpy();
12+
const element = {classList: {add: addClassSpy}} as any;
13+
sut.addPlaceHolderClass(element);
14+
expect(addClassSpy).toHaveBeenCalledWith('ng-sg-placeholder');
15+
});
16+
17+
it('should remove the placeholder class', () => {
18+
const removeClassSpy = createSpy();
19+
const element = {classList: {remove: removeClassSpy}} as any;
20+
sut.removePlaceHolderClass(element);
21+
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-placeholder');
22+
});
23+
24+
it('should add the dropped class', () => {
25+
const addClassSpy = createSpy();
26+
const element = {classList: {add: addClassSpy}} as any;
27+
sut.addDroppedClass(element);
28+
expect(addClassSpy).toHaveBeenCalledWith('ng-sg-dropped');
29+
});
30+
31+
it('should remove the placeholder class', () => {
32+
const removeClassSpy = createSpy();
33+
const element = {classList: {remove: removeClassSpy}} as any;
34+
sut.removeDroppedClass(element);
35+
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-dropped');
36+
});
37+
38+
it('should add the dropped class', () => {
39+
const addClassSpy = createSpy();
40+
const element = {classList: {add: addClassSpy}} as any;
41+
sut.addSelectedClass(element);
42+
expect(addClassSpy).toHaveBeenCalledWith('ng-sg-selected');
43+
});
44+
45+
it('should remove the placeholder class', () => {
46+
const removeClassSpy = createSpy();
47+
const element = {classList: {remove: removeClassSpy}} as any;
48+
sut.removeSelectedClass(element);
49+
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-selected');
50+
});
51+
52+
it('should add the dropped class', () => {
53+
const addClassSpy = createSpy();
54+
const element = {classList: {add: addClassSpy}} as any;
55+
sut.addActiveClass(element);
56+
expect(addClassSpy).toHaveBeenCalledWith('ng-sg-active');
57+
});
58+
59+
it('should remove the placeholder class', () => {
60+
const removeClassSpy = createSpy();
61+
const element = {classList: {remove: removeClassSpy}} as any;
62+
sut.removeActiveClass(element);
63+
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-active');
64+
});
65+
66+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {NgsgElementsHelper} from './ngsg-elements.helper';
2+
3+
describe('NgsgElementsHelper', () => {
4+
5+
it('must return the correct index of the element', () => {
6+
const elementOne = {name: 'child one'} as any;
7+
const elementTwo = {name: 'child two'} as any;
8+
const elementThree = {name: 'chil three'} as any;
9+
const elementFour = {name: 'chil four'} as any;
10+
11+
const children = [elementOne, elementTwo, elementThree, elementFour];
12+
elementOne.parentNode = {children};
13+
elementTwo.parentNode = {children};
14+
elementThree.parentNode = {children};
15+
elementFour.parentNode = {children};
16+
17+
const index = NgsgElementsHelper.findIndex(elementThree);
18+
expect(index).toBe(2);
19+
});
20+
21+
it('must find the element that matches the selector', () => {
22+
const selector = 'sample-selector';
23+
const result = {
24+
matches: (s) => s === selector,
25+
name: 'parentElement'
26+
};
27+
const element = {
28+
parentElement: {
29+
matches: () => false,
30+
parentElement: result
31+
}
32+
} as any;
33+
const findHostSpy = spyOn(NgsgElementsHelper, 'findHost');
34+
findHostSpy.and.callThrough();
35+
36+
NgsgElementsHelper.findHost(element, selector);
37+
expect(findHostSpy).toHaveBeenCalledTimes(2);
38+
});
39+
40+
it('must retunr the element that matches the selector', () => {
41+
const selector = 'sample-selector';
42+
const result = {
43+
matches: (s) => s === selector,
44+
name: 'parentElement'
45+
};
46+
const element = {
47+
parentElement: {
48+
matches: () => false,
49+
parentElement: result
50+
}
51+
} as any;
52+
const findHostSpy = spyOn(NgsgElementsHelper, 'findHost');
53+
findHostSpy.and.callThrough();
54+
55+
const actual = NgsgElementsHelper.findHost(element, selector);
56+
expect((actual as any)).toEqual(result);
57+
});
58+
59+
});

projects/ng-sortgrid/src/lib/ngsg-elements.helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export class NgsgElementsHelper {
2+
23
public static findIndex(element: Element): number {
34
const allElements = element.parentNode.children;
45
return Array.prototype.indexOf.call(allElements, element);
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
});

projects/ng-sortgrid/src/lib/ngsg-item.directive.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const selector = '[ngSortgridItem]';
2424

2525
@Directive({selector})
2626
export class NgsgItemDirective implements OnInit, AfterViewInit, OnDestroy {
27-
@Input() private ngSortGridGroup = 'defaultGroup';
27+
@Input() ngSortGridGroup = 'defaultGroup';
2828
@Input() ngSortGridItems;
2929

3030
@Output() sorted = new EventEmitter<any>();
@@ -37,15 +37,14 @@ export class NgsgItemDirective implements OnInit, AfterViewInit, OnDestroy {
3737
private sortService: NgsgSortService,
3838
private selectionService: NgsgSelectionService,
3939
private reflectService: NgsgReflectService,
40-
private classService: NgsgClassService,
4140
private ngsgStore: NgsgStoreService,
4241
private ngsgEventService: NgsgEventsService
4342
) {
4443
}
4544

4645
ngOnInit(): void {
4746
if (!this.ngSortGridItems) {
48-
console.error(`Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
47+
console.warn(`Ng-sortgrid: No items provided - please use [sortGridItems] to pass in an array of items -
4948
otherwhise the ordered items will not be emitted in the (sorted) event`);
5049
}
5150
this.ngsgStore.initState(this.ngSortGridGroup, this.ngSortGridItems, {});

0 commit comments

Comments
 (0)