-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): add persist form to storagecloses #24
- Loading branch information
Showing
10 changed files
with
233 additions
and
17 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
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
31 changes: 31 additions & 0 deletions
31
projects/ngneat/reactive-forms/src/lib/localStorageManager.spec.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,31 @@ | ||
import { LocalStorageManager } from './localStorageManager'; | ||
|
||
type Person = { | ||
name: string; | ||
phone: { | ||
num: number; | ||
prefix: number; | ||
}; | ||
}; | ||
|
||
const createStorageManager = () => { | ||
return new LocalStorageManager<Person>(); | ||
}; | ||
|
||
describe('LocalStorageManager', () => { | ||
it('should set value', () => { | ||
const person = { name: 'ewan', phone: { num: 5550153, prefix: 288 } }; | ||
const spy = jest.spyOn(localStorage, 'setItem').mockImplementation(() => {}); | ||
const storageManager = createStorageManager(); | ||
storageManager.setValue('key', person); | ||
expect(spy).toHaveBeenCalledWith('key', JSON.stringify(person)); | ||
}); | ||
|
||
it('should get value', () => { | ||
const spy = jest.spyOn(localStorage, 'getItem').mockImplementation(() => '{ "name": "ewan" }'); | ||
const storageManager = createStorageManager(); | ||
const value = storageManager.getValue('key'); | ||
expect(spy).toHaveBeenCalledWith('key'); | ||
expect(value).toEqual({ name: 'ewan' }); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
projects/ngneat/reactive-forms/src/lib/localStorageManager.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,11 @@ | ||
import { PersistManager } from './persistManager'; | ||
|
||
export class LocalStorageManager<T> implements PersistManager<T> { | ||
setValue(key: string, data: T): void { | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
} | ||
|
||
getValue(key: string): T { | ||
return JSON.parse(localStorage.getItem(key) || '{}'); | ||
} | ||
} |
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,4 @@ | ||
export interface PersistManager<T> { | ||
setValue(key: string, data: T): void; | ||
getValue(key: string): T; | ||
} |
31 changes: 31 additions & 0 deletions
31
projects/ngneat/reactive-forms/src/lib/sessionStorageManager.spec.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,31 @@ | ||
import { SessionStorageManager } from './sessionStorageManager'; | ||
|
||
type Person = { | ||
name: string; | ||
phone: { | ||
num: number; | ||
prefix: number; | ||
}; | ||
}; | ||
|
||
const createStorageManager = () => { | ||
return new SessionStorageManager<Person>(); | ||
}; | ||
|
||
describe('SessionStorageManager', () => { | ||
it('should set value', () => { | ||
const person = { name: 'ewan', phone: { num: 5550153, prefix: 288 } }; | ||
const spy = jest.spyOn(sessionStorage, 'setItem').mockImplementation(() => {}); | ||
const storageManager = createStorageManager(); | ||
storageManager.setValue('key', person); | ||
expect(spy).toHaveBeenCalledWith('key', JSON.stringify(person)); | ||
}); | ||
|
||
it('should get value', () => { | ||
const spy = jest.spyOn(sessionStorage, 'getItem').mockImplementation(() => '{ "name": "ewan" }'); | ||
const storageManager = createStorageManager(); | ||
const value = storageManager.getValue('key'); | ||
expect(spy).toHaveBeenCalledWith('key'); | ||
expect(value).toEqual({ name: 'ewan' }); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
projects/ngneat/reactive-forms/src/lib/sessionStorageManager.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,11 @@ | ||
import { PersistManager } from './persistManager'; | ||
|
||
export class SessionStorageManager<T> implements PersistManager<T> { | ||
setValue(key: string, data: T): void { | ||
sessionStorage.setItem(key, JSON.stringify(data)); | ||
} | ||
|
||
getValue(key: string): T { | ||
return JSON.parse(sessionStorage.getItem(key) || '{}'); | ||
} | ||
} |
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