Skip to content

Commit

Permalink
Merge pull request #19088 from abpframework/sinan/test
Browse files Browse the repository at this point in the history
Add test to remember-me.service and fix return value in service
  • Loading branch information
masum-ulu authored Feb 26, 2024
2 parents 5785bbd + 07bdf2e commit a6b62d7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class RememberMeService {
}

get() {
return Boolean(JSON.parse(this.localStorageService.getItem(this.#rememberMe)));
return Boolean(JSON.parse(this.localStorageService.getItem(this.#rememberMe) || 'false'));
}

getFromToken(accessToken: string) {
Expand Down
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);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LoaderBarComponent } from '../components/loader-bar/loader-bar.componen

describe('LoaderBarComponent', () => {
let spectator: Spectator<LoaderBarComponent>;
let router: Router;
const events$ = new Subject();

const createComponent = createComponentFactory({
Expand Down

0 comments on commit a6b62d7

Please sign in to comment.