Unit tests

This commit is contained in:
Matthew Ross 2018-04-06 14:55:20 -04:00
parent 4e2843499e
commit 4b0a8b628a
4 changed files with 340 additions and 27 deletions

View File

@ -26,41 +26,40 @@ class SelectableUser extends User {
@Component({
selector: 'tb-board-admin',
templateUrl: './board-admin.component.html',
providers: [ BoardAdminService ],
viewProviders: [ DragulaService ]
providers: [ BoardAdminService ]
})
export class BoardAdmin implements OnDestroy {
private displayBoards: Array<Board>;
private noBoardsMessage: string;
private boardToRemove: Board;
private userFilter: string;
private statusFilter: string;
private sortFilter: string;
private firstRun = true;
private subs: Array<any>;
public displayBoards: Array<Board>;
public users: Array<User>;
public boards: Array<Board>;
public activeUser: User;
public modalProps: BoardData;
public boardToRemove: Board;
public strings: any;
public hasBAUsers = false;
public loading = true;
public saving = false;
public userFilter: string;
public statusFilter: string;
public sortFilter: string;
public MODAL_ID: string;
public MODAL_CONFIRM_ID: string;
constructor(private auth: AuthService,
public modal: ModalService,
private settings: SettingsService,
private boardService: BoardAdminService,
public settings: SettingsService,
public boardService: BoardAdminService,
private notes: NotificationsService,
private stringsService: StringsService,
private dragula: DragulaService) {
public dragula: DragulaService) {
this.MODAL_ID = 'board-addedit-form';
this.MODAL_CONFIRM_ID = 'board-remove-confirm';
@ -144,9 +143,9 @@ export class BoardAdmin implements OnDestroy {
}
this.boardService.editBoard(this.modalProps)
.subscribe((response: ApiResponse) => {
this.handleResponse(response);
});
.subscribe((response: ApiResponse) => {
this.handleResponse(response);
});
}
removeBoard(): void {
@ -395,8 +394,8 @@ export class BoardAdmin implements OnDestroy {
private onPropertyEdit(obj: string, prop: string,
i: number, value: any): void {
this.modalProps[obj][i][prop] = value;
}
this.modalProps[obj][i][prop] = value;
}
private getColor(category: any): string {
if (category.default_task_color) {

View File

@ -21,9 +21,14 @@ export class DragulaMock {
{ parentNode: { id: '1' } },
{ parentNode: { id: '1' } }
]);
public dragend = {
subscribe: (fn) => { fn(); }
};
find () {
return {};
return { drake: {
containers: []
} };
}
destroy () {}
@ -34,7 +39,8 @@ export class DragulaMock {
}
export class BoardServiceMock {
public activeBoardChanged = new BehaviorSubject({ id: 0, name: 'Test', columns: [] });
public activeBoardChanged =
new BehaviorSubject({ id: 0, name: 'Test', columns: [] });
getBoards () {
return new BehaviorSubject({
@ -51,3 +57,21 @@ export class BoardServiceMock {
}
}
export class SettingsServiceMock {
public usersChanged = new BehaviorSubject([{ security_level: 2 }]);
public boardsChanged = new BehaviorSubject([
{ columns: [{ position: 3 }, { position: 2 }] }
]);
updateBoards () { }
updateActions () { }
}
export class AuthServiceMock {
public userChanged = new BehaviorSubject({ security_level: 1 });
public userOptions = { show_animations: false };
}

View File

@ -2,7 +2,6 @@ import { TestBed, ComponentFixture } from '@angular/core/testing'
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormsModule } from '@angular/forms';
import { DomSanitizer } from '@angular/platform-browser';
import { DragulaService } from 'ng2-dragula/ng2-dragula';
import { DragulaModule } from 'ng2-dragula/ng2-dragula';
@ -16,14 +15,22 @@ import {
StringsService
} from '../../../../src/app/shared/services';
import { SettingsService } from '../../../../src/app/settings/settings.service';
import { BoardAdminService } from '../../../../src/app/settings/board-admin/board-admin.service';
import {
BoardAdminService
} from '../../../../src/app/settings/board-admin/board-admin.service';
import { Board } from '../../../../src/app/shared/models';
import { DragulaMock, SettingsServiceMock, AuthServiceMock } from '../../mocks';
import { BoardAdmin } from '../../../../src/app/settings/board-admin/board-admin.component';
import {
BoardAdmin
} from '../../../../src/app/settings/board-admin/board-admin.component';
describe('BoardAdmin', () => {
let component: BoardAdmin,
fixture: ComponentFixture<BoardAdmin>;
const getPrivateFunction = name => component[name].bind(component);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
@ -41,9 +48,10 @@ describe('BoardAdmin', () => {
ModalService,
NotificationsService,
StringsService,
SettingsService,
BoardAdminService,
DragulaService
{ provide: DragulaService, useClass: DragulaMock },
{ provide: SettingsService, useClass: SettingsServiceMock },
{ provide: AuthService, useClass: AuthServiceMock }
]
}).compileComponents();
});
@ -58,10 +66,237 @@ describe('BoardAdmin', () => {
expect(component).toBeTruthy();
});
// it('calls a service to add a board', () => {
// component.modalProps.title = 'Add';
//
// });
it('sets up drag and drop during ngAfterContentInit', () => {
component.modalProps = <any>{ columns: [{ position: '' }] };
component.ngAfterContentInit();
expect((<any>component.dragula).opts.moves).toEqual(jasmine.any(Function));
expect(component.modalProps.columns[0].position).toEqual('0');
const test = (<any>component.dragula).opts.moves(null, null, {
classList: { contains: () => false }
});
expect(test).toEqual(false);
});
it('validates a board before saving', () => {
component.modalProps = <any>{ columns: [] };
component.addEditBoard();
expect(component.saving).toEqual(false);
});
it('calls a service to add a board', () => {
component.modalProps = <any>{
title: 'Add',
name: 'Test',
columns: [{}]
};
component.users = <any>[{ selected: true }];
let called = false;
(<any>component.boardService).addBoard = () => {
return { subscribe: fn => {
const board = new Board();
fn({ status: 'success', alerts: [{}], data: [{}, [board]] });
called = true;
} };
};
component.addEditBoard();
expect(called).toEqual(true);
});
it('calls a service to edit a board', () => {
component.modalProps = <any>{
title: '',
name: '',
columns: [{}]
};
component.users = <any>[{}];
let called = false;
(<any>component.boardService).editBoard = () => {
return { subscribe: fn => {
const board = new Board();
fn({ status: 'success', alerts: [{}], data: [{}, [board]] });
called = true;
} };
};
component.addEditBoard();
component.modalProps.name = 'Test';
expect(called).toEqual(false);
component.addEditBoard();
expect(called).toEqual(true);
});
it('calls a service to remove a board', () => {
let called = false;
(<any>component.boardService).removeBoard = () => {
return { subscribe: fn => fn({ alerts: [], data: [{}, []] }) };
};
(<any>component.settings).getActions = () => {
return { subscribe: fn => {
fn({ alerts: [], data: [{}, []] });
called = true;
} };
};
component.boardToRemove = <any>{ id: 1 };
component.removeBoard();
expect(called).toEqual(true);
});
it('calls a service to toggle a board\'s status', () => {
let called = false;
(<any>component.boardService).editBoard = () => {
return { subscribe: fn => {
const board = new Board();
fn({ status: 'success', alerts: [{}], data: [{}, [board]] });
called = true;
} };
};
component.toggleBoardStatus(<any>{
id: 1, name: 'Name', is_active: true, columns: [],
categories: [], issue_trackers: [], users: []
});
expect(called).toEqual(true);
});
it('can filter the list of boards by user', () => {
component.boards = <any>[
{ users: [{ id: 1 }] }
];
component.filterBoards();
expect(component.displayBoards.length).toEqual(1);
component.userFilter = '1';
component.filterBoards();
expect(component.displayBoards.length).toEqual(1);
component.userFilter = '2';
component.filterBoards();
expect(component.displayBoards.length).toEqual(0);
});
it('can filter the list of boards by status', () => {
component.boards = <any>[
{ is_active: true, users: [] }
];
component.filterBoards();
expect(component.displayBoards.length).toEqual(1);
component.statusFilter = '1';
component.filterBoards();
expect(component.displayBoards.length).toEqual(1);
component.statusFilter = '0';
component.filterBoards();
expect(component.displayBoards.length).toEqual(0);
});
it('sorts the list of boards after filtering', () => {
component.boards = <any>[
{ id: 1, name: 'last' },
{ id: 2, name: 'first' }
];
component.sortFilter = 'name-asc';
component.filterBoards();
expect(component.displayBoards[0].name).toEqual('first');
component.sortFilter = 'name-desc';
component.filterBoards();
expect(component.displayBoards[0].name).toEqual('last');
component.sortFilter = 'id-asc';
component.filterBoards();
expect(component.displayBoards[0].id).toEqual(1);
component.sortFilter = 'id-desc';
component.filterBoards();
expect(component.displayBoards[0].id).toEqual(2);
});
it('can stop enter key events from bubbling', () => {
let called = false;
component.cancelEnterKey({ stopPropagation: () => called = true });
expect(called).toEqual(true);
});
it('can get a property value for the modal', () => {
const getPropertyValue = getPrivateFunction('getPropertyValue');
component.modalProps = <any>{ columns: [{ name: 'test' }] };
const actual = getPropertyValue('columns', 'name', 0);
expect(actual).toEqual('test');
});
it('handles a property change', () => {
const onPropertyEdit = getPrivateFunction('onPropertyEdit');
component.modalProps = <any>{ columns: [{ name: 'test' }] };
onPropertyEdit('columns', 'name', 0, 'changed');
expect(component.modalProps.columns[0].name).toEqual('changed');
});
it('gets a category color', () => {
const getColor = getPrivateFunction('getColor');
let actual = getColor({ default_task_color: 'red' });
expect(actual).toEqual('red');
actual = getColor({ defaultColor: 'orange' });
expect(actual).toEqual('orange');
});
it('can set a category color', () => {
const setCategoryColor = getPrivateFunction('setCategoryColor');
component.modalProps = <any>{ categories: [{}] };
setCategoryColor('purple', 0);
const actual = component.modalProps.categories[0].default_task_color;
expect(actual).toEqual('purple');
});
it('can show a modal', () => {
const showModal = getPrivateFunction('showModal');
component.users = <any>[{ selected: true }];
showModal('Add');
expect((<any>component.users[0]).selected).toEqual(false);
showModal('Edit', new Board());
});
it('can show a confirmation modal', () => {
const showConfirmModal = getPrivateFunction('showConfirmModal');
showConfirmModal(<any>{ works: true });
expect((<any>component.boardToRemove).works).toEqual(true);
});
});

View File

@ -0,0 +1,55 @@
import { TestBed, getTestBed } from '@angular/core/testing'
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
import {
BoardAdminService
} from '../../../../src/app/settings/board-admin/board-admin.service';
describe('BoardAdminService', () => {
let injector: TestBed;
let service: BoardAdminService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [BoardAdminService]
});
injector = getTestBed();
service = injector.get(BoardAdminService);
httpMock = injector.get(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
})
it('should be created', () => {
expect(service).toBeTruthy();
});
it('adds a board', () => {
service.addBoard(<any>{}).subscribe(response => {
expect(response.data.length).toEqual(0);
});
testCall('api/boards', 'POST');
});
const testCall = (url, method, isError = false) => {
const req = httpMock.expectOne(url);
expect(req.request.method).toEqual(method);
if (isError) {
req.flush({ alerts: [{}] }, { status: 500, statusText: '' });
} else {
req.flush({ data: [] });
}
}
});