import type { IconifyJSON } from '@iconify/types' ; import type { StoredItem } from '../../lib/browser-storage' ; import { loadCache , count , config , emptyList } from '../../lib/browser-storage' ; import { getStorage , iconExists } from '../../lib/storage/storage' ; import { nextPrefix , createCache , reset , cachePrefix , cacheVersion , versionKey , countKey , hour , cacheExpiration , } from './fake_cache' ; describe ( 'Testing loading from localStorage' , ( ) => { const provider = '' ; it ( 'Valid icon set' , ( ) => { const prefix = nextPrefix ( ) ; const cache = createCache ( ) ; cache . setItem ( versionKey , cacheVersion ) ; cache . setItem ( countKey , '1' ) ; const item = { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : { prefix : prefix , icons : { foo : { body : '<g></g>' , } , } , } , } ; cache . setItem ( cachePrefix + '0' , JSON . stringify ( item ) ) ; reset ( { localStorage : cache , } ) ; const icons = getStorage ( provider , prefix ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; loadCache ( ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( true ) ; expect ( config ) . toEqual ( { local : true , session : false , } ) ; expect ( count ) . toEqual ( { local : 1 , session : 0 , } ) ; expect ( emptyList ) . toEqual ( { local : [ ] , session : [ ] , } ) ; } ) ; it ( 'Different provider' , ( ) => { const provider = nextPrefix ( ) ; const prefix = nextPrefix ( ) ; const cache = createCache ( ) ; cache . setItem ( versionKey , cacheVersion ) ; cache . setItem ( countKey , '1' ) ; const item = { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : { prefix : prefix , icons : { foo : { body : '<g></g>' , } , } , } , } ; cache . setItem ( cachePrefix + '0' , JSON . stringify ( item ) ) ; reset ( { localStorage : cache , } ) ; const icons = getStorage ( provider , prefix ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; const icons2 = getStorage ( '' , prefix ) ; expect ( iconExists ( icons2 , 'foo' ) ) . toBe ( false ) ; loadCache ( ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( true ) ; expect ( iconExists ( icons2 , 'foo' ) ) . toBe ( false ) ; expect ( config ) . toEqual ( { local : true , session : false , } ) ; expect ( count ) . toEqual ( { local : 1 , session : 0 , } ) ; expect ( emptyList ) . toEqual ( { local : [ ] , session : [ ] , } ) ; } ) ; it ( 'Expired icon set' , ( ) => { const prefix = nextPrefix ( ) ; const cache = createCache ( ) ; cache . setItem ( versionKey , cacheVersion ) ; cache . setItem ( countKey , '1' ) ; const item = { cached : Math . floor ( Date . now ( ) / hour ) - cacheExpiration - 1 , provider , data : { prefix : prefix , icons : { foo : { body : '<g></g>' , } , } , } , } ; cache . setItem ( cachePrefix + '0' , JSON . stringify ( item ) ) ; reset ( { localStorage : cache , } ) ; const icons = getStorage ( provider , prefix ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; loadCache ( ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; expect ( config ) . toEqual ( { local : true , session : false , } ) ; expect ( count ) . toEqual ( { local : 0 , session : 0 , } ) ; expect ( emptyList ) . toEqual ( { local : [ ] , session : [ ] , } ) ; } ) ; it ( 'Bad icon set' , ( ) => { const prefix = nextPrefix ( ) ; const cache = createCache ( ) ; cache . setItem ( versionKey , cacheVersion ) ; cache . setItem ( countKey , '1' ) ; cache . setItem ( cachePrefix + '0' , JSON . stringify ( { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : { prefix : prefix , icons : { foo : { width : 20 , } , } , } , } ) ) ; reset ( { localStorage : cache , } ) ; const icons = getStorage ( provider , prefix ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; loadCache ( ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; expect ( config ) . toEqual ( { local : true , session : false , } ) ; expect ( count ) . toEqual ( { local : 0 , session : 0 , } ) ; expect ( emptyList ) . toEqual ( { local : [ ] , session : [ ] , } ) ; } ) ; it ( 'Wrong counter' , ( ) => { const prefix = nextPrefix ( ) ; const cache = createCache ( ) ; cache . setItem ( versionKey , cacheVersion ) ; cache . setItem ( countKey , '0' ) ; const item = { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : { prefix : prefix , icons : { foo : { body : '<g></g>' , } , } , } , } ; cache . setItem ( cachePrefix + '0' , JSON . stringify ( item ) ) ; reset ( { localStorage : cache , } ) ; const icons = getStorage ( provider , prefix ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; loadCache ( ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; expect ( config ) . toEqual ( { local : true , session : false , } ) ; expect ( count ) . toEqual ( { local : 0 , session : 0 , } ) ; expect ( emptyList ) . toEqual ( { local : [ ] , session : [ ] , } ) ; } ) ; it ( 'Missing entries at the end' , ( ) => { const prefix = nextPrefix ( ) ; const cache = createCache ( ) ; cache . setItem ( versionKey , cacheVersion ) ; cache . setItem ( countKey , '5' ) ; const item = { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : { prefix : prefix , icons : { foo : { body : '<g></g>' , } , } , } , } ; cache . setItem ( cachePrefix + '0' , JSON . stringify ( item ) ) ; reset ( { localStorage : cache , } ) ; const icons = getStorage ( provider , prefix ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( false ) ; loadCache ( ) ; expect ( iconExists ( icons , 'foo' ) ) . toBe ( true ) ; expect ( config ) . toEqual ( { local : true , session : false , } ) ; expect ( count ) . toEqual ( { local : 1 , session : 0 , } ) ; expect ( emptyList ) . toEqual ( { local : [ ] , session : [ ] , } ) ; } ) ; it ( 'Missing entries' , ( ) => { const prefix = nextPrefix ( ) ; const cache = createCache ( ) ; cache . setItem ( versionKey , cacheVersion ) ; cache . setItem ( countKey , '5' ) ; const item1 = { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : { prefix : prefix , icons : { foo1 : { body : '<g></g>' , } , } , } , } ; const item4 = { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : { prefix : prefix , icons : { foo4 : { body : '<g></g>' , } , } , } , } ; cache . setItem ( cachePrefix + '1' , JSON . stringify ( item1 ) ) ; cache . setItem ( cachePrefix + '4' , JSON . stringify ( item4 ) ) ; reset ( { localStorage : cache , } ) ; const icons = getStorage ( provider , prefix ) ; expect ( iconExists ( icons , 'foo1' ) ) . toBe ( false ) ; expect ( iconExists ( icons , 'foo4' ) ) . toBe ( false ) ; loadCache ( ) ; expect ( iconExists ( icons , 'foo1' ) ) . toBe ( true ) ; expect ( iconExists ( icons , 'foo4' ) ) . toBe ( true ) ; expect ( config ) . toEqual ( { local : true , session : false , } ) ; expect ( count ) . toEqual ( { local : 5 , session : 0 , } ) ; expect ( emptyList ) . toEqual ( { local : [ 3 , 2 , 0 ] , session : [ ] , } ) ; } ) ; it ( 'Using both storage options' , ( ) => { const prefix = nextPrefix ( ) ; const cache1 = createCache ( ) ; const cache2 = createCache ( ) ; cache1 . setItem ( versionKey , cacheVersion ) ; cache2 . setItem ( versionKey , cacheVersion ) ; cache1 . setItem ( countKey , '6' ) ; cache2 . setItem ( countKey , '3' ) ; const icons = [ ] ; const items = [ ] ; for ( let i = 0 ; i < 6 ; i ++ ) { const icon = { prefix : prefix , icons : { [ 'foo' + i ] : { body : '<g></g>' , } , } , } ; const item = { cached : Math . floor ( Date . now ( ) / hour ) , provider , data : icon , } ; icons . push ( icon ) ; items . push ( item ) ; } [ 1 , 3 , 5 ] . forEach ( ( index ) => { cache1 . setItem ( cachePrefix + index , JSON . stringify ( items [ index ] ) ) ; } ) ; [ 0 , 2 ] . forEach ( ( index ) => { cache2 . setItem ( cachePrefix + index , JSON . stringify ( items [ index ] ) ) ; } ) ; reset ( { localStorage : cache1 , sessionStorage : cache2 , } ) ; const iconsStorage = getStorage ( provider , prefix ) ; for ( let i = 0 ; i < 6 ; i ++ ) { expect ( iconExists ( iconsStorage , 'foo' + i ) ) . toBe ( false ) ; } loadCache ( ) ; for ( let i = 0 ; i < 6 ; i ++ ) { expect ( iconExists ( iconsStorage , 'foo' + i ) ) . toBe ( i !== 4 ) ; } expect ( config ) . toEqual ( { local : true , session : true , } ) ; expect ( count ) . toEqual ( { local : 6 , session : 3 , } ) ; expect ( emptyList ) . toEqual ( { local : [ 4 , 2 , 0 ] , session : [ 1 ] , } ) ; } ) ; } ) ;