import { encode as encodeRison } from 'rison-node' ; import { mockStorage } from '../../storage/hashed_item_store/mock' ; import { createStateHash , isStateHash } from './state_hash' ; describe ( 'stateHash' , ( ) => { beforeEach ( ( ) => { mockStorage . clear ( ) ; } ) ; describe ( '#createStateHash' , ( ) => { it ( 'returns a hash' , ( ) => { const json = JSON . stringify ( { a : 'a' } ) ; const hash = createStateHash ( json ) ; expect ( isStateHash ( hash ) ) . toBe ( true ) ; } ) ; it ( 'returns the same hash for the same input' , ( ) => { const json = JSON . stringify ( { a : 'a' } ) ; const hash1 = createStateHash ( json ) ; const hash2 = createStateHash ( json ) ; expect ( hash1 ) . toEqual ( hash2 ) ; } ) ; it ( 'returns a different hash for different input' , ( ) => { const json1 = JSON . stringify ( { a : 'a' } ) ; const hash1 = createStateHash ( json1 ) ; const json2 = JSON . stringify ( { a : 'b' } ) ; const hash2 = createStateHash ( json2 ) ; expect ( hash1 ) . not . toEqual ( hash2 ) ; } ) ; } ) ; describe ( '#isStateHash' , ( ) => { it ( 'returns true for values created using #createStateHash' , ( ) => { const json = JSON . stringify ( { a : 'a' } ) ; const hash = createStateHash ( json ) ; expect ( isStateHash ( hash ) ) . toBe ( true ) ; } ) ; it ( 'returns false for values not created using #createStateHash' , ( ) => { const json = JSON . stringify ( { a : 'a' } ) ; expect ( isStateHash ( json ) ) . toBe ( false ) ; } ) ; it ( 'returns false for RISON' , ( ) => { const rison = encodeRison ( { a : 'a' } ) ; expect ( isStateHash ( rison ) ) . toBe ( false ) ; } ) ; } ) ; } ) ;