-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19088 from abpframework/sinan/test
Add test to remember-me.service and fix return value in service
- Loading branch information
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
npm/ng-packs/packages/oauth/src/lib/tests/remember-me.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { SpectatorService, SpyObject, createServiceFactory } from "@ngneat/spectator/jest"; | ||
import { RememberMeService } from "../services/remember-me.service"; | ||
import { AbpLocalStorageService } from "@abp/ng.core"; | ||
|
||
|
||
|
||
describe('RememberMeService', () => { | ||
const key = 'remember_me'; | ||
let spectator: SpectatorService<RememberMeService>; | ||
let rememberMeService: RememberMeService; | ||
let abpLocalStorageService: SpyObject<AbpLocalStorageService>; | ||
|
||
const createService = createServiceFactory({ | ||
service: RememberMeService, | ||
mocks: [AbpLocalStorageService] | ||
}); | ||
|
||
|
||
beforeEach(() => { | ||
spectator = createService(); | ||
rememberMeService = spectator.inject(RememberMeService); | ||
abpLocalStorageService = spectator.inject(AbpLocalStorageService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(1).toBe(1); | ||
expect(rememberMeService).toBeTruthy(); | ||
expect(abpLocalStorageService).toBeTruthy(); | ||
}); | ||
|
||
it('should set remember me', () => { | ||
rememberMeService.set(true); | ||
expect(abpLocalStorageService.setItem).toHaveBeenCalledWith(key, 'true'); | ||
expect(abpLocalStorageService.setItem).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should remove remember me', () => { | ||
rememberMeService.remove(); | ||
expect(abpLocalStorageService.removeItem).toHaveBeenCalledWith(key); | ||
expect(abpLocalStorageService.removeItem).toBeCalledTimes(1); | ||
}); | ||
|
||
it('if notting has ben setted, it should return false value', () => { | ||
expect(rememberMeService.get()).toBe(false); | ||
}); | ||
|
||
it('should return true value', () => { | ||
abpLocalStorageService.getItem.mockReturnValueOnce('true'); | ||
expect(rememberMeService.get()).toBe(true); | ||
}); | ||
|
||
it('should return false value', () => { | ||
abpLocalStorageService.getItem.mockReturnValueOnce('false'); | ||
expect(rememberMeService.get()).toBe(false); | ||
}); | ||
|
||
it('should return true when parsed token is setted to true', () => { | ||
const data = { "remember_me": "True" }; | ||
const base64_encoded = btoa(JSON.stringify(data)); | ||
const tokenWithValueTrue = "random." + base64_encoded + ".random"; | ||
expect(rememberMeService.getFromToken(tokenWithValueTrue)).toBe(true); | ||
}); | ||
|
||
it('should return false when value is not setted(undefined)', () => { | ||
const data = {}; | ||
const base64_encoded = btoa(JSON.stringify(data)); | ||
const tokenWithValueTrue = "random." + base64_encoded + ".random"; | ||
expect(rememberMeService.getFromToken(tokenWithValueTrue)).toBe(false); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters