Update app tests and refactor api-http

This commit is contained in:
Matthew Ross 2017-04-28 19:15:43 -04:00
parent 0c7179fea4
commit 263ca2498b
5 changed files with 77 additions and 24 deletions

View File

@ -83,26 +83,30 @@ export class ApiHttp extends Http {
intercept(observable: Observable<Response>): Observable<Response> { intercept(observable: Observable<Response>): Observable<Response> {
return observable return observable
.map((res: Response) => { .map(this.handleResponse)
let response: ApiResponse = res.json(); .catch(this.handleError);
}
if (response.data) { private handleResponse(res: Response): Response {
localStorage.setItem(this.JWT_KEY, response.data[0]); let response: ApiResponse = res.json();
}
return res; if (response.data) {
}) localStorage.setItem(this.JWT_KEY, response.data[0]);
.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);
}
return Observable.throw(err); return res;
}); }
private handleError(err: any, source: any): Observable<any> {
// 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);
} }
} }

View File

@ -1,14 +1,14 @@
/* globals expect RxJs localStorage */ /* globals expect RxJs localStorage RouterMock */
var path = '../../build/app.', var path = '../../build/',
apihttp = require(path + 'api-http.js'), apihttp = require(path + 'app.api-http.js'),
ApiHttp = apihttp.ApiHttp; ApiHttp = apihttp.ApiHttp;
describe('ApiHttp', () => { describe('ApiHttp', () => {
var apiHttp; var apiHttp;
beforeEach(() => { beforeEach(() => {
apiHttp = new ApiHttp(); apiHttp = new ApiHttp(null, null, new RouterMock());
}); });
it('provides API_HTTP_PROVIDERS', () => { it('provides API_HTTP_PROVIDERS', () => {
@ -52,5 +52,22 @@ describe('ApiHttp', () => {
.to.equal(''); .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);
});
}); });

View File

@ -2,6 +2,10 @@
var path = '../../build/', var path = '../../build/',
AppModule = require(path + 'app.module.js').AppModule; AppModule = require(path + 'app.module.js').AppModule;
try {
require(path + 'main.js');
} catch (ex) {}
describe('Module', () => { describe('Module', () => {
it('provides an AppModule class', () => { it('provides an AppModule class', () => {
var appModule = new AppModule(); var appModule = new AppModule();

View File

@ -4,17 +4,45 @@ var path = '../../../build/board/',
describe('BoardDisplay', () => { describe('BoardDisplay', () => {
var board, var board,
title; title,
router;
beforeEach(() => { beforeEach(() => {
title = new TitleMock(); title = new TitleMock();
router = new RouterMock();
board = new BoardDisplay(title, new RouterMock(), ActivatedRouteMock, board = new BoardDisplay(title, router, ActivatedRouteMock,
AuthServiceMock, BoardServiceMock); AuthServiceMock, BoardServiceMock);
}); });
it('sets the title when contstructed', () => { it('sets the title when contstructed', () => {
expect(title.getTitle()).to.equal('TaskBoard - Kanban App'); 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);
});
}); });

View File

@ -28,7 +28,7 @@ describe('Login', () => {
expect(login.remember).to.equal(false); expect(login.remember).to.equal(false);
}); });
it('has a isSubmitted property', () => { it('has an isSubmitted property', () => {
expect(login.isSubmitted) .to.equal(false); expect(login.isSubmitted) .to.equal(false);
}); });