feat: add local storage service
This commit is contained in:
61
src/app/services/localstorage.spec.ts
Normal file
61
src/app/services/localstorage.spec.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { Localstorage } from './localstorage';
|
||||
|
||||
describe('Localstorage', () => {
|
||||
let service: Localstorage;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(Localstorage);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should set item to localStorage', () => {
|
||||
const ITEM_MOCK = { mock: 'mock' };
|
||||
const ITEM_KEY_MOCK = 'mock';
|
||||
const setItemSpy = spyOn(globalThis.localStorage, 'setItem');
|
||||
service.setItem(ITEM_KEY_MOCK, ITEM_MOCK);
|
||||
expect(setItemSpy).toHaveBeenCalledOnceWith(ITEM_KEY_MOCK, JSON.stringify(ITEM_MOCK));
|
||||
});
|
||||
|
||||
it('should handle json stringify error on setItem', () => {
|
||||
const ITEM_MOCK = { mock: BigInt(1) };
|
||||
const ITEM_KEY_MOCK = 'mock';
|
||||
const setItemSpy = spyOn(globalThis.localStorage, 'setItem');
|
||||
service.setItem(ITEM_KEY_MOCK, ITEM_MOCK);
|
||||
expect(setItemSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should get null from localStorage if it is malformed JSON string ', () => {
|
||||
const ITEM_MOCK = '{"mock":"mock}';
|
||||
const ITEM_KEY_MOCK = 'mock';
|
||||
spyOn(globalThis.localStorage, 'getItem').and.returnValue(ITEM_MOCK);
|
||||
const item = service.getItem(ITEM_KEY_MOCK);
|
||||
expect(item).toBeNull();
|
||||
});
|
||||
|
||||
it('should get item from localStorage', () => {
|
||||
const ITEM_MOCK = '{"mock":"mock"}';
|
||||
const ITEM_KEY_MOCK = 'mock';
|
||||
const getItemSpy = spyOn(globalThis.localStorage, 'getItem').and.returnValue(ITEM_MOCK);
|
||||
service.getItem(ITEM_KEY_MOCK);
|
||||
expect(getItemSpy).toHaveBeenCalledOnceWith(ITEM_KEY_MOCK);
|
||||
});
|
||||
|
||||
it('should remove item from localStorage', () => {
|
||||
const ITEM_KEY_MOCK = 'mock';
|
||||
const removeSpy = spyOn(globalThis.localStorage, 'removeItem');
|
||||
service.removeItem(ITEM_KEY_MOCK);
|
||||
expect(removeSpy).toHaveBeenCalledOnceWith(ITEM_KEY_MOCK);
|
||||
});
|
||||
|
||||
it('should clear localStorage', () => {
|
||||
const clearSpy = spyOn(globalThis.localStorage, 'clear');
|
||||
service.clear();
|
||||
expect(clearSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
34
src/app/services/localstorage.ts
Normal file
34
src/app/services/localstorage.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class Localstorage {
|
||||
setItem(key: string, value: any): void {
|
||||
try {
|
||||
const jsonValue = JSON.stringify(value);
|
||||
localStorage.setItem(key, jsonValue);
|
||||
} catch (error) {
|
||||
if (!environment.prod) console.error('Error saving to local storage', error);
|
||||
}
|
||||
}
|
||||
|
||||
getItem<T>(key: string): T | null {
|
||||
try {
|
||||
const value = localStorage.getItem(key);
|
||||
return value ? JSON.parse(value) : null;
|
||||
} catch (error) {
|
||||
if (!environment.prod) console.error('Error reading from local storage', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
removeItem(key: string): void {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
localStorage.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user