Initial front end unit tests
This commit is contained in:
parent
7808ce18cd
commit
e4b1d93953
81
test/app/api-http.spec.js
Normal file
81
test/app/api-http.spec.js
Normal file
@ -0,0 +1,81 @@
|
||||
require('reflect-metadata/Reflect.js');
|
||||
|
||||
var chai = require('chai'),
|
||||
expect = chai.expect,
|
||||
path = '../../build/app.',
|
||||
apihttp = require(path + 'api-http.js'),
|
||||
ApiHttp = apihttp.ApiHttp,
|
||||
RxJs = require('rxjs/Rx');
|
||||
|
||||
global.localStorage = (function() {
|
||||
var storage = {};
|
||||
|
||||
return {
|
||||
getItem: (key) => {
|
||||
if (storage.hasOwnProperty(key)) {
|
||||
return storage[key];
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
},
|
||||
setItem: (key, value) => {
|
||||
storage[key] = value;
|
||||
},
|
||||
removeItem: (key) => {
|
||||
if (storage.hasOwnProperty(key)) {
|
||||
delete storage[key];
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
describe('ApiHttp', () => {
|
||||
var apiHttp;
|
||||
|
||||
beforeEach(() => {
|
||||
apiHttp = new ApiHttp();
|
||||
});
|
||||
|
||||
it('provides API_HTTP_PROVIDERS', () => {
|
||||
var provider = apihttp.API_HTTP_PROVIDERS;
|
||||
expect(provider).to.be.an('array');
|
||||
});
|
||||
|
||||
it('has JWT_KEY', () => {
|
||||
expect(apiHttp.JWT_KEY).to.equal('taskboard.jwt');
|
||||
});
|
||||
|
||||
it('injects headers', () => {
|
||||
global.localStorage.setItem('taskboard.jwt', 'testjwt');
|
||||
|
||||
var headers = apiHttp.getRequestOptionArgs().headers;
|
||||
|
||||
expect(headers._headersMap.get('Content-Type')[0])
|
||||
.to.equal('application/json');
|
||||
expect(headers._headersMap.get('Authorization')[0])
|
||||
.to.equal('testjwt');
|
||||
});
|
||||
|
||||
it('intercepts observable responses', () => {
|
||||
var response = {
|
||||
json: function() {
|
||||
return {
|
||||
data: ['testjwt']
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
apiHttp.intercept(RxJs.Observable.of(response))
|
||||
.map(response => {
|
||||
expect(global.localStorage.getItem('taskboard.jwt'))
|
||||
.to.equal('testjwt');
|
||||
});
|
||||
|
||||
apiHttp.intercept(RxJs.Observable.throw(null, response))
|
||||
.catch((err, caught) => {
|
||||
expect(global.localStorage.getItem('taskboard.jwt'))
|
||||
.to.equal('');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
11
test/app/app.routes.spec.js
Normal file
11
test/app/app.routes.spec.js
Normal file
@ -0,0 +1,11 @@
|
||||
var chai = require('chai'),
|
||||
expect = chai.expect,
|
||||
path = '../../build/',
|
||||
routes = require(path + 'app.routes.js');
|
||||
|
||||
describe('Routes', () => {
|
||||
it('provides APP_ROUTER_PROVIDERS', () => {
|
||||
expect(routes.APP_ROUTER_PROVIDERS).to.be.an('array');
|
||||
});
|
||||
});
|
||||
|
37
test/app/mocks.js
Normal file
37
test/app/mocks.js
Normal file
@ -0,0 +1,37 @@
|
||||
var RxJs = require('rxjs/Rx');
|
||||
|
||||
global.ConstantsMock = {
|
||||
VERSION: 'TEST'
|
||||
};
|
||||
|
||||
global.RouterMock = function() {
|
||||
return {
|
||||
path: 'test',
|
||||
url: 'test',
|
||||
navigate: function(arr) {
|
||||
this.path = arr[0];
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
global.AuthServiceMock = {
|
||||
userChanged: RxJs.Observable.of({
|
||||
username: 'tester'
|
||||
}),
|
||||
logout: () => {
|
||||
return RxJs.Observable.of({
|
||||
alerts: [ 'Logged out' ]
|
||||
});
|
||||
},
|
||||
authenticate: () => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
global.NotificationMock = {
|
||||
note: '',
|
||||
add: (note) => {
|
||||
this.note = note;
|
||||
}
|
||||
};
|
||||
|
18
test/app/shared/auth/auth.guard.spec.js
Normal file
18
test/app/shared/auth/auth.guard.spec.js
Normal file
@ -0,0 +1,18 @@
|
||||
/* global AuthServiceMock */
|
||||
var chai = require('chai'),
|
||||
expect = chai.expect,
|
||||
path = '../../../../build/shared/auth/',
|
||||
AuthGuard = require(path + 'auth.guard.js').AuthGuard;
|
||||
|
||||
describe('AuthGuard', () => {
|
||||
var authGuard;
|
||||
|
||||
beforeEach(() => {
|
||||
authGuard = new AuthGuard(AuthServiceMock);
|
||||
});
|
||||
|
||||
it('checks a route can activate via the auth service', () => {
|
||||
expect(authGuard.canActivate()).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
23
test/app/shared/models/notification.spec.js
Normal file
23
test/app/shared/models/notification.spec.js
Normal file
@ -0,0 +1,23 @@
|
||||
var chai = require('chai'),
|
||||
expect = chai.expect,
|
||||
path = '../../../../build/shared/models/',
|
||||
Notification = require(path + 'notification.model.js').Notification;
|
||||
|
||||
describe('Notification', () => {
|
||||
var notification;
|
||||
|
||||
beforeEach(() => {
|
||||
notification = new Notification();
|
||||
});
|
||||
|
||||
it('has type', () => {
|
||||
expect(notification.type).to.be.a('string');
|
||||
expect(notification.type).to.equal('');
|
||||
});
|
||||
|
||||
it('has text', () => {
|
||||
expect(notification.text).to.be.a('string');
|
||||
expect(notification.text).to.equal('');
|
||||
});
|
||||
});
|
||||
|
47
test/app/shared/models/user.spec.js
Normal file
47
test/app/shared/models/user.spec.js
Normal file
@ -0,0 +1,47 @@
|
||||
var chai = require('chai'),
|
||||
expect = chai.expect,
|
||||
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(3);
|
||||
});
|
||||
|
||||
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('');
|
||||
});
|
||||
});
|
||||
|
52
test/app/shared/top-nav/top-nav.spec.js
Normal file
52
test/app/shared/top-nav/top-nav.spec.js
Normal file
@ -0,0 +1,52 @@
|
||||
/* global ConstantsMock RouterMock AuthServiceMock NotificationMock */
|
||||
|
||||
require('../../mocks.js');
|
||||
|
||||
var chai = require('chai'),
|
||||
expect = chai.expect,
|
||||
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, NotificationMock);
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user