Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fake-browser): Upgrade webextension-polyfill types to 0.10.5 #46

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guide/fake-browser/implemented-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ For all APIs not listed here, you will have to mock the functions behavior yours

## `storage`

- The `local`, `sync`, and `managed` storages are all stored separately in memory.
- The `local`, `sync`, `session`, and `managed` storages are all stored separately in memory.
- `storage.onChanged`, `storage.{area}.onChanged` events are all triggered when updating values.
- Each storage area can be reset individually.

Expand Down
2 changes: 1 addition & 1 deletion packages/fake-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"devDependencies": {
"@types/lodash.merge": "^4.6.7",
"@types/webextension-polyfill": "^0.9.1",
"@types/webextension-polyfill": "^0.10.5",
"@vitest/coverage-c8": "^0.24.5",
"tsconfig": "workspace:*",
"tsup": "^6.4.0",
Expand Down
141 changes: 91 additions & 50 deletions packages/fake-browser/src/apis/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,116 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { fakeBrowser } from '..';

describe('Fake Storage API', () => {
beforeEach(fakeBrowser.reset);

it('should allow getting and setting storage', async () => {
const key1 = '1';
const value1 = '1';
const key2 = '2';
const value2 = 2;
it.each(['local', 'session', 'sync'] as const)(
'should allow getting and setting %s storage',
async area => {
const key1 = '1';
const value1 = '1';
const key2 = '2';
const value2 = 2;

await fakeBrowser.storage.local.set({ [key1]: value1 });
await fakeBrowser.storage.local.set({ [key2]: value2 });
await fakeBrowser.storage[area].set({ [key1]: value1 });
await fakeBrowser.storage[area].set({ [key2]: value2 });

expect(await fakeBrowser.storage.local.get()).toEqual({
[key1]: value1,
[key2]: value2,
});
expect(await fakeBrowser.storage.local.get(key1)).toEqual({
[key1]: value1,
});
expect(await fakeBrowser.storage[area].get()).toEqual({
[key1]: value1,
[key2]: value2,
});
expect(await fakeBrowser.storage[area].get(key1)).toEqual({
[key1]: value1,
});

await fakeBrowser.storage.local.remove(key1);
expect(await fakeBrowser.storage.local.get(key1)).toEqual({
[key1]: null,
});
expect(await fakeBrowser.storage.local.get({ [key1]: 'fallback' })).toEqual({
[key1]: 'fallback',
});
expect(await fakeBrowser.storage.local.get([key1, key2])).toEqual({
[key1]: null,
[key2]: value2,
});
await fakeBrowser.storage[area].remove(key1);
expect(await fakeBrowser.storage[area].get(key1)).toEqual({
[key1]: null,
});
expect(await fakeBrowser.storage[area].get({ [key1]: 'fallback' })).toEqual({
[key1]: 'fallback',
});
expect(await fakeBrowser.storage[area].get([key1, key2])).toEqual({
[key1]: null,
[key2]: value2,
});

await fakeBrowser.storage.local.clear();
expect(await fakeBrowser.storage.local.get()).toEqual({});
});

it('setting a value to undefined should do nothing', async () => {
const key = 'key';
const value = 'test';
await fakeBrowser.storage[area].clear();
expect(await fakeBrowser.storage[area].get()).toEqual({});
},
);

await fakeBrowser.storage.local.set({ [key]: value });
await fakeBrowser.storage.local.set({ [key]: undefined });
it.each(['local', 'session', 'sync'] as const)(
'setting a value to undefined should do nothing',
async area => {
const key = 'key';
const value = 'test';

expect(await fakeBrowser.storage.local.get(key)).toEqual({
[key]: value,
});
});
await fakeBrowser.storage[area].set({ [key]: value });
await fakeBrowser.storage[area].set({ [key]: undefined });
expect(await fakeBrowser.storage[area].get(key)).toEqual({
[key]: value,
});
},
);

it('sync.getBytesInUse should throw an error', () => {
expect(fakeBrowser.storage.sync.getBytesInUse).toThrowError();
});

it('setting a value should trigger change listeners', async () => {
describe('setting a value should trigger change listeners', () => {
const localListener = vi.fn();
const syncListener = vi.fn();
const sessionListener = vi.fn();
const globalListener = vi.fn();

fakeBrowser.storage.local.onChanged.addListener(localListener);
fakeBrowser.storage.sync.onChanged.addListener(syncListener);
fakeBrowser.storage.onChanged.addListener(globalListener);
beforeEach(() => {
fakeBrowser.storage.local.onChanged.addListener(localListener);
fakeBrowser.storage.sync.onChanged.addListener(syncListener);
fakeBrowser.storage.session.onChanged.addListener(sessionListener);
fakeBrowser.storage.onChanged.addListener(globalListener);
});
afterEach(() => {
localListener.mockClear();
syncListener.mockClear();
sessionListener.mockClear();
globalListener.mockClear();
});

it('setting a value to local storage should trigger change listeners', async () => {
await fakeBrowser.storage.local.set({ key: 'value' });
expect(localListener).toBeCalledTimes(1);
expect(syncListener).not.toBeCalled();
expect(sessionListener).not.toBeCalled();
expect(globalListener).toBeCalledTimes(1);

const changes = { key: { oldValue: null, newValue: 'value' } };
expect(localListener).toBeCalledWith(changes);
expect(globalListener).toBeCalledWith(changes, 'local');
});

it('setting a value to session storage should trigger change listeners', async () => {
await fakeBrowser.storage.session.set({ key: 'value' });
expect(localListener).not.toBeCalled();
expect(syncListener).not.toBeCalled();
expect(sessionListener).toBeCalledTimes(1);
expect(globalListener).toBeCalledTimes(1);

await fakeBrowser.storage.local.set({ key: 'value' });
const changes = { key: { oldValue: null, newValue: 'value' } };
expect(sessionListener).toBeCalledWith(changes);
expect(globalListener).toBeCalledWith(changes, 'session');
});

const changes = { key: { oldValue: null, newValue: 'value' } };
expect(localListener).toBeCalledTimes(1);
expect(syncListener).not.toBeCalled();
expect(globalListener).toBeCalledTimes(1);
it('setting a value to sync storage should trigger change listeners', async () => {
await fakeBrowser.storage.sync.set({ key: 'value' });
expect(localListener).not.toBeCalled();
expect(syncListener).toBeCalledTimes(1);
expect(sessionListener).not.toBeCalled();
expect(globalListener).toBeCalledTimes(1);

expect(localListener).toBeCalledWith(changes);
expect(globalListener).toBeCalledWith(changes, 'local');
const changes = { key: { oldValue: null, newValue: 'value' } };
expect(syncListener).toBeCalledWith(changes);
expect(globalListener).toBeCalledWith(changes, 'sync');
});
});
});
9 changes: 8 additions & 1 deletion packages/fake-browser/src/apis/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type StorageAreaWithTrigger = Storage.StorageArea & {
};
};

function defineStorageArea(area: 'local' | 'sync' | 'managed'): StorageAreaWithTrigger {
type StorageArea = 'local' | 'managed' | 'session' | 'sync';
function defineStorageArea(area: StorageArea): StorageAreaWithTrigger {
const data: Record<string, any> = {};
const onChanged =
defineEventWithTrigger<(changes: Storage.StorageAreaOnChangedChangesType) => void>();
Expand Down Expand Up @@ -90,6 +91,10 @@ const managedStorage = {
...defineStorageArea('managed'),
QUOTA_BYTES: 5242880 as const,
};
const sessionStorage = {
...defineStorageArea('session'),
QUOTA_BYTES: 10485760 as const,
};
const syncStorage = {
...defineStorageArea('sync'),
MAX_ITEMS: 512 as const,
Expand All @@ -106,11 +111,13 @@ export const storage: BrowserOverrides['storage'] = {
resetState() {
localStorage.resetState();
managedStorage.resetState();
sessionStorage.resetState();
syncStorage.resetState();
globalOnChanged.removeAllListeners();
},
local: localStorage,
managed: managedStorage,
session: sessionStorage,
sync: syncStorage,
onChanged: globalOnChanged,
};
Loading