import { expect } from "chai" ; import { ObservableSet } from "../core-bentley" ; class Listener { private _added = false ; private _deleted = false ; private _cleared = false ; public constructor ( set ) { set . onAdded . addListener ( ( _ ) => this . _added = true ) ; set . onDeleted . addListener ( ( _ ) => this . _deleted = true ) ; set . onCleared . addListener ( ( ) => this . _cleared = true ) ; } private clear ( ) { this . _added = this . _deleted = this . _cleared ; } public expect ( added , deleted , cleared , func ) { this . clear ( ) ; func ( ) ; expect ( this . _added ) . to . equal ( added ) ; expect ( this . _deleted ) . to . equal ( deleted ) ; expect ( this . _cleared ) . to . equal ( cleared ) ; this . clear ( ) ; } public expectNone ( func ) { this . expect ( false , false , false , func ) ; } public expectAdd ( func ) { this . expect ( true , false , false , func ) ; } public expectDelete ( func ) { this . expect ( false , true , false , func ) ; } public expectClear ( func ) { this . expect ( false , false , true , func ) ; } } describe ( "ObservableSet" , ( ) => { it ( "should raise events only when contents change" , ( ) => { const set = new ObservableSet < string > ( ) ; const listener = new Listener ( set ) ; listener . expectNone ( ( ) => { set . clear ( ) ; set . delete ( "abc" ) ; } ) ; listener . expectAdd ( ( ) => set . add ( "abc" ) ) ; listener . expectAdd ( ( ) => set . add ( "def" ) ) ; listener . expectNone ( ( ) => set . add ( "abc" ) ) ; listener . expectDelete ( ( ) => set . delete ( "def" ) ) ; listener . expectClear ( ( ) => set . clear ( ) ) ; } ) ; it ( "should construct from iterable" , ( ) => { const elems = [ "a" , "b" , "c" ] ; const observable = new ObservableSet < string > ( elems ) ; const set = new Set < string > ( elems ) ; expect ( observable ) . to . deep . equal ( set ) ; } ) ; } ) ;