Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelvis committed Nov 1, 2023
1 parent f91948a commit 7a21a1e
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class PersistentValueContainer<Key extends string = string, Value = unkno

#key: Key;

#value: Value;
#value!: Value;

// TODO: make required after the migration to event-driven background.
#save?: () => void;
Expand Down
2 changes: 1 addition & 1 deletion packages/tswebextension/src/lib/mv2/background/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class AppContext {
* Is app started.
*/
@sessionDecorator(SessionStorageKey.IsAppStarted)
accessor isAppStarted: boolean;
accessor isAppStarted!: boolean;

/**
* MV2 ConfigurationMV2 excludes heavyweight fields with rules.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable jsdoc/require-jsdoc */
import browser from 'webextension-polyfill';
import { createExtensionStorageDecorator } from '@lib/common/storage/extension-storage-decorator';
import { ExtensionStorage } from '@lib/common/storage/extension-storage';

describe('createExtensionStorageDecorator', () => {
const key = 'test-key';
const data = { foo: 'bar', baz: 42 };
let storage: ExtensionStorage<typeof data>;

beforeAll(async () => {
storage = new ExtensionStorage(key, browser.storage.local);
await storage.init(data);
});

it('should create a decorator', () => {
const decorator = createExtensionStorageDecorator(storage)('foo');

expect(typeof decorator).toBe('function');
});

it('should throw an error if decorator is already registered for the storage field', () => {
const decorator = createExtensionStorageDecorator(storage);
decorator('foo');
expect(() => decorator('foo')).toThrow(
'Decorator for foo field is already registered',
);
});

it('should throw an error if decorator is applied to non-auto accessor', () => {
const fieldDecorator = createExtensionStorageDecorator(storage)('foo');

// Required for test runtime errors
// @ts-ignore
expect(() => fieldDecorator({}, { kind: 'method' })).toThrow(
'Class member is not auto accessor',
);
});

it('should get and set the value of the specified field', () => {
const decorator = createExtensionStorageDecorator(storage);

class TestClass {
@decorator('foo')
accessor foo!: string
}

const instance = new TestClass();

expect(instance.foo).toBe('bar');

instance.foo = 'new-value';

expect(instance.foo).toBe('new-value');
expect(storage.get('foo')).toBe('new-value');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import browser from 'webextension-polyfill';
import { ExtensionStorage } from '@lib/common/storage/extension-storage';

describe('ExtensionStorage', () => {
const key = 'test-key';
const data = { foo: 'bar', baz: 42 };
const api = browser.storage.local;

it('should initialize the storage', async () => {
const storage = new ExtensionStorage<typeof data>(key, api);
await storage.init(data);

expect(storage.get('foo')).toBe('bar');
expect(storage.get('baz')).toBe(42);
});

it('should get the value by the specified key', async () => {
const storage = new ExtensionStorage<typeof data>(key, api);
await storage.init(data);

expect(storage.get('foo')).toBe('bar');
expect(storage.get('baz')).toBe(42);
});

it('should set the value by the specified key', async () => {
const storage = new ExtensionStorage<typeof data>(key, api);
await storage.init(data);

storage.set('foo', 'new-bar');

expect(storage.get('foo')).toBe('new-bar');
});

it('should delete the value by the specified key', async () => {
const storage = new ExtensionStorage<typeof data>(key, api);
await storage.init(data);

storage.delete('foo');

expect(storage.get('foo')).toBeUndefined();
expect(storage.get('baz')).toBe(42);
});

it('should throw an error if storage is not initialized', () => {
const storage = new ExtensionStorage<typeof data>(key, api);

expect(() => storage.get('foo')).toThrow('Storage not initialized');
expect(() => storage.set('foo', 'bar')).toThrow('Storage not initialized');
expect(() => storage.delete('foo')).toThrow('Storage not initialized');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import browser from 'webextension-polyfill';
import { PersistentValueContainer } from '@lib/common/storage/persistent-value-container';

describe('PersistentValueContainer', () => {
const key = 'test-key';
const value = 'test-value';
const api = browser.storage.local;

it('should initialize the value', async () => {
const container = new PersistentValueContainer(key, api);
await container.init(value);

expect(container.get()).toBe(value);
});

it('should set the value', async () => {
const container = new PersistentValueContainer(key, api);
await container.init(value);

const newValue = 'new-value';
container.set(newValue);

expect(container.get()).toBe(newValue);
});

it('should throw an error if storage is not initialized', () => {
const container = new PersistentValueContainer(key, browser.storage.local);

expect(() => container.get()).toThrow('Storage not initialized');
expect(() => container.set(value)).toThrow('Storage not initialized');
});

it('should throw an error if storage is already initialized', async () => {
const container = new PersistentValueContainer(key, browser.storage.local);
await container.init(value);

await expect(container.init(value)).rejects.toThrow('Storage already initialized');
});
});
1 change: 0 additions & 1 deletion packages/tswebextension/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"resolveJsonModule": true,
"esModuleInterop": true, // This will set allowSyntheticDefaultImports to true
"typeRoots": ["./types", "node_modules/@types"],
"strictPropertyInitialization": false, // We don not want to initialize accessor properties with decorators
}
}

0 comments on commit 7a21a1e

Please sign in to comment.