import { normalize } from '../../src/utils/normalize-args' ; import { stableHash as hash } from '../../src/utils/hash' ; import { serialize } from '../../src/utils/serialize' ; import { mergeConfigs } from '../../src/utils/merge-config' ; describe ( 'Utils' , ( ) => { it ( 'should normalize arguments correctly' , async ( ) => { const fetcher = ( ) => { } ; const opts = { revalidateOnFocus : false } ; expect ( normalize ( [ 'key' ] ) ) . toEqual ( [ 'key' , null , { } ] ) ; expect ( normalize ( [ 'key' , null ] ) ) . toEqual ( [ 'key' , null , { } ] ) ; expect ( normalize ( [ 'key' , fetcher ] ) ) . toEqual ( [ 'key' , fetcher , { } ] ) ; expect ( normalize ( [ 'key' , opts ] ) ) . toEqual ( [ 'key' , null , opts ] ) ; expect ( normalize ( [ 'key' , null , opts ] ) ) . toEqual ( [ 'key' , null , opts ] ) ; expect ( normalize ( [ 'key' , fetcher , opts ] ) ) . toEqual ( [ 'key' , fetcher , opts ] ) ; } ) ; it ( 'should hash arguments correctly' , async ( ) => { expect ( serialize ( [ ] ) [ 0 ] ) . toEqual ( '' ) ; expect ( hash ( [ 'key' ] ) ) . toEqual ( '@"key",' ) ; expect ( hash ( [ 1 ] ) ) . toEqual ( '@1,' ) ; expect ( hash ( [ 'false' ] ) ) . toEqual ( '@"false",' ) ; expect ( hash ( [ false ] ) ) . toEqual ( '@false,' ) ; expect ( hash ( [ true ] ) ) . toEqual ( '@true,' ) ; expect ( hash ( [ null ] ) ) . toEqual ( '@null,' ) ; expect ( hash ( [ 'null' ] ) ) . toEqual ( '@"null",' ) ; expect ( hash ( [ undefined ] ) ) . toEqual ( '@undefined,' ) ; expect ( hash ( [ NaN ] ) ) . toEqual ( '@NaN,' ) ; expect ( hash ( [ Infinity ] ) ) . toEqual ( '@Infinity,' ) ; expect ( hash ( [ '' ] ) ) . toEqual ( '@"",' ) ; expect ( hash ( [ '","' , 1 ] ) ) . not . toEqual ( hash ( [ '' , '' , 1 ] ) ) ; expect ( hash ( [ BigInt ( 1 ) ] ) ) . toEqual ( '@1,' ) ; const date = new Date ( ) ; expect ( hash ( [ date ] ) ) . toEqual ( `@${ date . toJSON ( ) },` ) ; expect ( hash ( [ new Date ( 1234 ) ] ) ) . toEqual ( hash ( [ new Date ( 1234 ) ] ) ) ; expect ( hash ( [ /regex/ ] ) ) . toEqual ( '@/regex/,' ) ; expect ( hash ( [ Symbol ( 'key' ) ] ) ) . toMatch ( '@Symbol(key),' ) ; const symbol = Symbol ( 'foo' ) ; expect ( hash ( [ symbol ] ) ) . toMatch ( hash ( [ symbol ] ) ) ; expect ( hash ( [ Symbol . for ( 'key' ) ] ) ) . toMatch ( hash ( [ Symbol . for ( 'key' ) ] ) ) ; expect ( hash ( [ Symbol ( 'key' ) ] ) ) . toMatch ( hash ( [ Symbol ( 'key' ) ] ) ) ; expect ( hash ( [ Symbol ( 'key' ) ] ) ) . toMatch ( hash ( [ Symbol . for ( 'key' ) ] ) ) ; const set = new Set ( ) ; expect ( hash ( [ set ] ) ) . not . toMatch ( hash ( [ new Set ( ) ] ) ) ; expect ( hash ( [ set ] ) ) . toMatch ( hash ( [ set ] ) ) ; const map = new Map ( ) ; expect ( hash ( [ map ] ) ) . not . toMatch ( hash ( [ new Map ( ) ] ) ) ; expect ( hash ( [ map ] ) ) . toMatch ( hash ( [ map ] ) ) ; const buffer = new ArrayBuffer ( 0 ) ; expect ( hash ( [ buffer ] ) ) . not . toMatch ( hash ( [ new ArrayBuffer ( 0 ) ] ) ) ; expect ( hash ( [ buffer ] ) ) . toMatch ( hash ( [ buffer ] ) ) ; expect ( hash ( [ { x : 1 } ] ) ) . toEqual ( '@#x:1,,' ) ; expect ( hash ( [ { '' : 1 } ] ) ) . toEqual ( '@#:1,,' ) ; expect ( hash ( [ { x : { y : 2 } } ] ) ) . toEqual ( '@#x:#y:2,,,' ) ; expect ( hash ( [ [ ] ] ) ) . toEqual ( '@@,' ) ; expect ( hash ( [ [ [ ] ] ] ) ) . not . toMatch ( hash ( [ [ ] , [ ] ] ) ) ; const o = { } ; o . o = o ; expect ( hash ( [ o ] ) ) . toEqual ( hash ( [ o ] ) ) ; expect ( hash ( [ o ] ) ) . not . toEqual ( hash ( [ { } ] ) ) ; const a = [ ] ; a . push ( a ) ; expect ( hash ( [ a ] ) ) . toEqual ( hash ( [ a ] ) ) ; expect ( hash ( [ a ] ) ) . not . toEqual ( hash ( [ [ ] ] ) ) ; const o2 = { } ; const a2 = [ o2 ] ; o2 . a = a2 ; expect ( hash ( [ o2 ] ) ) . toEqual ( hash ( [ o2 ] ) ) ; expect ( hash ( [ ( ) => { } ] ) ) . toMatch ( /@\d+~,/ ) ; expect ( hash ( [ class { } ] ) ) . toMatch ( /@\d+~,/ ) ; } ) ; it ( 'should always generate the same and stable hash' , async ( ) => { expect ( hash ( [ ( ) => { } , 1 , 'key' , null , { x : 1 } ] ) ) . toMatch ( /@\d+~,1,"key",null,#x:1,,/ ) ; expect ( hash ( [ { x : 1 , y : 2 , z : undefined } ] ) ) . toMatch ( hash ( [ { z : undefined , y : 2 , x : 1 } ] ) ) ; expect ( hash ( [ { x : 1 , y : { a : 1 , b : 2 } , z : undefined } ] ) ) . toMatch ( hash ( [ { y : { b : 2 , a : 1 } , x : 1 } ] ) ) ; const f = ( ) => { } ; expect ( hash ( [ f ] ) ) . toEqual ( hash ( [ f ] ) ) ; expect ( hash ( [ ( ) => { } ] ) ) . not . toEqual ( hash ( [ ( ) => { } ] ) ) ; } ) ; it ( 'should correctly merge configs' , async ( ) => { const a = { a : 1 } , b = { b : 1 } ; expect ( mergeConfigs ( { use : [ a ] } , { use : [ b ] } ) ) . toEqual ( { use : [ a , b ] } ) ; expect ( mergeConfigs ( { fallback : a } , { fallback : b } ) ) . toEqual ( { fallback : { a : 1 , b : 1 } } ) ; } ) ; } ) ;