From e4b1d9395333b40816ba7fa92c1e14aaa7d338ab Mon Sep 17 00:00:00 2001 From: kiswa Date: Sun, 31 Jul 2016 20:02:18 +0000 Subject: [PATCH] Initial front end unit tests --- test/app/api-http.spec.js | 81 +++++++++++++++++++++ test/app/app.routes.spec.js | 11 +++ test/app/mocks.js | 37 ++++++++++ test/app/shared/auth/auth.guard.spec.js | 18 +++++ test/app/shared/models/notification.spec.js | 23 ++++++ test/app/shared/models/user.spec.js | 47 ++++++++++++ test/app/shared/top-nav/top-nav.spec.js | 52 +++++++++++++ test/app/todo | 0 8 files changed, 269 insertions(+) create mode 100644 test/app/api-http.spec.js create mode 100644 test/app/app.routes.spec.js create mode 100644 test/app/mocks.js create mode 100644 test/app/shared/auth/auth.guard.spec.js create mode 100644 test/app/shared/models/notification.spec.js create mode 100644 test/app/shared/models/user.spec.js create mode 100644 test/app/shared/top-nav/top-nav.spec.js delete mode 100644 test/app/todo diff --git a/test/app/api-http.spec.js b/test/app/api-http.spec.js new file mode 100644 index 0000000..98573ef --- /dev/null +++ b/test/app/api-http.spec.js @@ -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(''); + }); + }); +}); + diff --git a/test/app/app.routes.spec.js b/test/app/app.routes.spec.js new file mode 100644 index 0000000..88f856a --- /dev/null +++ b/test/app/app.routes.spec.js @@ -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'); + }); +}); + diff --git a/test/app/mocks.js b/test/app/mocks.js new file mode 100644 index 0000000..4a72969 --- /dev/null +++ b/test/app/mocks.js @@ -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; + } +}; + diff --git a/test/app/shared/auth/auth.guard.spec.js b/test/app/shared/auth/auth.guard.spec.js new file mode 100644 index 0000000..c5fcc27 --- /dev/null +++ b/test/app/shared/auth/auth.guard.spec.js @@ -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); + }); +}); + diff --git a/test/app/shared/models/notification.spec.js b/test/app/shared/models/notification.spec.js new file mode 100644 index 0000000..2dde2d7 --- /dev/null +++ b/test/app/shared/models/notification.spec.js @@ -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(''); + }); +}); + diff --git a/test/app/shared/models/user.spec.js b/test/app/shared/models/user.spec.js new file mode 100644 index 0000000..43e8e39 --- /dev/null +++ b/test/app/shared/models/user.spec.js @@ -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(''); + }); +}); + diff --git a/test/app/shared/top-nav/top-nav.spec.js b/test/app/shared/top-nav/top-nav.spec.js new file mode 100644 index 0000000..7a03a10 --- /dev/null +++ b/test/app/shared/top-nav/top-nav.spec.js @@ -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'); + }); +}); + diff --git a/test/app/todo b/test/app/todo deleted file mode 100644 index e69de29..0000000