File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
packages/cozy-pouch-link/src Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ export const LOCALSTORAGE_ADAPTERNAME = 'cozy-client-pouch-link-adaptername'
99
1010export class PouchLocalStorage {
1111 constructor ( storageEngine ) {
12+ checkStorageEngine ( storageEngine )
1213 this . storageEngine = storageEngine
1314 }
1415
@@ -220,3 +221,23 @@ export class PouchLocalStorage {
220221 await this . storageEngine . setItem ( LOCALSTORAGE_ADAPTERNAME , adapter )
221222 }
222223}
224+
225+ /**
226+ * Throw if the given storage engine does not implement the expected Interface
227+ *
228+ * @param {* } storageEngine - Object containing storage access methods
229+ */
230+ const checkStorageEngine = storageEngine => {
231+ const requiredMethods = [ 'setItem' , 'getItem' , 'removeItem' ]
232+
233+ const missingMethods = requiredMethods . filter (
234+ requiredMethod => ! storageEngine [ requiredMethod ]
235+ )
236+
237+ if ( missingMethods . length > 0 ) {
238+ const missingMethodsString = missingMethods . join ( ', ' )
239+ throw new Error (
240+ `Provided storageEngine is missing the following methods: ${ missingMethodsString } `
241+ )
242+ }
243+ }
Original file line number Diff line number Diff line change 1+ import { PouchLocalStorage } from './localStorage'
2+
3+ describe ( 'LocalStorage' , ( ) => {
4+ describe ( 'Type assertion' , ( ) => {
5+ it ( 'should throw if setItem method is missing' , ( ) => {
6+ expect ( ( ) => {
7+ new PouchLocalStorage ( {
8+ getItem : jest . fn ( ) ,
9+ removeItem : jest . fn ( )
10+ } )
11+ } ) . toThrow (
12+ 'Provided storageEngine is missing the following methods: setItem'
13+ )
14+ } )
15+
16+ it ( 'should throw if getItem method is missing' , ( ) => {
17+ expect ( ( ) => {
18+ new PouchLocalStorage ( {
19+ setItem : jest . fn ( ) ,
20+ removeItem : jest . fn ( )
21+ } )
22+ } ) . toThrow (
23+ 'Provided storageEngine is missing the following methods: getItem'
24+ )
25+ } )
26+
27+ it ( 'should throw if removeItem method is missing' , ( ) => {
28+ expect ( ( ) => {
29+ new PouchLocalStorage ( {
30+ getItem : jest . fn ( ) ,
31+ setItem : jest . fn ( )
32+ } )
33+ } ) . toThrow (
34+ 'Provided storageEngine is missing the following methods: removeItem'
35+ )
36+ } )
37+
38+ it ( 'should throw if multiple methods are missing' , ( ) => {
39+ expect ( ( ) => {
40+ new PouchLocalStorage ( {
41+ getItem : jest . fn ( )
42+ } )
43+ } ) . toThrow (
44+ 'Provided storageEngine is missing the following methods: setItem, removeItem'
45+ )
46+ } )
47+ } )
48+ } )
You can’t perform that action at this time.
0 commit comments