Skip to content

Commit a38c5b1

Browse files
test(key-storage): adds test coverage for key-storage
1 parent ff165ed commit a38c5b1

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

Diff for: apps/www/src/app/library-components/services/color-mode/color-mode.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class ColorModeService {
3535

3636
private setAuto() {
3737
this._mode.next(ColourMode.AUTO);
38-
this.storage.clear();
38+
this.storage.removeItem();
3939
this.document.body.classList?.remove(ColourMode.LIGHT);
4040
this.document.body.classList?.remove(ColourMode.DARK);
4141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {TestBed} from "@angular/core/testing";
2+
import {createKeyStorage} from "./create-key-storage";
3+
import {KeyStorage} from "./key-storage";
4+
import {InMemoryStore, LocalStorageStore} from "./stores";
5+
6+
describe('Create Key Storage', () => {
7+
8+
const key = 'key';
9+
const defaultValue = 'default-value';
10+
11+
it('should return an Angular injection token', () => {
12+
const token = createKeyStorage(key, defaultValue);
13+
const store = TestBed.inject(token);
14+
expect(store).toBeInstanceOf(KeyStorage);
15+
});
16+
17+
it('should configure the key storage correctly', () => {
18+
const token = createKeyStorage(key, defaultValue);
19+
const store = TestBed.inject(token);
20+
expect(store['_key']).toBe(key)
21+
expect(store['_defaultValue']).toBe(defaultValue)
22+
});
23+
24+
it('should configure the key storage to use the localStorageStore by default', () => {
25+
const token = createKeyStorage(key, defaultValue);
26+
const store = TestBed.inject(token);
27+
expect(store['_store']).toBeInstanceOf(LocalStorageStore)
28+
});
29+
30+
it('should configure the key storage to use the localStorage Store', () => {
31+
const token = createKeyStorage(key, defaultValue, );
32+
const store = TestBed.inject(token);
33+
expect(store['_store']).toBeInstanceOf(LocalStorageStore)
34+
});
35+
36+
it('should configure the key storage to use the InMemory Store', () => {
37+
const token = createKeyStorage(key, defaultValue, new InMemoryStore());
38+
const store = TestBed.inject(token);
39+
expect(store['_store']).toBeInstanceOf(InMemoryStore)
40+
});
41+
42+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {KeyStorage} from "./key-storage";
2+
import {InMemoryStore} from "./stores";
3+
4+
describe('Key Storage', () => {
5+
6+
const key = 'key';
7+
const defaultValue = 'default';
8+
9+
let store: KeyStorage;
10+
11+
beforeEach(() => {
12+
store = new KeyStorage(
13+
key,
14+
defaultValue,
15+
new InMemoryStore(),
16+
);
17+
});
18+
19+
it('should return a stored record', () => {
20+
const record = 'value';
21+
store.setItem(record);
22+
23+
const returnedRecord = store.getItem();
24+
expect(returnedRecord).toEqual(record);
25+
});
26+
27+
it('should overwrite the value', () => {
28+
const record1 = 'value';
29+
const record2 = 'value-2';
30+
31+
store.setItem(record1);
32+
store.setItem(record2);
33+
34+
const returnedRecord = store.getItem();
35+
expect(returnedRecord).toEqual(record2);
36+
});
37+
38+
it('should return the default value if the record doesnt exist', () => {
39+
const returnedRecord = store.getItem();
40+
expect(returnedRecord).toBe(defaultValue);
41+
});
42+
43+
it('should return the default value for removed records', () => {
44+
const record = 'value';
45+
store.setItem(record);
46+
store.removeItem();
47+
48+
const returnedRecord = store.getItem();
49+
expect(returnedRecord).toEqual(defaultValue);
50+
});
51+
52+
})

Diff for: apps/www/src/app/library-components/utilities/key-storage/key-storage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class KeyStorage<T = unknown> {
1919
this._store.setItem(this._key, data);
2020
}
2121

22-
clear(): void {
22+
removeItem(): void {
2323
this._store.removeItem(this._key);
2424
}
2525

0 commit comments

Comments
 (0)