-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/tswebextension/test/lib/common/storage/extension-storage-decorator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/tswebextension/test/lib/common/storage/extension-storage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/tswebextension/test/lib/common/storage/persistent-value-container.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters