Skip to content

Commit 1951337

Browse files
committed
test(asyncpipe): fix failing tests
1 parent 0db7f1e commit 1951337

3 files changed

Lines changed: 88 additions & 20 deletions

File tree

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

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

projects/ng-sortgrid/src/lib/ngsg-store.service.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ describe('NgsgStoreService', () => {
2929
expect(sut.getItems(group)).toEqual([]);
3030
});
3131

32+
it('should return false if the group does not contain items', () => {
33+
const group = 'testgroup';
34+
sut.initState(group);
35+
36+
const hasItems = sut.hasItems(group);
37+
expect(hasItems).toBeFalsy();
38+
});
39+
40+
it('should return true if the group contains items', () => {
41+
const group = 'testgroup';
42+
sut.initState(group, ['item one', 'item two']);
43+
44+
const hasItems = sut.hasItems(group);
45+
expect(hasItems).toBeTruthy();
46+
});
47+
48+
it('should return false if the current group has yet not been initialized', () => {
49+
const group = 'testgroup';
50+
51+
const hasGroup = sut.hasGroup(group);
52+
expect(hasGroup).toBeFalsy();
53+
});
54+
55+
it('should return true if the current group has been initialized', () => {
56+
const group = 'testgroup';
57+
sut.initState(group);
58+
59+
const hasGroup = sut.hasGroup(group);
60+
expect(hasGroup).toBeTruthy();
61+
});
62+
3263
it('should add the classes to the group', () => {
3364
const group = 'testgroup';
3465
const items = ['Item1', 'Item2'];

projects/ng-sortgrid/src/lib/ngsg-store.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export class NgsgStoreService {
2929
}
3030

3131
public setItems(group: string, items: any): void {
32-
console.log('Items', items);
3332
this.state.get(group).items = [...items];
3433
}
3534

0 commit comments

Comments
 (0)