Final unit tests?

This commit is contained in:
Matthew Ross 2018-04-12 15:20:08 -04:00
parent 68550a2276
commit e5fd5cd523
24 changed files with 380 additions and 825 deletions

View File

@ -10,7 +10,7 @@ import { NotificationsService } from './notifications.service';
export class Notifications {
public notes: Array<Notification>;
constructor(private notifications: NotificationsService) {
constructor(public notifications: NotificationsService) {
this.notes = new Array<Notification>();
notifications.noteAdded
@ -28,7 +28,7 @@ export class Notifications {
setTimeout(() => {
this.notes.splice(index, 1);
}, 500); // 500ms is the fade out transition time
}, 500); // 500ms is the fade out transition time
}
}
}

View File

@ -21,8 +21,8 @@ export class TopNav {
version: string = '';
username: string = '';
constructor(constants: Constants, private router: Router,
private authService: AuthService,
constructor(public constants: Constants, private router: Router,
public authService: AuthService,
private notes: NotificationsService,
private stringsService: StringsService) {
this.version = constants.VERSION;

View File

@ -1,67 +0,0 @@
/* global expect ConstantsMock AuthServiceMock RouterMock NotificationsServiceMock */
var path = '../../../build/login/',
Login = require(path + 'login.component.js').Login;
describe('Login', () => {
var login,
router;
beforeEach(() => {
router = new RouterMock();
login = new Login(ConstantsMock, AuthServiceMock,
router, new NotificationsServiceMock());
});
it('has a version property', () => {
expect(login.version).to.equal('TEST');
});
it('has a username property', () => {
expect(login.username).to.equal('');
});
it('has a password property', () => {
expect(login.password).to.equal('');
});
it('has a remember property', () => {
expect(login.remember).to.equal(false);
});
it('has an isSubmitted property', () => {
expect(login.isSubmitted) .to.equal(false);
});
it('requires both username and password to log in', done => {
login.notes.noteAdded.subscribe(note => {
expect(note.type).to.equal('error');
done();
});
login.username = 'test';
login.login();
});
it('calls the AuthService to log in and navigates the router', done => {
login.username = 'test';
login.password = 'test';
login.login();
setTimeout(() => {
expect(router.path).to.equal('/boards');
done();
}, 10);
});
it('calls the AuthService during ngOnInit', done => {
expect(login.ngOnInit).to.be.a('function');
login.ngOnInit();
setTimeout(() => {
expect(router.path).to.equal('/boards');
done();
}, 10);
});
});

View File

@ -100,3 +100,11 @@ export class AuthServiceMock {
updateUser () { }
}
export class NotificationsServiceMock {
public noteAdded = new BehaviorSubject({});
addNote (note) {
this.noteAdded.next(note);
}
}

View File

@ -1,37 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
ApiResponse = require(path + 'api-response.model.js').ApiResponse;
describe('ApiResponse', () => {
var apiResponse;
beforeEach(() => {
apiResponse = new ApiResponse([{ type: 'error', text: 'test' }],
[{ data: 'whatever' }],
'success');
});
it('has sane defaults', () => {
apiResponse = new ApiResponse();
expect(apiResponse.alerts.length).to.equal(0);
expect(apiResponse.data.length).to.equal(0);
expect(apiResponse.status).to.equal('');
});
it('has an alerts array', () => {
expect(apiResponse.alerts).to.be.an('array');
expect(apiResponse.alerts.length).to.equal(1);
});
it('has a data array', () => {
expect(apiResponse.data).to.be.an('array');
expect(apiResponse.data.length).to.equal(1);
});
it('has a status string', () => {
expect(apiResponse.status).to.be.a('string');
expect(apiResponse.status).to.equal('success');
});
});

View File

@ -1,50 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
Attachment = require(path + 'attachment.model.js').Attachment;
describe('Attachment', () => {
var attachment,
now = new Date();
beforeEach(() => {
attachment = new Attachment();
});
it('has an id', () => {
expect(attachment.id).to.be.a('number');
expect(attachment.id).to.equal(0);
});
it('has a filename', () => {
expect(attachment.filename).to.be.a('string');
expect(attachment.filename).to.equal('');
});
it('has a name', () => {
expect(attachment.name).to.be.a('string');
expect(attachment.name).to.equal('');
});
it('has a type', () => {
expect(attachment.type).to.be.a('string');
expect(attachment.type).to.equal('');
});
it('has a user_id', () => {
expect(attachment.user_id).to.be.a('number');
expect(attachment.user_id).to.equal(0);
});
it('has a timestamp', () => {
attachment = new Attachment(0, '', '', '', 0, now, 0);
expect(attachment.timestamp).to.be.a('date');
expect(attachment.timestamp).to.equal(now);
});
it('has a task_id', () => {
expect(attachment.task_id).to.be.a('number');
expect(attachment.task_id).to.equal(0);
});
});

View File

@ -1,39 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
AutoAction = require(path + 'auto-actions.model.js').AutoAction;
describe('AutoAction', () => {
var autoAction;
beforeEach(() => {
autoAction = new AutoAction();
});
it('has an id', () => {
expect(autoAction.id).to.be.a('number');
expect(autoAction.id).to.equal(0);
});
it('has a trigger', () => {
expect(autoAction.trigger).to.be.a('number');
expect(autoAction.trigger).to.equal(1);
});
it('has a source_id', () => {
expect(autoAction.source_id).to.equal(null);
});
it('has a type', () => {
expect(autoAction.type).to.be.a('number');
expect(autoAction.type).to.equal(1);
});
it('has a change_to', () => {
expect(autoAction.change_to).to.equal(null);
});
it('has a board_id', () => {
expect(autoAction.board_id).to.equal(null);
});
});

View File

@ -1,104 +0,0 @@
/* globals expect */
var path = '../../../../build/shared/models/',
Board = require(path + 'board.model.js').Board;
describe('Board', () => {
var board;
beforeEach(() => {
var cols = [
{ id: 1, name: 'col1', position: 0, board_id: 1,
ownTask: [{ id: 1 }] }
],
cats = [
{ id: 1, name: 'cat1', default_task_color: '#ffffe0', board_id: 1 }
],
acts = [
{ id: 1, trigger: 1, source_id: 1, type: 1, change_to: '', board_id: 1 }
],
tracks = [
{ id: 1, url: '', regex: '' }
],
users = [
{
default_board_id: 0, email: '', id: 1, last_login: null,
security_level: 1, user_option_id: 1, username: 'tester',
board_access: [ 1 ], collapsed: []
}
];
board = new Board(1, 'test', true, cols, cats, acts, tracks, users);
});
it('has sane defaults', () => {
board = new Board();
expect(board.id).to.equal(0);
expect(board.name).to.equal('');
expect(board.is_active).to.equal(true);
});
it('has id', () => {
expect(board.id).to.be.a('number');
expect(board.id).to.equal(1);
});
it('has name', () => {
expect(board.name).to.be.a('string');
expect(board.name).to.equal('test');
});
it('has is_active', () => {
expect(board.is_active).to.be.a('boolean');
expect(board.is_active).to.equal(true);
});
it('has columns', () => {
expect(board.columns).to.be.an('array');
expect(board.columns.length).to.equal(1);
});
it('has categories', () => {
expect(board.categories).to.be.an('array');
expect(board.categories.length).to.equal(1);
});
it('has auto_actions', () => {
expect(board.auto_actions).to.be.an('array');
expect(board.auto_actions.length).to.equal(1);
});
it('has issue_trackers', () => {
expect(board.issue_trackers).to.be.an('array');
expect(board.issue_trackers.length).to.equal(1);
});
it('has users', () => {
expect(board.users).to.be.an('array');
expect(board.users.length).to.equal(1);
});
it('has a method to add a column', () => {
expect(board.addColumn).to.be.a('function');
board.addColumn('test');
expect(board.columns[1].name).to.equal('test');
});
it('has a method to add a category', () => {
expect(board.addCategory).to.be.a('function');
board.addCategory('test', 'color');
expect(board.categories[1].name).to.equal('test');
expect(board.categories[1].default_task_color).to.equal('color');
});
it('has a method to add an issue tracker', () => {
expect(board.addIssueTracker).to.be.a('function');
board.addIssueTracker('testUrl', 'testRegex');
expect(board.issue_trackers[1].url).to.equal('testUrl');
expect(board.issue_trackers[1].regex).to.equal('testRegex');
});
});

View File

@ -1,32 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
Category = require(path + 'category.model.js').Category;
describe('Category', () => {
var category;
beforeEach(() => {
category = new Category();
});
it('has an id', () => {
expect(category.id).to.be.a('number');
expect(category.id).to.equal(0);
});
it('has a name', () => {
expect(category.name).to.be.a('string');
expect(category.name).to.equal('');
});
it('has a default_task_color', () => {
expect(category.default_task_color).to.be.a('string');
expect(category.default_task_color).to.equal('');
});
it('has a board_id', () => {
expect(category.board_id).to.be.a('number');
expect(category.board_id).to.equal(0);
});
});

View File

@ -1,42 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
Column = require(path + 'column.model.js').Column;
describe('ApiResponse', () => {
var column;
beforeEach(() => {
column = new Column();
});
it('has an id', () => {
expect(column.id).to.be.a('number');
expect(column.id).to.equal(0);
});
it('has a name', () => {
expect(column.name).to.be.a('string');
expect(column.name).to.equal('');
});
it('has a position', () => {
expect(column.position).to.be.a('number');
expect(column.position).to.equal(0);
});
it('has a board_id', () => {
expect(column.board_id).to.be.a('number');
expect(column.board_id).to.equal(0);
});
it('has tasks', () => {
expect(column.tasks).to.be.an('array');
expect(column.tasks.length).to.equal(0);
});
it('has a method to check for task limit', () => {
expect(column.hasTaskLimit).to.be.a('function');
expect(column.hasTaskLimit()).to.equal(false);
});
});

View File

@ -1,41 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
Comment = require(path + 'comment.model.js').Comment;
describe('Comment', () => {
var comment;
beforeEach(() => {
comment = new Comment(1, 'text', 1, 1);
});
it('has sane defauls', () => {
comment =new Comment();
expect(comment.id).to.equal(0);
expect(comment.text).to.equal('');
expect(comment.user_id).to.equal(0);
expect(comment.task_id).to.equal(0);
});
it('has an id', () => {
expect(comment.id).to.be.a('number');
expect(comment.id).to.equal(1);
});
it('has a text', () => {
expect(comment.text).to.be.a('string');
expect(comment.text).to.equal('text');
});
it('has a user_id', () => {
expect(comment.user_id).to.be.a('number');
expect(comment.user_id).to.equal(1);
});
it('has a task_id', () => {
expect(comment.task_id).to.be.a('number');
expect(comment.task_id).to.equal(1);
});
});

View File

@ -1,35 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
IssueTracker = require(path + 'issue-tracker.model.js').IssueTracker;
describe('IssueTracker', () => {
var issueTracker;
beforeEach(() => {
issueTracker = new IssueTracker(1, 'url', 'regex');
});
it('has sane defaults', () => {
issueTracker = new IssueTracker();
expect(issueTracker.id).to.equal(0);
expect(issueTracker.url).to.equal('');
expect(issueTracker.regex).to.equal('');
});
it('has an id', () => {
expect(issueTracker.id).to.be.a('number');
expect(issueTracker.id).to.equal(1);
});
it('has a url', () => {
expect(issueTracker.url).to.be.a('string');
expect(issueTracker.url).to.equal('url');
});
it('has a regex', () => {
expect(issueTracker.regex).to.be.a('string');
expect(issueTracker.regex).to.equal('regex');
});
});

View File

@ -0,0 +1,175 @@
import {
Activity,
ActivitySimple,
ApiResponse,
Attachment,
AutoAction,
Board,
Category,
Column,
Comment,
IssueTracker,
Notification,
Task,
UserOptions,
User
} from '../../../../src/app/shared/models';
describe('Models', () => {
describe('Activity', () => {
it('can be constructed', () => {
let actual = new Activity(1, '', '', '', '', 1, 1);
expect(actual).toBeTruthy();
});
it('has a simple version', () => {
let actual = new ActivitySimple('', 1);
expect(actual).toBeTruthy();
});
});
describe('ApiResponse', () => {
it('can be constructed', () => {
expect(new ApiResponse()).toBeTruthy();
});
});
describe('Attachment', () => {
it('can be constructed', () => {
expect(new Attachment()).toBeTruthy();
});
});
describe('AutoAction', () => {
it('can be constructed', () => {
expect(new AutoAction()).toBeTruthy();
});
});
describe('Board', () => {
let board: Board;
beforeEach(() => {
board = new Board();
});
it('can be created with values', () => {
const actual = new Board(1, '', true,
[{ id: 1, name: '', position: 1, board_id: 1, task_limit: 0, ownTask: [] }],
[{ id: 1, name: '', default_task_color: '', board_id: 1 }],
[{ id: 1, trigger: 1, source_id: 1, type: 1, change_to: '', board_id: 1 }],
[{ id: 1, url: '', regex: '' }],
[{
default_board_id: 1, email: '', id: 1, last_login: '',
security_level: 1, user_option_id: 1, username: '',
board_access: '', collapsed: true
}]
);
});
it('can add a column', () => {
expect(board.addColumn).toEqual(jasmine.any(Function));
board.addColumn('test');
expect(board.columns.length).toEqual(1);
});
it('can add a category', () => {
expect(board.addCategory).toEqual(jasmine.any(Function));
board.addCategory('test', 'color');
expect(board.categories.length).toEqual(1);
});
it('can add an issue tracker', () => {
expect(board.addIssueTracker).toEqual(jasmine.any(Function));
board.addIssueTracker('test', 'test');
expect(board.issue_trackers.length).toEqual(1);
});
});
describe('Category', () => {
it('can be constructed', () => {
expect(new Category()).toBeTruthy();
});
});
describe('Column', () => {
it('can check for task limit', () => {
const column = new Column(
1, '', 1, 1, 1, [{
id: 1, title: '', description: '', color: '', due_date: '',
points: 1, position: 1, column_id: 1, columns: [], attachments: [],
assignees: [], categories: []
}]
);
expect(column.hasTaskLimit()).toEqual(true);
});
});
describe('Comment', () => {
it('can be constructed', () => {
expect(new Comment()).toBeTruthy();
});
});
describe('IssueTracker', () => {
it('can be constructed', () => {
expect(new IssueTracker()).toBeTruthy();
});
});
describe('Notification', () => {
it('can be constructed', () => {
expect(new Notification()).toBeTruthy();
});
});
describe('Task', () => {
it('can be constructed with values', () => {
let task = new Task(
1, '', '', '', '', 1, 1, 1,
[{ id: 1, text: '', user_id: 1, task_id: 1, timestamp: '', is_edited: 1 }],
[{
id: 1, filename: '', name: '', type: '', user_id: 1,
timestamp: '', task_id: 1
}],
[{
default_board_id: 1, email: '', id: 1, last_login: 1,
security_level: 1, user_option_id: 1, username: '',
board_access: [], collapsed: []
}],
[{ id: 1, name: '', default_task_color: '', board_id: 1 }]
);
});
});
describe('UserOptions', () => {
it('can be constructed', () => {
expect(new UserOptions()).toBeTruthy();
});
});
describe('User', () => {
it('can check if is admin', () => {
const user = new User();
expect(user.isAdmin()).toEqual(false);
});
it('can check if is board admin', () => {
const user = new User();
expect(user.isBoardAdmin()).toEqual(false);
});
it('can check if is any admin', () => {
const user = new User();
expect(user.isAnyAdmin()).toEqual(false);
});
});
});

View File

@ -1,22 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
Notification = require(path + 'notification.model.js').Notification;
describe('Notification', () => {
var notification;
beforeEach(() => {
notification = new Notification();
});
it('has a type string', () => {
expect(notification.type).to.be.a('string');
expect(notification.type).to.equal('');
});
it('has a text string', () => {
expect(notification.text).to.be.a('string');
expect(notification.text).to.equal('');
});
});

View File

@ -1,101 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
Task = require(path + 'task.model.js').Task;
describe('Task', () => {
var task;
beforeEach(() => {
task = new Task(1, 'title', 'desc', 'color', 'today', 3, 1, 1,
[{ id: 1, text: '', user_id: 1, task_id: 1 }],
[{
id: 1, filename: '', name: '', type: '',
user_id: 1, timestamp: '', task_id: 1
}],
[{
default_board_id: 0, email: '', id: 1,
last_login: null, security_level: 1,
user_option_id: 1, username: '',
board_access: [], collapsed: []
}],
[{ id: 1, name: '', default_task_color: '', board_id: 1 }]);
});
it('has sane defaults', () => {
task = new Task();
expect(task.id).to.equal(0);
expect(task.title).to.equal('');
expect(task.description).to.equal('');
expect(task.color).to.equal('#ffffe0');
expect(task.due_date).to.equal('');
expect(task.points).to.equal(0);
expect(task.position).to.equal(0);
expect(task.column_id).to.equal(0);
expect(task.comments.length).to.equal(0);
expect(task.attachments.length).to.equal(0);
expect(task.assignees.length).to.equal(0);
expect(task.categories.length).to.equal(0);
});
it('has an id', () => {
expect(task.id).to.be.a('number');
expect(task.id).to.equal(1);
});
it('has a title', () => {
expect(task.title).to.be.a('string');
expect(task.title).to.equal('title');
});
it('has a description', () => {
expect(task.description).to.be.a('string');
expect(task.description).to.equal('desc');
});
it('has a color', () => {
expect(task.color).to.be.a('string');
expect(task.color).to.equal('color');
});
it('has a due_date', () => {
expect(task.due_date).to.be.a('string');
expect(task.due_date).to.equal('today');
});
it('has points', () => {
expect(task.points).to.be.a('number');
expect(task.points).to.equal(3);
});
it('has a position', () => {
expect(task.position).to.be.a('number');
expect(task.position).to.equal(1);
});
it('has a column_id', () => {
expect(task.column_id).to.be.a('number');
expect(task.column_id).to.equal(1);
});
it('has comments', () => {
expect(task.comments).to.be.an('array');
expect(task.comments.length).to.equal(1);
});
it('has attachments', () => {
expect(task.attachments).to.be.an('array');
expect(task.attachments.length).to.equal(1);
});
it('has assignees', () => {
expect(task.assignees).to.be.an('array');
expect(task.assignees.length).to.equal(1);
});
it('has categories', () => {
expect(task.categories).to.be.an('array');
expect(task.categories.length).to.equal(1);
});
});

View File

@ -1,47 +0,0 @@
/* global expect */
var path = '../../../../build/shared/models/',
UserOptions = require(path + 'user-options.model.js').UserOptions;
describe('UserOptions', () => {
var userOptions;
beforeEach(() => {
userOptions = new UserOptions(1, false, false, false, true, 'es');
});
it('has sane defaults', () => {
userOptions = new UserOptions();
expect(userOptions.id).to.equal(0);
expect(userOptions.new_tasks_at_bottom).to.equal(true);
expect(userOptions.show_animations).to.equal(true);
expect(userOptions.show_assignee).to.equal(true);
expect(userOptions.multiple_tasks_per_row).to.equal(false);
expect(userOptions.language).to.equal('en');
});
it('has an id', () => {
expect(userOptions.id).to.be.a('number');
expect(userOptions.id).to.equal(1);
});
it('has boolean settings', () => {
expect(userOptions.new_tasks_at_bottom).to.be.a('boolean');
expect(userOptions.new_tasks_at_bottom).to.equal(false);
expect(userOptions.show_animations).to.be.a('boolean');
expect(userOptions.show_animations).to.equal(false);
expect(userOptions.show_assignee).to.be.a('boolean');
expect(userOptions.show_assignee).to.equal(false);
expect(userOptions.multiple_tasks_per_row).to.be.a('boolean');
expect(userOptions.multiple_tasks_per_row).to.equal(true);
});
it('has a language', () => {
expect(userOptions.language).to.be.a('string');
expect(userOptions.language).to.equal('es');
});
});

View File

@ -1,61 +0,0 @@
/* globals expect */
var path = '../../../../build/shared/models/',
User = require(path + 'user.model.js').User;
describe('User', () => {
var user;
beforeEach(() => {
user = new User();
});
it('has default_board_id', () => {
expect(user.default_board_id).to.be.a('number');
expect(user.default_board_id).to.equal(0);
});
it('has email', () => {
expect(user.email).to.be.a('string');
expect(user.email).to.equal('');
});
it('has id', () => {
expect(user.id).to.be.a('number');
expect(user.id).to.equal(0);
});
it('has last_login', () => {
expect(user.last_login).to.equal(null);
});
it('has security_level', () => {
expect(user.security_level).to.be.a('number');
expect(user.security_level).to.equal(4);
});
it('has user_option_id', () => {
expect(user.user_option_id).to.be.a('number');
expect(user.user_option_id).to.equal(0);
});
it('has username', () => {
expect(user.username).to.be.a('string');
expect(user.username).to.equal('');
});
it('has a method to check for admin', () => {
expect(user.isAdmin).to.be.a('function');
expect(user.isAdmin()).to.equal(false);
});
it('has a method to check for board admin', () => {
expect(user.isBoardAdmin).to.be.a('function');
expect(user.isBoardAdmin()).to.equal(false);
});
it('has a method to check for any admin', () => {
expect(user.isAnyAdmin).to.be.a('function');
expect(user.isAnyAdmin()).to.equal(false);
});
});

View File

@ -1,36 +0,0 @@
/* global expect NotificationsServiceMock */
var path = '../../../../build/shared/notifications/',
Notifications = require(path + 'notifications.component.js').Notifications;
describe('Notifications', () => {
var notifications,
notificationsService;
beforeEach(() => {
notificationsService = new NotificationsServiceMock();
notifications = new Notifications(notificationsService);
});
it('has an array of notes', () => {
expect(notifications.notes).to.be.an('array');
});
it('subscribes to note additions', () => {
notificationsService.add(true);
expect(notifications.notes[0]).to.equal(true);
});
it('hides a specific note', done => {
var note = { test: 'test note', type: '' };
notificationsService.add(note);
expect(notifications.notes.length).to.equal(1);
notifications.hide(note);
setTimeout(() => {
expect(notifications.notes.length).to.equal(0);
done();
}, 510);
});
});

View File

@ -0,0 +1,63 @@
import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
import { SharedModule } from '../../../../src/app/shared/shared.module';
import { NotificationsServiceMock } from '../../mocks';
import {
NotificationsService
} from '../../../../src/app/shared/notifications/notifications.service';
import {
Notifications
} from '../../../../src/app/shared/notifications/notifications.component';
describe('Notifications', () => {
let component: Notifications,
fixture: ComponentFixture<Notifications>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
SharedModule
],
providers: [{
provide: NotificationsService, useClass: NotificationsServiceMock
}]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(Notifications);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('can be constructed', () => {
expect(component).toBeTruthy();
});
it('subscribes to note add events', fakeAsync(done => {
const note = { type: 'info' };
(<any>component.notifications).addNote(note);
tick(4000);
expect(note.type).toEqual('info clicked');
}));
it('hides notes', fakeAsync(() => {
const hide = component['hide'].bind(component),
note = { type: 'test' };
component.notes = <any>[note];
hide({});
tick(4000);
expect(note.type).toEqual('test');
hide(note);
tick(4000);
expect(note.type).toEqual('test clicked');
}));
});

View File

@ -1,27 +0,0 @@
/* global expect */
var path = '../../../../build/shared/notifications/',
NotificationsService = require(path + 'notifications.service.js')
.NotificationsService;
describe('NotificationsService', () => {
var notificationsService;
beforeEach(() => {
notificationsService = new NotificationsService();
});
it('has an observable of added notes', () => {
expect(notificationsService.noteAdded).to.be.an('object');
expect(notificationsService.noteAdded.subscribe).to.be.a('function');
});
it('has a method to add notifications', done => {
notificationsService.noteAdded.subscribe(note => {
expect(note).to.equal(true);
done();
});
notificationsService.add(true);
});
});

View File

@ -1,32 +0,0 @@
/* global expect, HttpMock */
var path = '../../../../build/shared/strings/',
StringsService = require(path + 'strings.service.js').StringsService;
describe('StringsService', () => {
var stringsService;
beforeEach(() => {
stringsService = new StringsService(HttpMock);
});
it('has stringsChanged observable', () => {
expect(stringsService.stringsChanged).to.be.an('object');
});
it('loads a JSON file to get strings', done => {
var first = true;
stringsService.stringsChanged.subscribe(res => {
if (first) {
first = false;
return;
}
expect(res.endpoint).to.equal('strings/test.json');
done();
});
stringsService.loadStrings('test');
});
});

View File

@ -0,0 +1,57 @@
import { TestBed, getTestBed } from '@angular/core/testing'
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
import {
StringsService
} from '../../../../src/app/shared/strings/strings.service';
describe('StringsService', () => {
let injector: TestBed;
let service: StringsService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
RouterTestingModule.withRoutes([])
],
providers: [ StringsService ]
});
injector = getTestBed();
service = injector.get(StringsService);
httpMock = injector.get(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
})
it('should be created', () => {
expect(service).toBeTruthy();
});
it('loads strings by language', () => {
service.loadStrings('es');
testCall('strings/es.json', 'GET');
});
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: [{}, {}] });
}
};
});

