Skip to content

Commit

Permalink
Add additional tests and run on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck committed Dec 20, 2022
1 parent 2f2d3f8 commit 7f8b93f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"build": "ng build angular-jwt --configuration production",
"postbuild": "cp ./README.md ./CHANGELOG.md ./LICENSE ./dist/angular-jwt/",
"test": "ng test",
"test:ci": "ng test angular2-jwt --no-watch --no-progress --browsers=ChromeHeadlessCI --code-coverage",
"test:ci": "npm run test:angular-jwt && npm run test:angular2-jwt",
"test:angular-jwt": "ng test angular-jwt --no-watch --no-progress --browsers=ChromeHeadlessCI --code-coverage",
"test:angular2-jwt": "ng test angular2-jwt --no-watch --no-progress --browsers=ChromeHeadlessCI --code-coverage",
"test:types": "tsd projects/angular-jwt/src",
"lint": "ng lint",
"e2e": "ng e2e",
Expand Down
75 changes: 75 additions & 0 deletions projects/angular-jwt/src/lib/jwthelper.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe('Example HttpService: with simple based tokken getter', () => {
],
});
service = TestBed.inject(JwtHelperService);

tokenGetter.calls.reset();
});

it('should return null when tokenGetter returns null', () => {
Expand All @@ -40,6 +42,42 @@ describe('Example HttpService: with simple based tokken getter', () => {

expect(() => service.decodeToken()).toThrow();
});

it('should call the tokenGetter when no token passed', () => {
tokenGetter.and.returnValue('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

const result = service.decodeToken();

expect(tokenGetter).toHaveBeenCalled();
expect(result.name).toBe('John Doe');
});

it('should call the tokenGetter when undefined is passed', () => {
tokenGetter.and.returnValue('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

const result = service.decodeToken(undefined);

expect(tokenGetter).toHaveBeenCalled();
expect(result.name).toBe('John Doe');
});

it('should not call the tokenGetter when token passed', () => {
tokenGetter.and.returnValue(null);

const result = service.decodeToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

expect(tokenGetter).not.toHaveBeenCalled();
expect(result.name).toBe('John Doe');
});

it('should not call the tokenGetter when token passed as empty string', () => {
tokenGetter.and.returnValue('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

const result = service.decodeToken('');

expect(tokenGetter).not.toHaveBeenCalled();
expect(result).toBeNull();
});
});

describe('Example HttpService: with a promise based tokken getter', () => {
Expand All @@ -59,6 +97,7 @@ describe('Example HttpService: with a promise based tokken getter', () => {
],
});
service = TestBed.inject(JwtHelperService);
tokenGetter.calls.reset();
});

it('should return null when tokenGetter returns null', async () => {
Expand All @@ -84,5 +123,41 @@ describe('Example HttpService: with a promise based tokken getter', () => {

await expectAsync(service.decodeToken()).toBeResolvedTo(jasmine.anything());
});

it('should call the tokenGetter when no token passed', async () => {
tokenGetter.and.resolveTo('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

const result = await service.decodeToken();

expect(tokenGetter).toHaveBeenCalled();
expect(result.name).toBe('John Doe');
});

it('should call the tokenGetter when undefined is passed', async () => {
tokenGetter.and.resolveTo('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

const result = await service.decodeToken(undefined);

expect(tokenGetter).toHaveBeenCalled();
expect(result.name).toBe('John Doe');
});

it('should not call the tokenGetter when token passed', async () => {
tokenGetter.and.resolveTo(null);

const result = await service.decodeToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

expect(tokenGetter).not.toHaveBeenCalled();
expect(result.name).toBe('John Doe');
});

it('should not call the tokenGetter when token passed as empty string', async () => {
tokenGetter.and.resolveTo('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');

const result = await service.decodeToken('');

expect(tokenGetter).not.toHaveBeenCalled();
expect(result).toBeNull();
});
});

0 comments on commit 7f8b93f

Please sign in to comment.