2022-11-15 18:52:08 +01:00
|
|
|
import browser from 'webextension-polyfill';
|
|
|
|
import LocalStore from './local-store';
|
|
|
|
|
|
|
|
jest.mock('webextension-polyfill', () => ({
|
2023-08-17 13:59:05 +02:00
|
|
|
runtime: { lastError: null },
|
2022-11-15 18:52:08 +01:00
|
|
|
storage: { local: true },
|
|
|
|
}));
|
|
|
|
|
2023-08-17 13:59:05 +02:00
|
|
|
const setup = ({ localMock = jest.fn() } = {}) => {
|
|
|
|
browser.storage.local = localMock;
|
2022-11-15 18:52:08 +01:00
|
|
|
return new LocalStore();
|
|
|
|
};
|
|
|
|
describe('LocalStore', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
});
|
|
|
|
describe('contructor', () => {
|
|
|
|
it('should set isSupported property to false when browser does not support local storage', () => {
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup({ localMock: false });
|
2022-11-15 18:52:08 +01:00
|
|
|
|
|
|
|
expect(localStore.isSupported).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set isSupported property to true when browser supports local storage', () => {
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup();
|
2022-11-15 18:52:08 +01:00
|
|
|
expect(localStore.isSupported).toBe(true);
|
|
|
|
});
|
2023-08-17 13:59:05 +02:00
|
|
|
|
|
|
|
it('should initialize mostRecentRetrievedState to null', () => {
|
|
|
|
const localStore = setup({ localMock: false });
|
|
|
|
|
|
|
|
expect(localStore.mostRecentRetrievedState).toBeNull();
|
|
|
|
});
|
2022-11-15 18:52:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('setMetadata', () => {
|
|
|
|
it('should set the metadata property on LocalStore', () => {
|
|
|
|
const metadata = { version: 74 };
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup();
|
2022-11-15 18:52:08 +01:00
|
|
|
localStore.setMetadata(metadata);
|
|
|
|
|
|
|
|
expect(localStore.metadata).toStrictEqual(metadata);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('set', () => {
|
|
|
|
it('should throw an error if called in a browser that does not support local storage', async () => {
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup({ localMock: false });
|
2022-11-15 18:52:08 +01:00
|
|
|
await expect(() => localStore.set()).rejects.toThrow(
|
|
|
|
'Metamask- cannot persist state to local store as this browser does not support this action',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if not passed a truthy value as an argument', async () => {
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup();
|
2022-11-15 18:52:08 +01:00
|
|
|
await expect(() => localStore.set()).rejects.toThrow(
|
|
|
|
'MetaMask - updated state is missing',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if passed a valid argument but metadata has not yet been set', async () => {
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup();
|
2022-11-15 18:52:08 +01:00
|
|
|
await expect(() =>
|
|
|
|
localStore.set({ appState: { test: true } }),
|
|
|
|
).rejects.toThrow(
|
|
|
|
'MetaMask - metadata must be set on instance of ExtensionStore before calling "set"',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw if passed a valid argument and metadata has been set', async () => {
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup();
|
2022-11-15 18:52:08 +01:00
|
|
|
localStore.setMetadata({ version: 74 });
|
|
|
|
await expect(async function () {
|
|
|
|
localStore.set({ appState: { test: true } });
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('get', () => {
|
|
|
|
it('should return undefined if called in a browser that does not support local storage', async () => {
|
2023-08-17 13:59:05 +02:00
|
|
|
const localStore = setup({ localMock: false });
|
2022-11-15 18:52:08 +01:00
|
|
|
const result = await localStore.get();
|
|
|
|
expect(result).toStrictEqual(undefined);
|
|
|
|
});
|
2023-08-17 13:59:05 +02:00
|
|
|
|
|
|
|
it('should update mostRecentRetrievedState', async () => {
|
|
|
|
const localStore = setup({
|
|
|
|
localMock: {
|
|
|
|
get: jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation(() =>
|
|
|
|
Promise.resolve({ appState: { test: true } }),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await localStore.get();
|
|
|
|
|
|
|
|
expect(localStore.mostRecentRetrievedState).toStrictEqual({
|
|
|
|
appState: { test: true },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should reset mostRecentRetrievedState to null if storage.local is empty', async () => {
|
|
|
|
const localStore = setup({
|
|
|
|
localMock: {
|
|
|
|
get: jest.fn().mockImplementation(() => Promise.resolve({})),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await localStore.get();
|
|
|
|
|
|
|
|
expect(localStore.mostRecentRetrievedState).toStrictEqual(null);
|
|
|
|
});
|
2022-11-15 18:52:08 +01:00
|
|
|
});
|
|
|
|
});
|