diff --git a/src/app/app.api-http.ts b/src/app/app.api-http.ts index 68009a4..59c5343 100644 --- a/src/app/app.api-http.ts +++ b/src/app/app.api-http.ts @@ -83,26 +83,30 @@ export class ApiHttp extends Http { intercept(observable: Observable): Observable { return observable - .map((res: Response) => { - let response: ApiResponse = res.json(); + .map(this.handleResponse) + .catch(this.handleError); + } - if (response.data) { - localStorage.setItem(this.JWT_KEY, response.data[0]); - } + private handleResponse(res: Response): Response { + let response: ApiResponse = res.json(); - return res; - }) - .catch((err, source) => { - // 401 for invalid token, 400 for no token, and convert - // url to string in case it is null. - if ((err.status === 401 || err.status === 400) && - (err.url + '').indexOf('login') === -1) { - this.router.navigate(['']); - localStorage.removeItem(this.JWT_KEY); - } + if (response.data) { + localStorage.setItem(this.JWT_KEY, response.data[0]); + } - return Observable.throw(err); - }); + return res; + } + + private handleError(err: any, source: any): Observable { + // 401 for invalid token, 400 for no token, and convert + // url to string in case it is null. + if ((err.status === 401 || err.status === 400) && + (err.url + '').indexOf('login') === -1) { + this.router.navigate(['']); + localStorage.removeItem(this.JWT_KEY); + } + + return Observable.throw(err); } } diff --git a/test/app/app.api-http.spec.js b/test/app/app.api-http.spec.js index 8831c24..2a0f70a 100644 --- a/test/app/app.api-http.spec.js +++ b/test/app/app.api-http.spec.js @@ -1,14 +1,14 @@ -/* globals expect RxJs localStorage */ +/* globals expect RxJs localStorage RouterMock */ -var path = '../../build/app.', - apihttp = require(path + 'api-http.js'), +var path = '../../build/', + apihttp = require(path + 'app.api-http.js'), ApiHttp = apihttp.ApiHttp; describe('ApiHttp', () => { var apiHttp; beforeEach(() => { - apiHttp = new ApiHttp(); + apiHttp = new ApiHttp(null, null, new RouterMock()); }); it('provides API_HTTP_PROVIDERS', () => { @@ -52,5 +52,22 @@ describe('ApiHttp', () => { .to.equal(''); }); }); + + it('handles valid responses', () => { + apiHttp.handleResponse(new ResponseMock()); + + expect(localStorage.getItem('taskboard.jwt')).to.equal('jwt'); + }); + + it('handles error responses', () => { + let error = { + status: 401, + url: '' + }; + + apiHttp.handleError(error, null); + + expect(localStorage.getItem('taskboard.jwt')).to.equal(null); + }); }); diff --git a/test/app/app.module.spec.js b/test/app/app.module.spec.js index ff4ff51..fc5bb20 100644 --- a/test/app/app.module.spec.js +++ b/test/app/app.module.spec.js @@ -2,6 +2,10 @@ var path = '../../build/', AppModule = require(path + 'app.module.js').AppModule; +try { + require(path + 'main.js'); +} catch (ex) {} + describe('Module', () => { it('provides an AppModule class', () => { var appModule = new AppModule(); diff --git a/test/app/board/board.component.spec.js b/test/app/board/board.component.spec.js index 217feee..da3d114 100644 --- a/test/app/board/board.component.spec.js +++ b/test/app/board/board.component.spec.js @@ -4,17 +4,45 @@ var path = '../../../build/board/', describe('BoardDisplay', () => { var board, - title; + title, + router; beforeEach(() => { title = new TitleMock(); + router = new RouterMock(); - board = new BoardDisplay(title, new RouterMock(), ActivatedRouteMock, + board = new BoardDisplay(title, router, ActivatedRouteMock, AuthServiceMock, BoardServiceMock); }); it('sets the title when contstructed', () => { expect(title.getTitle()).to.equal('TaskBoard - Kanban App'); }); + + it('implements ngOnOnit', () => { + board.boardNavId = '0'; + board.ngOnInit(); + expect(board.boardNavId).to.equal('0'); + + board.boardNavId = null; + board.ngOnInit(); + board.goToBoard(); + expect(board.boardNavId).to.equal(null); + + board.activeUser = { default_board_id: '2' }; + board.ngOnInit(); + + expect(board.boardNavId).to.equal('2'); + expect(router.path).to.equal('/boards/2'); + }); + + it('has a function to check for boards', () => { + expect(board.noBoards()).to.equal(true); + + board.loading = false; + board.boards = [{}]; + + expect(board.noBoards()).to.equal(false); + }); }); diff --git a/test/app/login/login.component.spec.js b/test/app/login/login.component.spec.js index 17c4c59..a593dfd 100644 --- a/test/app/login/login.component.spec.js +++ b/test/app/login/login.component.spec.js @@ -28,7 +28,7 @@ describe('Login', () => { expect(login.remember).to.equal(false); }); - it('has a isSubmitted property', () => { + it('has an isSubmitted property', () => { expect(login.isSubmitted) .to.equal(false); });