Unit tests and api improvements for document list

This commit is contained in:
Denys Vuika
2016-06-30 15:44:26 +01:00
parent 258148cc1c
commit b1553a9bed
7 changed files with 133 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ import { ContentAction } from './content-action';
import { DocumentActionsService } from '../services/document-actions.service';
import { FolderActionsService } from '../services/folder-actions.service';
import { ContentActionHandler } from '../models/content-action.model';
import { FileNode } from '../assets/document-library.model.mock';
describe('ContentAction', () => {
@@ -225,4 +226,47 @@ describe('ContentAction', () => {
expect(documentActions.getHandler).not.toHaveBeenCalled();
});
it('should wire model with custom event handler', (done) => {
let action = new ContentAction(actionList, documentActions, folderActions);
let file = new FileNode();
let handler = new EventEmitter();
handler.subscribe((e) => {
expect(e.value).toBe(file);
done();
});
action.execute = handler;
action.ngOnInit();
action.model.handler(file);
});
it('should allow registering model without handler', () => {
let action = new ContentAction(actionList, documentActions, folderActions);
spyOn(actionList, 'registerAction').and.callThrough();
action.execute = null;
action.ngOnInit();
expect(action.model.handler).toBeUndefined();
expect(actionList.registerAction).toHaveBeenCalledWith(action.model);
});
it('should register on init', () => {
let action = new ContentAction(actionList, null, null);
spyOn(action, 'register').and.callThrough();
action.ngOnInit();
expect(action.register).toHaveBeenCalled();
});
it('should require action list to register action with', () => {
let action = new ContentAction(actionList, null, null);
expect(action.register()).toBeTruthy();
action = new ContentAction(null, null, null);
expect(action.register()).toBeFalsy();
});
});

View File

@@ -73,7 +73,14 @@ export class ContentAction implements OnInit, OnChanges {
};
}
this.list.registerAction(this.model);
this.register();
}
register(): boolean {
if (this.list) {
return this.list.registerAction(this.model);
}
return false;
}
ngOnChanges(changes) {

View File

@@ -93,4 +93,20 @@ describe('ContentColumn', () => {
expect(column.model.title).toBe('title2');
});
it('should register on init', () => {
let column = new ContentColumn(columnList);
spyOn(column, 'register').and.callThrough();
column.ngOnInit();
expect(column.register).toHaveBeenCalled();
});
it('should require action list to register action with', () => {
let column = new ContentColumn(columnList);
expect(column.register()).toBeTruthy();
column = new ContentColumn(null);
expect(column.register()).toBeFalsy();
});
});

View File

@@ -66,9 +66,14 @@ export class ContentColumn implements OnInit, OnChanges {
this.model.srTitle = 'Thumbnail';
}
this.register();
}
register(): boolean {
if (this.list) {
this.list.registerColumn(this.model);
return this.list.registerColumn(this.model);
}
return false;
}
ngOnChanges(change) {

View File

@@ -106,4 +106,27 @@ describe('DocumentListBreadcrumb', () => {
expect(documentList.currentFolderPath).toBe(node.path);
});
it('should do nothing for same path', () => {
let called = 0;
component.pathChanged.subscribe(() => called++);
component.currentFolderPath = '/';
component.currentFolderPath = '/';
expect(called).toBe(0);
});
it('should emit path changed event', (done) => {
let path = '/some/path';
component.pathChanged.subscribe(e => {
expect(e.value).toBe(path);
expect(e.route).toBe(component.route);
done();
});
component.currentFolderPath = path;
});
});

View File

@@ -49,6 +49,10 @@ export class DocumentListBreadcrumb {
this._currentFolderPath = this.rootFolder.path;
this.route = [ this.rootFolder ];
}
this.pathChanged.emit({
value: this._currentFolderPath,
route: this.route
});
}
}
@@ -65,6 +69,9 @@ export class DocumentListBreadcrumb {
@Output()
navigate: EventEmitter<any> = new EventEmitter();
@Output()
pathChanged: EventEmitter<any> = new EventEmitter();
onRoutePathClick(route: PathNode, e?: Event) {
if (e) {
e.preventDefault();

View File

@@ -849,15 +849,29 @@ describe('DocumentList', () => {
it('should put folders to top on sort', () => {
let folder = new FolderNode();
let file = new FileNode();
let page = new PageNode([file, folder]);
let file1 = new FileNode('file1');
let file2 = new FileNode('file2');
let page = new PageNode([file1, file2, folder]);
// asc
documentList.sort(page, new ColumnSortingModel({
key: 'name'
key: 'name',
direction: 'asc'
}));
expect(page.list.entries[0]).toBe(folder);
expect(page.list.entries[1]).toBe(file);
expect(page.list.entries[1]).toBe(file1);
expect(page.list.entries[2]).toBe(file2);
// desc
documentList.sort(page, new ColumnSortingModel({
key: 'name',
direction: 'desc'
}));
expect(page.list.entries[0]).toBe(folder);
expect(page.list.entries[1]).toBe(file2);
expect(page.list.entries[2]).toBe(file1);
});
it('should sort by dates up to ms', () => {
@@ -868,6 +882,8 @@ describe('DocumentList', () => {
file2.entry['dateProp'] = new Date(2016, 6, 30, 13, 14, 2);
let page = new PageNode([file1, file2]);
// desc
documentList.sort(page, new ColumnSortingModel({
key: 'dateProp',
direction: 'desc'
@@ -875,6 +891,15 @@ describe('DocumentList', () => {
expect(page.list.entries[0]).toBe(file2);
expect(page.list.entries[1]).toBe(file1);
// asc
documentList.sort(page, new ColumnSortingModel({
key: 'dateProp',
direction: 'asc'
}));
expect(page.list.entries[0]).toBe(file1);
expect(page.list.entries[1]).toBe(file2);
});
});