Skip to content

Commit

Permalink
Refactored tests into a simpler structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Jun 30, 2015
1 parent 6b71139 commit a573134
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dist/ngJwtAuth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ declare module NgJwtAuth {
private $http;
private config;
static $inject: string[];
constructor($http: ng.IHttpService, config: any);
constructor(_$http: ng.IHttpService, _config: any);
private getLoginEndpoint();
private getTokenExchangeEndpoint();
private getRefreshEndpoint();
Expand Down
2 changes: 1 addition & 1 deletion dist/ngJwtAuth.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 78 additions & 21 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,104 @@

var expect = chai.expect;

describe('Service Provider Tests', () => {

var ngJwtAuthServiceProviderObj:NgJwtAuth.NgJwtAuthServiceProvider;
var defaultAuthServiceObj:NgJwtAuth.NgJwtAuthService;
describe('Default configuration', function () {

describe('Configuration', () => {
var defaultAuthServiceProvider:NgJwtAuth.NgJwtAuthServiceProvider;
var defaultAuthService:NgJwtAuth.NgJwtAuthService;

beforeEach(() => {

beforeEach(function () {
module('ngJwtAuth', (_ngJwtAuthServiceProvider_) => {
defaultAuthServiceProvider = _ngJwtAuthServiceProvider_; //register injection of service provider
});

angular.module("providers", ['ngJwtAuth']); //require the module as dependency
module("providers"); //mock the depending module
});

module((ngJwtAuthServiceProvider) => {
ngJwtAuthServiceProviderObj = ngJwtAuthServiceProvider; //register injection of service provider
});
it('should have the default endpoints', () => {
expect((<any>defaultAuthServiceProvider).config.apiEndpoints.base).to.equal('/api/auth');
expect((<any>defaultAuthServiceProvider).config.apiEndpoints.login).to.equal('/login');
expect((<any>defaultAuthServiceProvider).config.apiEndpoints.refresh).to.equal('/refresh');
});

inject(function(_ngJwtAuthService_){
defaultAuthServiceObj = _ngJwtAuthService_;
}); //complete injection
});
beforeEach(()=>{
inject(function(_ngJwtAuthService_){
defaultAuthService = _ngJwtAuthService_;
})
});

it('should have the default login endpoint', function() {
expect((<any>defaultAuthService).getLoginEndpoint()).to.equal('/api/auth/login');
});

});

describe('Custom configuration', function () {

var authServiceProvider:NgJwtAuth.NgJwtAuthServiceProvider;
var customAuthService:NgJwtAuth.NgJwtAuthService;

beforeEach(() => {

it('should configure api endpoints', function () {
module('ngJwtAuth', (_ngJwtAuthServiceProvider_) => {
authServiceProvider = _ngJwtAuthServiceProvider_; //register injection of service provider

ngJwtAuthServiceProviderObj.setApiEndpoints({
authServiceProvider.setApiEndpoints({
base: 'mock/base/path/',
login: 'to/login',
refresh: 'to/refresh'
});

expect((<any>ngJwtAuthServiceProviderObj).config.apiEndpoints.base).to.equal('mock/base/path/');
expect((<any>ngJwtAuthServiceProviderObj).config.apiEndpoints.login).to.equal('to/login');
expect((<any>ngJwtAuthServiceProviderObj).config.apiEndpoints.refresh).to.equal('to/refresh');
});

it('should have the default endpoints', function() {
expect((<any>defaultAuthServiceObj).getLoginEndpoint()).to.equal('/api/auth/login');
});

it('should have the custom endpoints', () => {
expect((<any>authServiceProvider).config.apiEndpoints.base).to.equal('mock/base/path/');
expect((<any>authServiceProvider).config.apiEndpoints.login).to.equal('to/login');
expect((<any>authServiceProvider).config.apiEndpoints.refresh).to.equal('to/refresh');
});

beforeEach(()=>{
inject(function(_ngJwtAuthService_){
customAuthService = _ngJwtAuthService_;
})
});

it('should have the configured login endpoint', function() {
expect((<any>customAuthService).getLoginEndpoint()).to.equal('mock/base/path/to/login');
});

});


describe('Service tests', () => {

var ngJwtAuthService:NgJwtAuth.NgJwtAuthService;

beforeEach(() => {

module('ngJwtAuth');

inject(($injector) => {
if (!ngJwtAuthService){ //dont rebind, so each test gets the singleton
ngJwtAuthService = $injector.get('ngJwtAuthService');
}
});

});

it('should be an injectable service', () => {

return expect(ngJwtAuthService).to.be.an('object');

});

it('should retrieve a json web token', () => {

ngJwtAuthService.authenticate('joe.bloggs@example.com', 'password');

});


});

0 comments on commit a573134

Please sign in to comment.