Skip to content

Commit

Permalink
Add storage proxy test
Browse files Browse the repository at this point in the history
  • Loading branch information
zapo committed Dec 18, 2024
1 parent 890ebb6 commit 93a4c66
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/core/regs/storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { LocalStorageProxy } from "./storage";

describe("LocalStorageProxy", () => {
let windowSpy;

let localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
};

beforeEach(() => {
windowSpy = jest.spyOn(window, "window", "get");
windowSpy.mockImplementation(() => ({
localStorage: localStorageMock,
}));
});

afterEach(() => {
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockClear();
localStorageMock.removeItem.mockClear();
windowSpy.mockRestore();
});

it("proxies to underlying storage when consent granted", () => {
const storage = new LocalStorageProxy({ deviceAccess: true });

storage.getItem("key");
expect(localStorageMock.getItem).toHaveBeenCalledWith("key");

storage.setItem("key", "value");
expect(localStorageMock.setItem).toHaveBeenCalledWith("key", "value");

storage.removeItem("key");
expect(localStorageMock.removeItem).toHaveBeenCalledWith("key");
});

it("doesn't access underlying storage when consent not granted", () => {
const storage = new LocalStorageProxy({ deviceAccess: false });

const result = storage.getItem("key");
expect(localStorageMock.getItem).not.toHaveBeenCalled();
expect(result).toBeNull();

storage.setItem("key", "value");
expect(localStorageMock.setItem).not.toHaveBeenCalled();

storage.removeItem("key");
expect(localStorageMock.removeItem).not.toHaveBeenCalled();
});
});

0 comments on commit 93a4c66

Please sign in to comment.