Skip to content

refactor(key-storage): renamed to use-store #23

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

Merged
merged 2 commits into from
Mar 8, 2023
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 apps/www/src/app/library-components/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './key-storage';
export * from './use-store';

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './stores';
export * from './key-store';
export * from './use-store';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {KeyStore} from "./key-store";
import {InMemoryStore} from "./stores";

describe('Key Store', () => {

const key = 'key';
const defaultValue = 'default';

let store: KeyStore;

beforeEach(() => {
store = new KeyStore(
key,
defaultValue,
new InMemoryStore(),
);
});

it('should return a stored record', () => {
const record = 'value';
store.setValue(record);
expect(store.value).toEqual(record);
});

it('should overwrite the value', () => {
const record1 = 'value';
const record2 = 'value-2';

store.setValue(record1);
store.setValue(record2);

expect(store.value).toEqual(record2);
});

it('should return the default value if the record doesnt exist', () => {
expect(store.value).toBe(defaultValue);
});

})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class KeyStorage<T = unknown> {
export class KeyStore<T = unknown> {

constructor(
private _key: string,
Expand All @@ -7,20 +7,16 @@ export class KeyStorage<T = unknown> {
) {
}

getItem(): T {
get value(): T {
const data = this._store.getItem(this._key);
return data
? JSON.parse(data)
: this._defaultValue;
}

setItem(value: T): void {
setValue(value: T): void {
const data = JSON.stringify(value);
this._store.setItem(this._key, data);
}

removeItem(): void {
this._store.removeItem(this._key);
}

}
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import {TestBed} from "@angular/core/testing";
import {createKeyStorage} from "./create-key-storage";
import {KeyStorage} from "./key-storage";
import {useStore} from "./use-store";
import {KeyStore} from "./key-store";
import {InMemoryStore, LocalStorageStore} from "./stores";

describe('Create Key Storage', () => {
describe('useStore', () => {

const key = 'key';
const defaultValue = 'default-value';

it('should return an Angular injection token', () => {
const token = createKeyStorage(key, defaultValue);
const token = useStore(key, defaultValue);
const store = TestBed.inject(token);
expect(store).toBeInstanceOf(KeyStorage);
expect(store).toBeInstanceOf(KeyStore);
});

it('should configure the key storage correctly', () => {
const token = createKeyStorage(key, defaultValue);
const token = useStore(key, defaultValue);
const store = TestBed.inject(token);
expect(store['_key']).toBe(key)
expect(store['_defaultValue']).toBe(defaultValue)
});

it('should configure the key storage to use the localStorageStore by default', () => {
const token = createKeyStorage(key, defaultValue);
const token = useStore(key, defaultValue);
const store = TestBed.inject(token);
expect(store['_store']).toBeInstanceOf(LocalStorageStore)
});

it('should configure the key storage to use the localStorage Store', () => {
const token = createKeyStorage(key, defaultValue, );
const token = useStore(key, defaultValue, new LocalStorageStore());
const store = TestBed.inject(token);
expect(store['_store']).toBeInstanceOf(LocalStorageStore)
});

it('should configure the key storage to use the InMemory Store', () => {
const token = createKeyStorage(key, defaultValue, new InMemoryStore());
const token = useStore(key, defaultValue, new InMemoryStore());
const store = TestBed.inject(token);
expect(store['_store']).toBeInstanceOf(InMemoryStore)
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {InjectionToken} from "@angular/core";
import {KeyStorage} from "./key-storage";
import {KeyStore} from "./key-store";
import {localStorageStore} from "./stores";

export const createKeyStorage = <T = unknown>(
export const useStore = <T = unknown>(
key: string,
defaultValue: T,
store: Storage = localStorageStore,
) =>
new InjectionToken(
`storage-key-${key}`, {
`store-${key}`, {
providedIn: 'root',
factory: () => new KeyStorage<T>(key, defaultValue, store)
factory: () => new KeyStore<T>(key, defaultValue, store)
});