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 2 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
132 changes: 120 additions & 12 deletions packages/fake-browser/src/apis/storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { fakeBrowser } from '..';

describe('Fake Storage API', () => {
Expand Down Expand Up @@ -35,6 +35,58 @@ describe('Fake Storage API', () => {

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

await fakeBrowser.storage.session.set({ [key1]: value1 });
kayac-chang marked this conversation as resolved.
Show resolved Hide resolved
await fakeBrowser.storage.session.set({ [key2]: value2 });

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

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

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

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

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

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

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

it('setting a value to undefined should do nothing', async () => {
Expand All @@ -47,29 +99,85 @@ describe('Fake Storage API', () => {
expect(await fakeBrowser.storage.local.get(key)).toEqual({
[key]: value,
});

await fakeBrowser.storage.session.set({ [key]: value });
await fakeBrowser.storage.session.set({ [key]: undefined });

expect(await fakeBrowser.storage.session.get(key)).toEqual({
[key]: value,
});

await fakeBrowser.storage.sync.set({ [key]: value });
await fakeBrowser.storage.sync.set({ [key]: undefined });

expect(await fakeBrowser.storage.sync.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(() => {
fakeBrowser.storage.local.onChanged.removeListener(localListener);
fakeBrowser.storage.sync.onChanged.removeListener(syncListener);
fakeBrowser.storage.session.onChanged.removeListener(sessionListener);
fakeBrowser.storage.onChanged.removeListener(globalListener);
});
afterEach(() => {
kayac-chang marked this conversation as resolved.
Show resolved Hide resolved
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: 5242880 as const,
kayac-chang marked this conversation as resolved.
Show resolved Hide resolved
};
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,
};
3 changes: 3 additions & 0 deletions packages/fake-browser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export interface BrowserOverrides {
local: {
onChanged: EventForTesting<[changes: Storage.StorageAreaOnChangedChangesType]>;
};
session: {
onChanged: EventForTesting<[changes: Storage.StorageAreaOnChangedChangesType]>;
};
sync: {
onChanged: EventForTesting<[changes: Storage.StorageAreaOnChangedChangesType]>;
};
Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading