Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to remember-me.service and fix return value in service #19088

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
masum-ulu marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading