storage.test.ts 1.1 KB

12345678910111213141516171819202122232425262728
  1. import { beforeEach, describe, expect, it, vi, type Mock } from "vitest";
  2. import { getCachedConfig } from "../../src/utils/storage";
  3. describe("getCachedConfig", () => {
  4. beforeEach(() => {
  5. // Clear all instances and calls to constructor and all methods
  6. Storage.prototype.getItem = vi.fn();
  7. });
  8. it("should return an empty object when local storage is null or undefined", () => {
  9. (Storage.prototype.getItem as Mock).mockReturnValue(null);
  10. expect(getCachedConfig()).toEqual({});
  11. (Storage.prototype.getItem as Mock).mockReturnValue(undefined);
  12. expect(getCachedConfig()).toEqual({});
  13. });
  14. it("should return an empty object when local storage has invalid JSON", () => {
  15. (Storage.prototype.getItem as Mock).mockReturnValue("invalid JSON");
  16. expect(getCachedConfig()).toEqual({});
  17. });
  18. it("should return parsed object when local storage has valid JSON", () => {
  19. const validJSON = '{"key":"value"}';
  20. (Storage.prototype.getItem as Mock).mockReturnValue(validJSON);
  21. expect(getCachedConfig()).toEqual({ key: "value" });
  22. });
  23. });