import { fullIcon } from '@iconify/utils/lib/icon' ; import { listIcons } from '../../lib/storage/storage' ; import { iconExists , getIcon , addIcon , addCollection , allowSimpleNames , } from '../../lib/storage/functions' ; describe ( 'Testing IconifyStorageFunctions' , ( ) => { let count = 0 ; function nextProvider ( ) { return 'storage-test-' + count ++ ; } it ( 'Storage functions' , ( ) => { const provider = nextProvider ( ) ; const testName = `@${ provider }:foo:bar` ; expect ( iconExists ( testName ) ) . toBe ( false ) ; expect ( getIcon ( testName ) ) . toBeNull ( ) ; expect ( listIcons ( provider ) ) . toEqual ( [ ] ) ; expect ( addIcon ( testName , { body : '<g />' , } ) ) . toBe ( true ) ; expect ( iconExists ( testName ) ) . toBe ( true ) ; expect ( listIcons ( provider ) ) . toEqual ( [ testName ] ) ; } ) ; it ( 'Invalid icon name' , ( ) => { const testName = 'storage' + count ++ ; allowSimpleNames ( false ) ; expect ( iconExists ( testName ) ) . toBe ( false ) ; expect ( getIcon ( testName ) ) . toBeNull ( ) ; expect ( addIcon ( testName , { body : '<g />' , } ) ) . toBe ( false ) ; expect ( iconExists ( testName ) ) . toBe ( false ) ; } ) ; it ( 'Invalid icon set' , ( ) => { allowSimpleNames ( false ) ; expect ( addCollection ( { prefix : '' , icons : { foo : { body : '<g />' , } , } , } ) ) . toBe ( false ) ; } ) ; it ( 'Simple icon name' , ( ) => { const testName = 'storage' + count ++ ; allowSimpleNames ( true ) ; expect ( iconExists ( testName ) ) . toBe ( false ) ; expect ( getIcon ( testName ) ) . toBeNull ( ) ; expect ( addIcon ( testName , { body : '<g />' , } ) ) . toBe ( true ) ; expect ( iconExists ( testName ) ) . toBe ( true ) ; allowSimpleNames ( false ) ; } ) ; it ( 'Collection with simple icon name' , ( ) => { const n = count ++ ; const n2 = count ++ ; let name ; allowSimpleNames ( true ) ; const name1 = 'test' + n ; const prefix2 = `prefixed${ n }` ; const name2 = `icon${ n2 }` ; expect ( addCollection ( { prefix : '' , icons : { [ name1 ] : { body : '<g data-icon="basic-icon" />' , } , [ `${ prefix2 }-${ name2 }` ] : { body : '<g data-icon="prefixed-icon" />' , } , } , } ) ) . toBe ( true ) ; name = name1 ; expect ( iconExists ( name ) ) . toBe ( true ) ; expect ( getIcon ( name ) ) . toEqual ( fullIcon ( { body : '<g data-icon="basic-icon" />' , } ) ) ; name = `${ prefix2 }:${ name2 }` ; expect ( listIcons ( '' , prefix2 ) ) . toEqual ( [ name ] ) ; expect ( iconExists ( name ) ) . toBe ( true ) ; expect ( getIcon ( name ) ) . toEqual ( fullIcon ( { body : '<g data-icon="prefixed-icon" />' , } ) ) ; name = `${ prefix2 }-${ name2 }` ; expect ( iconExists ( name ) ) . toBe ( true ) ; expect ( getIcon ( name ) ) . toEqual ( fullIcon ( { body : '<g data-icon="prefixed-icon" />' , } ) ) ; allowSimpleNames ( false ) ; } ) ; } ) ;