Skip to content

Commit

Permalink
Merge pull request #225 from IT-Academy-BCN/lara188
Browse files Browse the repository at this point in the history
cookies mock
  • Loading branch information
laradelrio authored Feb 7, 2024
2 parents f4710e7 + 5210aaa commit 6642089
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
29 changes: 15 additions & 14 deletions src/app/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import { AuthService } from "./auth.service";
import { HttpClient } from "@angular/common/http";
import { Router } from "@angular/router";
import { CookieService } from "ngx-cookie-service";
import { of, throwError } from "rxjs";

describe("AuthService", () => {
let authService: AuthService;
let httpClientMock: any;
let routerMock: any;

let cookieService: CookieService;
beforeEach(() => {
httpClientMock = {
post: jest.fn(),
};
routerMock = {
navigate: jest.fn(),
};
authService = new AuthService(httpClientMock, routerMock);
authService = new AuthService(httpClientMock, routerMock, cookieService);

// Mock localStorage
const localStorageMock = (function () {
let store : any= {};
// Mock Cookies
const cookiesMock = (function () {
let store: any = {};
return {
getItem: jest.fn((key) => store[key] || null),
setItem: jest.fn((key, value) => {
store[key] = value.toString();
get: jest.fn((name) => {
return name + '=' + store[name];
}),
removeItem: jest.fn((key) => {
delete store[key];
set: jest.fn((name, value) => {
store[name] = value;
}),
clear: jest.fn(() => {
store = {};
delete: jest.fn((name) => {
delete store[name];
}),
};
})();
Object.defineProperty(window, "localStorage", {
value: localStorageMock,
Object.defineProperty(window.document, "cookiesFile", {
writable: true,
value: cookiesMock,
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class AuthService {
}

/**
* Creates a new anonymous user if there is no user in the local storage.
* Creates a new anonymous user if there is no user in the cookies.
*/
public get currentUser(): User {
if (this.userSubject.value === null) {
Expand Down

0 comments on commit 6642089

Please sign in to comment.