removed DL reload dependency (#2581)

This commit is contained in:
Cilibiu Bogdan
2017-11-01 11:36:51 +02:00
committed by Eugenio Romano
parent e63e29586b
commit b67ed7e545
3 changed files with 51 additions and 37 deletions

View File

@@ -92,6 +92,25 @@ describe('NodeFavoriteDirective', () => {
expect(directiveInstance.markFavoritesNodes).toHaveBeenCalledWith(component.selection);
});
it('should reset favorites if selection is empty', fakeAsync(() => {
spyOn(favoritesApi, 'getFavorite').and.returnValue(Promise.resolve());
component.selection = [
{ entry: { id: '1', name: 'name1' } }
];
fixture.detectChanges();
tick();
expect(directiveInstance.hasFavorites()).toBe(true);
component.selection = [];
fixture.detectChanges();
tick();
expect(directiveInstance.hasFavorites()).toBe(false);
}));
});
describe('markFavoritesNodes()', () => {
@@ -267,6 +286,32 @@ describe('NodeFavoriteDirective', () => {
expect(component.done).toHaveBeenCalled();
}));
it('should set isFavorites items to false', fakeAsync(() => {
removeFavoriteSpy.and.returnValue(Promise.resolve());
directiveInstance.favorites = [
{ entry: { id: '1', name: 'name1', isFavorite: true } }
];
element.triggerEventHandler('click', null);
tick();
expect(directiveInstance.hasFavorites()).toBe(false);
}));
it('should set isFavorites items to true', fakeAsync(() => {
addFavoriteSpy.and.returnValue(Promise.resolve());
directiveInstance.favorites = [
{ entry: { id: '1', name: 'name1', isFavorite: false } }
];
element.triggerEventHandler('click', null);
tick();
expect(directiveInstance.hasFavorites()).toBe(true);
}));
});
describe('getFavorite()', () => {
@@ -297,35 +342,6 @@ describe('NodeFavoriteDirective', () => {
}));
});
describe('reset()', () => {
beforeEach(() => {
spyOn(favoritesApi, 'removeFavoriteSite').and.returnValue(Promise.resolve());
spyOn(favoritesApi, 'addFavorite').and.returnValue(Promise.resolve());
});
it('should reset favorite collection after removeFavoriteSite()', fakeAsync(() => {
directiveInstance.favorites = [
{ entry: { id: '1', name: 'name1', isFavorite: true } }
];
element.triggerEventHandler('click', null);
tick();
expect(directiveInstance.favorites.length).toBe(0);
}));
it('should reset favorite collection after addFavorite()', fakeAsync(() => {
directiveInstance.favorites = [
{ entry: { id: '1', name: 'name1', isFavorite: false } }
];
element.triggerEventHandler('click', null);
tick();
expect(directiveInstance.favorites.length).toBe(0);
}));
});
describe('hasFavorites()', () => {
it('should return false when favorites collection is empty', () => {
directiveInstance.favorites = [];

View File

@@ -41,6 +41,8 @@ export class NodeFavoriteDirective implements OnChanges {
ngOnChanges(changes) {
if (!changes.selection.currentValue.length) {
this.favorites = [];
return;
}
@@ -63,7 +65,7 @@ export class NodeFavoriteDirective implements OnChanges {
});
Observable.forkJoin(batch).subscribe(() => {
this.reset();
this.favorites.map(selected => selected.entry.isFavorite = false);
this.toggle.emit();
});
}
@@ -74,14 +76,14 @@ export class NodeFavoriteDirective implements OnChanges {
Observable.fromPromise(this.alfrescoApiService.getInstance().core.favoritesApi.addFavorite('-me-', <any> body))
.subscribe(() => {
this.reset();
notFavorite.map(selected => selected.entry.isFavorite = true);
this.toggle.emit();
});
}
}
markFavoritesNodes(selection: MinimalNodeEntity[]) {
if (selection.length < this.favorites.length) {
if (selection.length <= this.favorites.length) {
const newFavorites = this.reduce(this.favorites, selection);
this.favorites = newFavorites;
}
@@ -100,10 +102,6 @@ export class NodeFavoriteDirective implements OnChanges {
return this.favorites.every((selected) => selected.entry.isFavorite);
}
private reset() {
this.favorites = [];
}
private getProcessBatch(selection): any[] {
return selection.map((selected: MinimalNodeEntity) => this.getFavorite(selected));
}