View File

@ -1,48 +0,0 @@
/* global expect ConstantsMock RouterMock AuthServiceMock NotificationsServiceMock StringsServiceMock */
var path = '../../../../build/shared/top-nav/',
TopNav = require(path + 'top-nav.component.js').TopNav;
describe('TopNav', () => {
var topNav,
router;
beforeEach(() => {
router = new RouterMock();
topNav = new TopNav(ConstantsMock, router,
AuthServiceMock, new NotificationsServiceMock(),
StringsServiceMock);
});
it('has pageName', () => {
expect(topNav.pageName).to.be.a('string');
expect(topNav.pageName).to.equal('');
});
it('has version', () => {
expect(topNav.version).to.be.a('string');
expect(topNav.version).to.equal('TEST');
});
it('has username', () => {
expect(topNav.username).to.be.a('string');
expect(topNav.username).to.equal('tester');
});
it('allows a user to log out', () => {
expect(router.path).to.equal('test');
topNav.logout();
expect(router.path).to.equal('');
});
it('can tell if a route is active', () => {
expect(topNav.isActive('test')).to.equal(true);
expect(topNav.isActive('whatever')).to.equal(false);
});
it('can navigate to a new path', () => {
expect(router.path).to.equal('test');
topNav.navigateTo('newRoute');
expect(router.path).to.equal('/newRoute');
});
});

View File

@ -0,0 +1,73 @@
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 { TopNav } from '../../../../src/app/shared/top-nav/top-nav.component';
import {
Constants,
AuthService,
NotificationsService,
StringsService
} from '../../../../src/app/shared/services';
describe('TopNav', () => {
let component: TopNav,
fixture: ComponentFixture<TopNav>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
RouterTestingModule.withRoutes([]),
FormsModule
],
declarations: [ TopNav ],
providers: [
RouterTestingModule,
Constants,
AuthService,
NotificationsService,
StringsService
]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(TopNav);
component = fixture.componentInstance;
});
it('can be constructed', () => {
expect(component).toBeTruthy();
});
it('can log out a user', () => {
let called = false;
(<any>component.authService).logout = () => {
return { subscribe: fn => {
fn({ alerts: [{}] });
called = true;
} };
};
component.logout();
expect(called).toEqual(true);
});
it('checks if a route is active', () => {
const actual = component.isActive('nope');
expect(actual).toEqual(false);
});
it('can navigate to a target route', () => {
let spy = spyOn((<any>component).router, 'navigate');
component.navigateTo('test');
expect(spy).toHaveBeenCalledWith(['/test']);
});
});