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> {
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<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.',
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);
});
});

View File

@ -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();

View File

@ -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);
});
});

View File

@ -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);
});