Skip to content

Commit daa80b3

Browse files
committed
fix(reflection): emit correct sort order in nested DOM elements
1 parent 301049b commit daa80b3

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

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

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,22 @@ describe('NgsgItemDirective', () => {
8484
expect(ngsgSortService.initSort).toHaveBeenCalledWith(sortGroup);
8585
});
8686

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 = {};
87+
it('should call sort with the host if the event occured on the host', () => {
88+
const event = {target: {matches: () => true}};
89+
ngsgStore.hasSelectedItems.and.returnValue(true);
90+
9091
sut.dragEnter(event);
91-
expect(ngsgSortService.sort).not.toHaveBeenCalled();
92+
expect(ngsgSortService.sort).toHaveBeenCalledWith(event.target);
9293
});
9394

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-
};
95+
it('should call sort with the host, even if the event did not occure on it', () => {
96+
const event = {target: {matches: () => false}};
97+
const host = 'Some element' as any;
98+
ngsgStore.hasSelectedItems.and.returnValue(true);
99+
NgsgElementsHelper.findHost = () => host;
100+
101101
sut.dragEnter(event);
102-
expect(ngsgSortService.sort).not.toHaveBeenCalled();
102+
expect(ngsgSortService.sort).toHaveBeenCalledWith(host);
103103
});
104104

105105
it('should sort the items if the event occured on the host and on the correct group', () => {
@@ -133,13 +133,39 @@ describe('NgsgItemDirective', () => {
133133

134134
it('should sort if the group contains selectedItems', () => {
135135
ngsgStore.hasSelectedItems.and.returnValue(true);
136-
sut.drop({});
136+
sut.drop({target: {matches: () => true}});
137137
expect(ngsgSortService.endSort).toHaveBeenCalled();
138138
});
139139

140+
it('should call the reflection service with the host if the event occured on it', () => {
141+
const group = 'test-group';
142+
const event = {target: {matches: () => true}};
143+
sut.ngSortGridGroup = group;
144+
ngsgStore.hasSelectedItems.and.returnValue(true);
145+
146+
sut.drop(event);
147+
expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, event.target);
148+
});
149+
150+
it('should call the reflection service with the host even if the event did not occured on it', () => {
151+
const group = 'test-group';
152+
const event = {target: {matches: () => false}};
153+
const host = 'Some element' as any;
154+
NgsgElementsHelper.findHost = () => host;
155+
sut.ngSortGridGroup = group;
156+
ngsgStore.hasSelectedItems.and.returnValue(true);
157+
158+
sut.drop(event);
159+
expect(ngsgReflectService.reflectChanges).toHaveBeenCalledWith(group, host);
160+
});
161+
140162
it('should get the reflected changes from the reflection service and emit them', done => {
141163
const group = 'test-group';
142-
const event = {target: 'some target'};
164+
const event = {
165+
target: {
166+
matches: () => true
167+
}
168+
};
143169
const reflectedChanges = ['item two', 'item one', 'item three'];
144170

145171
ngsgStore.hasSelectedItems.and.returnValue(true);
@@ -155,13 +181,13 @@ describe('NgsgItemDirective', () => {
155181
});
156182

157183
it('should reset the selected items on drop', () => {
158-
const event = {target: 'some target'};
184+
const event = {target: {matches: () => true}};
159185
sut.drop(event);
160186
expect(ngsgStore.resetSelectedItems).toHaveBeenCalled();
161187
});
162188

163189
it('should stream the dropped event on the eventservice', done => {
164-
const event = {target: 'some target'};
190+
const event = {target: {matches: () => true}};
165191
ngsgEventService.dropped$.subscribe(() => done());
166192
sut.drop(event);
167193
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export class NgsgItemDirective implements OnInit, AfterViewInit, OnDestroy {
9595
return;
9696
}
9797
this.sortService.endSort();
98-
const reflectedChanges = this.reflectService.reflectChanges(this.ngSortGridGroup, event.target);
98+
const element = !this.occuredOnHost(event) ? NgsgElementsHelper.findHost(event.target, selector) : event.target;
99+
const reflectedChanges = this.reflectService.reflectChanges(this.ngSortGridGroup, element);
99100
this.sorted.next(reflectedChanges);
100101
this.ngsgStore.resetSelectedItems(this.ngSortGridGroup);
101102
this.ngsgEventService.dropped$.next();

0 commit comments

Comments
 (0)