Skip to content

Commit

Permalink
testing for register method works
Browse files Browse the repository at this point in the history
  • Loading branch information
valerioprds committed Nov 8, 2023
1 parent b5a3625 commit 6f0ffba
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/app/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { AuthService } from "./auth.service";
import { TestScheduler } from "rxjs/testing";
import { HttpClient } from "@angular/common/http";
import { Router } from "@angular/router";
import { of } from "rxjs";
import { TestScheduler } from "rxjs/testing";

// Mocking the dependencies: HttpClient and Router
jest.mock("@angular/common/http", () => ({
HttpClient: jest.fn().mockImplementation(() => ({
get: jest.fn(),
})),
}));
jest.mock("@angular/router", () => ({
Router: jest.fn().mockImplementation(() => ({
navigate: jest.fn(),
})),
}));

describe("AuthService", () => {
let authService: AuthService;
Expand All @@ -11,22 +21,35 @@ describe("AuthService", () => {
let testScheduler: TestScheduler;

beforeEach(() => {
httpClientMock = jasmine.createSpyObj("HttpClient", ["get"]);
routerMock = jasmine.createSpyObj("Router", ["navigate"]);
// Since HttpClient and Router are now mocked, we can require them here
const { HttpClient } = require("@angular/common/http");
const { Router } = require("@angular/router");

// Mocking local storage
spyOn(localStorage, "setItem");
spyOn(localStorage, "getItem").and.returnValue(null);
// Initialize the mocks
httpClientMock = new HttpClient();
routerMock = new Router();

// Mock the local storage methods
Object.defineProperty(window, "localStorage", {
value: {
setItem: jest.fn(),
getItem: jest.fn().mockReturnValue(null), // Default to 'null' to simulate empty local storage
removeItem: jest.fn(),
},
writable: true,
});

// Create the AuthService instance with the mocked HttpClient and Router
authService = new AuthService(httpClientMock, routerMock);

// Create a new TestScheduler for each test
testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
});

it("should simulate a successful registration", () => {
// Dummy user data that we expect to register
const dummyUser = {
dni: "01875580E",
email: "julia123@gmail.cat",
Expand All @@ -38,20 +61,25 @@ describe("AuthService", () => {
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
};

httpClientMock.get.and.returnValue(of(dummyUser));
// Set up the HttpClient mock to return our dummy user data when get is called
httpClientMock.get.mockReturnValue(of(dummyUser));

testScheduler.run(({ expectObservable }) => {
const expectedMarble = "a"; // Use the appropriate marble diagram
// Marble diagram representing the expected observable stream
const expectedMarble = "(a|)"; // Emission 'a' followed by completion '|'
// Values that the 'a' in the marble diagram represents
const expectedValues = { a: dummyUser };

// Call the register method, which returns an Observable
const obs$ = authService.register(dummyUser);

// Assert that the observable behaves as expected
expectObservable(obs$).toBe(expectedMarble, expectedValues);
});

// You can also expect that setLocalStorage was called if necessary
// Check that localStorage.setItem was called (if your service does that upon registration)
expect(localStorage.setItem).toHaveBeenCalled();
});

// Add more test cases as needed
// Add more test cases as needed...
});

0 comments on commit 6f0ffba

Please sign in to comment.