diff --git a/src/app/services/localstorage.spec.ts b/src/app/services/localstorage.spec.ts new file mode 100644 index 0000000..a35da6f --- /dev/null +++ b/src/app/services/localstorage.spec.ts @@ -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); + }); +}); diff --git a/src/app/services/localstorage.ts b/src/app/services/localstorage.ts new file mode 100644 index 0000000..9d18867 --- /dev/null +++ b/src/app/services/localstorage.ts @@ -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(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(); + } +} diff --git a/src/environments/environment.development.ts b/src/environments/environment.development.ts index 072ccf4..3ef919a 100644 --- a/src/environments/environment.development.ts +++ b/src/environments/environment.development.ts @@ -1,3 +1,4 @@ export const environment = { - apiUrl: 'http://localhost:4444' + prod: false, + apiUrl: 'http://192.168.1.3:4444', }; diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 9a56100..fca3858 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -1,3 +1,4 @@ export const environment = { - apiUrl: 'http://localhost:333' + prod: true, + apiUrl: 'http://localhost:333', };