import { Dictionary } from './dictionary' ; describe ( 'Dictionary creation' , ( ) => { it ( 'creates a new empty dictionary' , ( ) => { const dictionary = new Dictionary ( ) ; expect ( dictionary ) . toBeDefined ( ) ; expect ( dictionary . count ) . toBe ( 0 ) ; } ) ; it ( 'creates a dictionary from a Map<string, T>' , ( ) => { const dictionary = new Dictionary < any > ( new Map < string , any > ( [ [ 'item1' , 1 ] , [ 'item2' , 'the second item' ] , [ 'item3' , { number : 3 } ] ] ) ) ; expect ( dictionary ) . toBeDefined ( ) ; expect ( dictionary . count ) . toBe ( 3 ) ; expect ( dictionary . get ( 'item1' ) ) . toBe ( 1 ) ; expect ( dictionary . get ( 'item2' ) ) . toBe ( 'the second item' ) ; } ) ; it ( 'creates a dictionary from Array<[string, T]>' , ( ) => { const dictionary = new Dictionary < any > ( [ [ 'item1' , 1 ] , [ 'item2' , 'the second item' ] , [ 'item3' , { number : 3 } ] ] ) ; expect ( dictionary ) . toBeDefined ( ) ; expect ( dictionary . count ) . toBe ( 3 ) ; expect ( dictionary . get ( 'item1' ) ) . toBe ( 1 ) ; expect ( dictionary . get ( 'item2' ) ) . toBe ( 'the second item' ) ; } ) ; it ( 'creates a dictionary from an Object' , ( ) => { const dictionary = new Dictionary < any > ( { item1 : 1 , item2 : 'the second item' , item3 : { number : 3 } } ) ; expect ( dictionary ) . toBeDefined ( ) ; expect ( dictionary . count ) . toBe ( 3 ) ; expect ( dictionary . get ( 'item1' ) ) . toBe ( 1 ) ; expect ( dictionary . get ( 'item2' ) ) . toBe ( 'the second item' ) ; } ) ; it ( 'fails if Dictionary is instantiated with invalid values' , ( ) => { expect ( ( ) => new Dictionary ( 2 as any ) ) . toThrow ( TypeError ) ; expect ( ( ) => new Dictionary ( new Set ( ) as any ) ) . toThrow ( TypeError ) ; expect ( ( ) => new Dictionary ( 'new Set() as any)' as any ) ) . toThrow ( TypeError ) ; } ) ; } ) ; describe ( 'Dictionary operations' , ( ) => { let dictionary ; beforeEach ( ( ) => { dictionary = new Dictionary ( { item1 : 1 , item2 : 'the second item' , item3 : { number : 3 } } ) ; } ) ; describe ( 'Get' , ( ) => { it ( 'returns an object' , ( ) => { expect ( dictionary . get ( 'item1' ) ) . toBe ( 1 ) ; } ) ; it ( 'returns undefined if the object doesn\'t exist' , ( ) => { expect ( dictionary . get ( 'item4' ) ) . toBeUndefined ( ) ; } ) ; it ( 'returns undefined if the key is invalid' , ( ) => { expect ( dictionary . get ( undefined ) ) . toBeUndefined ( ) ; } ) ; } ) ; describe ( 'Set' , ( ) => { it ( 'inserts the object' , ( ) => { const originalCount = dictionary . count ; dictionary . set ( 'item4' , 4 ) ; const item = dictionary . get ( 'item4' ) ; expect ( item ) . toBe ( 4 ) ; expect ( dictionary . count ) . toBe ( originalCount + 1 ) ; } ) ; it ( 'returns the inserted object' , ( ) => { const item = dictionary . set ( 'item4' , 4 ) ; expect ( item ) . toBe ( 4 ) ; } ) ; it ( 'replaces if the key already exists' , ( ) => { dictionary . set ( 'item4' , 4 ) ; let item = dictionary . get ( 'item4' ) ; expect ( item ) . toBe ( 4 ) ; dictionary . set ( 'item4' , 'random' ) ; item = dictionary . get ( 'item4' ) ; expect ( item ) . toBe ( 'random' ) ; } ) ; it ( 'throws if the key is invalid' , ( ) => { expect ( ( ) => dictionary . set ( null , 'random' ) ) . toThrow ( TypeError ) ; expect ( ( ) => dictionary . set ( 2 as any , 'random' ) ) . toThrow ( TypeError ) ; } ) ; } ) ; describe ( 'Delete' , ( ) => { it ( 'deletes the object' , ( ) => { const originalCount = dictionary . count ; dictionary . delete ( 'item3' ) ; const item = dictionary . get ( 'item3' ) ; expect ( item ) . toBeUndefined ( ) ; expect ( dictionary . count ) . toBe ( originalCount - 1 ) ; } ) ; it ( 'returns the deleted object' , ( ) => { const item = dictionary . delete ( 'item1' ) ; expect ( item ) . toBe ( 1 ) ; } ) ; it ( 'throws if the doesn\'t exist' , ( ) => { expect ( ( ) => dictionary . delete ( 'item4' ) ) . toThrow ( ReferenceError ) ; } ) ; it ( 'throws if the key is invalid' , ( ) => { expect ( ( ) => dictionary . delete ( null ) ) . toThrow ( TypeError ) ; expect ( ( ) => dictionary . delete ( 2 as any ) ) . toThrow ( TypeError ) ; } ) ; } ) ; describe ( 'Clear' , ( ) => { it ( 'empties the dictionary' , ( ) => { dictionary . clear ( ) ; const item = dictionary . get ( 'item4' ) ; expect ( item ) . toBeUndefined ( ) ; expect ( dictionary . count ) . toBe ( 0 ) ; } ) ; } ) ; describe ( 'Keys' , ( ) => { it ( 'returns the keys in the dictionary' , ( ) => { const keys = dictionary . keys ( ) ; expect ( keys ) . toBeDefined ( ) ; expect ( keys [ 0 ] ) . toBe ( 'item1' ) ; } ) ; } ) ; describe ( 'Values' , ( ) => { it ( 'returns the values in the dictionary' , ( ) => { const values = dictionary . values ( ) ; expect ( values ) . toBeDefined ( ) ; expect ( values [ 0 ] ) . toBe ( 1 ) ; } ) ; } ) ; describe ( 'Clone' , ( ) => { it ( 'returns a shallow copy of the dictonary' , ( ) => { const dictionaryCopy = dictionary . clone ( ) ; expect ( dictionaryCopy ) . toBeDefined ( ) ; } ) ; it ( 'ensure the copy is shallow' , ( ) => { const dictionaryCopy = dictionary . clone ( ) ; dictionaryCopy . set ( 'item1' , 10 ) ; expect ( dictionaryCopy . get ( 'item1' ) ) . toBe ( 10 ) ; expect ( dictionary . get ( 'item1' ) ) . toBe ( 1 ) ; } ) ; } ) ; describe ( 'Count' , ( ) => { it ( 'returns the count of the dictonary' , ( ) => { const count = dictionary . count ; expect ( count ) . toBe ( 3 ) ; } ) ; } ) ; } ) ;