mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
unit test performance (#3194)
* DataTable (-4 sec) * PaginationComponent (-1 sec) * DocumentList * custom testbed setup, test upgrades * test fixes * more test fixes * remove fdescribe * test fixes * test fixes * more test fixes * test fixes * upgrade tests * update tests * upgrade tests * upgrade tests * upgrade tests * upgrade tests * update tests * translate loader fixes * auth and cookie fixes * upgrade tests * upgrade tests * test fixes * almost there * diable broken tests * process tests (part 1) * fix lint issues * another test upgrade * almost there * cleanup * insights testing upgrade * improve tests * tests cleanup * tests cleanup * cleanup tests * test cleanup * favorite nodes tests * rebase fix syntax * fix core test * give up test focus * flush tabs * fix search test * Update document-list.component.spec.ts * fix document list lock * increase tick time * remove duplicate test
This commit is contained in:
committed by
Eugenio Romano
parent
9fbfcfa96e
commit
382ea3c1b3
@@ -15,97 +15,71 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Component, DebugElement } from '@angular/core';
|
||||
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
|
||||
import { By } from '@angular/platform-browser';
|
||||
import { AlfrescoApiService } from '../services/alfresco-api.service';
|
||||
import { SimpleChange } from '@angular/core';
|
||||
import { fakeAsync, tick } from '@angular/core/testing';
|
||||
import { NodeFavoriteDirective } from './node-favorite.directive';
|
||||
|
||||
@Component({
|
||||
template: `
|
||||
<div [adf-node-favorite]="selection"
|
||||
(toggle)="done()">
|
||||
</div>`
|
||||
})
|
||||
class TestComponent {
|
||||
selection;
|
||||
|
||||
done = jasmine.createSpy('done');
|
||||
}
|
||||
import { AlfrescoApiServiceMock } from '../mock/alfresco-api.service.mock';
|
||||
import { AppConfigService, StorageService } from '@alfresco/adf-core';
|
||||
|
||||
describe('NodeFavoriteDirective', () => {
|
||||
let component: TestComponent;
|
||||
let fixture: ComponentFixture<TestComponent>;
|
||||
let element: DebugElement;
|
||||
let directiveInstance;
|
||||
let apiService;
|
||||
let favoritesApi;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [
|
||||
TestComponent
|
||||
]
|
||||
})
|
||||
.compileComponents()
|
||||
.then(() => {
|
||||
fixture = TestBed.createComponent(TestComponent);
|
||||
component = fixture.componentInstance;
|
||||
element = fixture.debugElement.query(By.directive(NodeFavoriteDirective));
|
||||
directiveInstance = element.injector.get(NodeFavoriteDirective);
|
||||
let directive;
|
||||
let alfrescoApiService;
|
||||
|
||||
apiService = TestBed.get(AlfrescoApiService);
|
||||
favoritesApi = apiService.getInstance().core.favoritesApi;
|
||||
});
|
||||
}));
|
||||
beforeEach(() => {
|
||||
alfrescoApiService = new AlfrescoApiServiceMock(new AppConfigService(null), new StorageService());
|
||||
directive = new NodeFavoriteDirective( alfrescoApiService);
|
||||
});
|
||||
|
||||
describe('selection input change event', () => {
|
||||
it('should not call markFavoritesNodes() if input list is empty', () => {
|
||||
spyOn(directiveInstance, 'markFavoritesNodes');
|
||||
spyOn(directive, 'markFavoritesNodes');
|
||||
|
||||
component.selection = [];
|
||||
const change = new SimpleChange(null, [], true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(directiveInstance.markFavoritesNodes).not.toHaveBeenCalledWith();
|
||||
expect(directive.markFavoritesNodes).not.toHaveBeenCalledWith();
|
||||
});
|
||||
|
||||
it('should call markFavoritesNodes() on input change', () => {
|
||||
spyOn(directiveInstance, 'markFavoritesNodes');
|
||||
spyOn(directive, 'markFavoritesNodes');
|
||||
|
||||
component.selection = [{ entry: { id: '1', name: 'name1' } }];
|
||||
let selection = [{ entry: { id: '1', name: 'name1' } }];
|
||||
|
||||
fixture.detectChanges();
|
||||
let change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
|
||||
expect(directiveInstance.markFavoritesNodes).toHaveBeenCalledWith(component.selection);
|
||||
expect(directive.markFavoritesNodes).toHaveBeenCalledWith(selection);
|
||||
|
||||
component.selection = [
|
||||
selection = [
|
||||
{ entry: { id: '1', name: 'name1' } },
|
||||
{ entry: { id: '1', name: 'name1' } }
|
||||
{ entry: { id: '2', name: 'name2' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
|
||||
expect(directiveInstance.markFavoritesNodes).toHaveBeenCalledWith(component.selection);
|
||||
expect(directive.markFavoritesNodes).toHaveBeenCalledWith(selection);
|
||||
});
|
||||
|
||||
it('should reset favorites if selection is empty', fakeAsync(() => {
|
||||
spyOn(favoritesApi, 'getFavorite').and.returnValue(Promise.resolve());
|
||||
spyOn(alfrescoApiService.getInstance().core.favoritesApi, 'getFavorite').and.returnValue(Promise.resolve());
|
||||
|
||||
component.selection = [
|
||||
let selection = [
|
||||
{ entry: { id: '1', name: 'name1' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
let change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.hasFavorites()).toBe(true);
|
||||
expect(directive.hasFavorites()).toBe(true);
|
||||
|
||||
component.selection = [];
|
||||
fixture.detectChanges();
|
||||
change = new SimpleChange(null, [], true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.hasFavorites()).toBe(false);
|
||||
expect(directive.hasFavorites()).toBe(false);
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -113,76 +87,78 @@ describe('NodeFavoriteDirective', () => {
|
||||
let favoritesApiSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
favoritesApiSpy = spyOn(favoritesApi, 'getFavorite');
|
||||
favoritesApiSpy = spyOn(alfrescoApiService.getInstance().core.favoritesApi, 'getFavorite')
|
||||
.and.returnValue(Promise.resolve());
|
||||
});
|
||||
|
||||
it('should check each selected node if it is a favorite', fakeAsync(() => {
|
||||
favoritesApiSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
component.selection = [
|
||||
const selection = [
|
||||
{ entry: { id: '1', name: 'name1' } },
|
||||
{ entry: { id: '2', name: 'name2' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
tick();
|
||||
let change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
|
||||
tick();
|
||||
expect(favoritesApiSpy.calls.count()).toBe(2);
|
||||
|
||||
}));
|
||||
|
||||
it('should not check processed node when another is unselected', fakeAsync(() => {
|
||||
favoritesApiSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
component.selection = [
|
||||
let selection = [
|
||||
{ entry: { id: '1', name: 'name1' } },
|
||||
{ entry: { id: '2', name: 'name2' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
tick();
|
||||
let change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
|
||||
expect(directiveInstance.favorites.length).toBe(2);
|
||||
tick();
|
||||
expect(directive.favorites.length).toBe(2);
|
||||
expect(favoritesApiSpy.calls.count()).toBe(2);
|
||||
|
||||
favoritesApiSpy.calls.reset();
|
||||
|
||||
component.selection = [
|
||||
selection = [
|
||||
{ entry: { id: '2', name: 'name2' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
tick();
|
||||
change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
|
||||
expect(directiveInstance.favorites.length).toBe(1);
|
||||
tick();
|
||||
expect(directive.favorites.length).toBe(1);
|
||||
expect(favoritesApiSpy).not.toHaveBeenCalled();
|
||||
}));
|
||||
|
||||
it('should not check processed nodes when another is selected', fakeAsync(() => {
|
||||
favoritesApiSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
component.selection = [
|
||||
let selection = [
|
||||
{ entry: { id: '1', name: 'name1' } },
|
||||
{ entry: { id: '2', name: 'name2' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
let change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.favorites.length).toBe(2);
|
||||
expect(directive.favorites.length).toBe(2);
|
||||
expect(favoritesApiSpy.calls.count()).toBe(2);
|
||||
|
||||
favoritesApiSpy.calls.reset();
|
||||
|
||||
component.selection = [
|
||||
selection = [
|
||||
{ entry: { id: '1', name: 'name1' } },
|
||||
{ entry: { id: '2', name: 'name2' } },
|
||||
{ entry: { id: '3', name: 'name3' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.favorites.length).toBe(3);
|
||||
expect(directive.favorites.length).toBe(3);
|
||||
expect(favoritesApiSpy.calls.count()).toBe(1);
|
||||
}));
|
||||
});
|
||||
@@ -192,8 +168,8 @@ describe('NodeFavoriteDirective', () => {
|
||||
let addFavoriteSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
removeFavoriteSpy = spyOn(favoritesApi, 'removeFavoriteSite');
|
||||
addFavoriteSpy = spyOn(favoritesApi, 'addFavorite');
|
||||
removeFavoriteSpy = spyOn(alfrescoApiService.getInstance().core.favoritesApi, 'removeFavoriteSite');
|
||||
addFavoriteSpy = spyOn(alfrescoApiService.getInstance().core.favoritesApi, 'addFavorite');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -201,170 +177,172 @@ describe('NodeFavoriteDirective', () => {
|
||||
addFavoriteSpy.calls.reset();
|
||||
});
|
||||
|
||||
it('should not perform action if favorites collection is empty', () => {
|
||||
component.selection = [];
|
||||
it('should not perform action if favorites collection is empty', fakeAsync(() => {
|
||||
let change = new SimpleChange(null, [], true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
tick();
|
||||
|
||||
fixture.detectChanges();
|
||||
element.triggerEventHandler('click', null);
|
||||
directive.toggleFavorite();
|
||||
|
||||
expect(removeFavoriteSpy).not.toHaveBeenCalled();
|
||||
expect(addFavoriteSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should call addFavorite() if none is a favorite', fakeAsync(() => {
|
||||
it('should call addFavorite() if none is a favorite', () => {
|
||||
addFavoriteSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: false } },
|
||||
{ entry: { id: '2', name: 'name2', isFavorite: false } }
|
||||
];
|
||||
|
||||
element.triggerEventHandler('click', null);
|
||||
tick();
|
||||
directive.toggleFavorite();
|
||||
|
||||
expect(addFavoriteSpy.calls.argsFor(0)[1].length).toBe(2);
|
||||
}));
|
||||
});
|
||||
|
||||
it('should call addFavorite() on node that is not a favorite in selection', fakeAsync(() => {
|
||||
it('should call addFavorite() on node that is not a favorite in selection', () => {
|
||||
addFavoriteSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFile: true, isFolder: false, isFavorite: false } },
|
||||
{ entry: { id: '2', name: 'name2', isFile: true, isFolder: false, isFavorite: true } }
|
||||
];
|
||||
|
||||
element.triggerEventHandler('click', null);
|
||||
tick();
|
||||
directive.toggleFavorite();
|
||||
|
||||
const callArgs = addFavoriteSpy.calls.argsFor(0)[1];
|
||||
const callParameter = callArgs[0];
|
||||
|
||||
expect(callArgs.length).toBe(1);
|
||||
expect(callParameter.target.file.guid).toBe('1');
|
||||
}));
|
||||
});
|
||||
|
||||
it('should call removeFavoriteSite() if all are favorites', fakeAsync(() => {
|
||||
it('should call removeFavoriteSite() if all are favorites', () => {
|
||||
removeFavoriteSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: true } },
|
||||
{ entry: { id: '2', name: 'name2', isFavorite: true } }
|
||||
];
|
||||
|
||||
element.triggerEventHandler('click', null);
|
||||
tick();
|
||||
directive.toggleFavorite();
|
||||
|
||||
expect(removeFavoriteSpy.calls.count()).toBe(2);
|
||||
}));
|
||||
});
|
||||
|
||||
it('should emit event when removeFavoriteSite() is done', fakeAsync(() => {
|
||||
removeFavoriteSpy.and.returnValue(Promise.resolve());
|
||||
spyOn(directive.toggle, 'emit');
|
||||
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: true } }
|
||||
];
|
||||
|
||||
element.triggerEventHandler('click', null);
|
||||
directive.toggleFavorite();
|
||||
tick();
|
||||
|
||||
expect(component.done).toHaveBeenCalled();
|
||||
expect(directive.toggle.emit).toHaveBeenCalled();
|
||||
}));
|
||||
|
||||
it('should emit event when addFavorite() is done', fakeAsync(() => {
|
||||
addFavoriteSpy.and.returnValue(Promise.resolve());
|
||||
spyOn(directive.toggle, 'emit');
|
||||
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: false } }
|
||||
];
|
||||
|
||||
element.triggerEventHandler('click', null);
|
||||
directive.toggleFavorite();
|
||||
tick();
|
||||
|
||||
expect(component.done).toHaveBeenCalled();
|
||||
expect(directive.toggle.emit).toHaveBeenCalled();
|
||||
}));
|
||||
|
||||
it('should set isFavorites items to false', fakeAsync(() => {
|
||||
removeFavoriteSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: true } }
|
||||
];
|
||||
|
||||
element.triggerEventHandler('click', null);
|
||||
directive.toggleFavorite();
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.hasFavorites()).toBe(false);
|
||||
expect(directive.hasFavorites()).toBe(false);
|
||||
}));
|
||||
|
||||
it('should set isFavorites items to true', fakeAsync(() => {
|
||||
addFavoriteSpy.and.returnValue(Promise.resolve());
|
||||
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: false } }
|
||||
];
|
||||
|
||||
element.triggerEventHandler('click', null);
|
||||
directive.toggleFavorite();
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.hasFavorites()).toBe(true);
|
||||
expect(directive.hasFavorites()).toBe(true);
|
||||
}));
|
||||
});
|
||||
|
||||
describe('getFavorite()', () => {
|
||||
it('should process node as favorite', fakeAsync(() => {
|
||||
spyOn(favoritesApi, 'getFavorite').and.returnValue(Promise.resolve());
|
||||
spyOn(alfrescoApiService.getInstance().core.favoritesApi, 'getFavorite').and.returnValue(Promise.resolve());
|
||||
|
||||
component.selection = [
|
||||
const selection = [
|
||||
{ entry: { id: '1', name: 'name1' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
let change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.favorites[0].entry.isFavorite).toBe(true);
|
||||
expect(directive.favorites[0].entry.isFavorite).toBe(true);
|
||||
}));
|
||||
|
||||
it('should not process node as favorite', fakeAsync(() => {
|
||||
spyOn(favoritesApi, 'getFavorite').and.returnValue(Promise.reject(null));
|
||||
spyOn(alfrescoApiService.getInstance().core.favoritesApi, 'getFavorite').and.returnValue(Promise.reject({}));
|
||||
|
||||
component.selection = [
|
||||
const selection = [
|
||||
{ entry: { id: '1', name: 'name1' } }
|
||||
];
|
||||
|
||||
fixture.detectChanges();
|
||||
let change = new SimpleChange(null, selection, true);
|
||||
directive.ngOnChanges({'selection': change});
|
||||
tick();
|
||||
|
||||
expect(directiveInstance.favorites[0].entry.isFavorite).toBe(false);
|
||||
expect(directive.favorites[0].entry.isFavorite).toBe(false);
|
||||
}));
|
||||
});
|
||||
|
||||
describe('hasFavorites()', () => {
|
||||
it('should return false when favorites collection is empty', () => {
|
||||
directiveInstance.favorites = [];
|
||||
directive.favorites = [];
|
||||
|
||||
const hasFavorites = directiveInstance.hasFavorites();
|
||||
const hasFavorites = directive.hasFavorites();
|
||||
|
||||
expect(hasFavorites).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false when some are not favorite', () => {
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: true } },
|
||||
{ entry: { id: '2', name: 'name2', isFavorite: false } }
|
||||
];
|
||||
|
||||
const hasFavorites = directiveInstance.hasFavorites();
|
||||
const hasFavorites = directive.hasFavorites();
|
||||
|
||||
expect(hasFavorites).toBe(false);
|
||||
});
|
||||
|
||||
it('return true when all are favorite', () => {
|
||||
directiveInstance.favorites = [
|
||||
directive.favorites = [
|
||||
{ entry: { id: '1', name: 'name1', isFavorite: true } },
|
||||
{ entry: { id: '2', name: 'name2', isFavorite: true } }
|
||||
];
|
||||
|
||||
const hasFavorites = directiveInstance.hasFavorites();
|
||||
const hasFavorites = directive.hasFavorites();
|
||||
|
||||
expect(hasFavorites).toBe(true);
|
||||
});
|
||||
|
Reference in New Issue
Block a user