auth.test.ts 631 B

123456789101112131415161718192021
  1. import { beforeEach, describe, expect, it, vi, type Mock } from "vitest";
  2. import { getToken } from "../../src/services/auth";
  3. Storage.prototype.getItem = vi.fn();
  4. Storage.prototype.setItem = vi.fn();
  5. describe("Auth Service", () => {
  6. beforeEach(() => {
  7. vi.clearAllMocks();
  8. });
  9. describe("getToken", () => {
  10. it("should fetch and return a token", () => {
  11. (Storage.prototype.getItem as Mock).mockReturnValue("newToken");
  12. const data = getToken();
  13. expect(localStorage.getItem).toHaveBeenCalledWith("token"); // Used to set Authorization header
  14. expect(data).toEqual("newToken");
  15. });
  16. });
  17. });