import { expect } from './utils/chai' ; import * as localforage from 'localforage' ; import '../' ; import { formatChangeArg } from './utils/formatChangeArg' ; describe ( 'Observing methods' , function ( ) { let setItemSubscription ; let clearSubscription ; let setItemObservableLogs ; let clearObservableLogs ; let errorCallCount ; let completeCallCount ; beforeEach ( function ( ) { setItemObservableLogs = [ ] ; clearObservableLogs = [ ] ; errorCallCount = 0 ; completeCallCount = 0 ; } ) ; const runTestScenario = ( ) => localforage . setItem ( 'UserProfile' , { UserName : 'user1' , Password : '12345' , } ) . then ( ( ) => localforage . setItem ( 'UserProfile' , { UserName : 'user1' , Password : '67890' , } ) ) . then ( ( ) => localforage . setItem ( 'UserProfile' , { UserName : 'user1' , Password : '67890' , } ) ) . then ( ( ) => localforage . setItem ( 'test1' , 'value1' ) ) . then ( ( ) => localforage . setItem ( 'test2' , 'value2' ) ) . then ( ( ) => localforage . setItem ( 'test2' , 'value2b' ) ) . then ( ( ) => localforage . setItem ( 'test2' , 'value2b' ) ) . then ( ( ) => localforage . setItem ( 'test3' , 'value3' ) ) . then ( ( ) => localforage . removeItem ( 'test3' ) ) . then ( ( ) => localforage . removeItem ( 'test3' ) ) . then ( ( ) => localforage . clear ( ) ) . then ( ( ) => localforage . clear ( ) ) ; describe ( 'Given a setItem & a clear observable & subscription w/ changeDetection' , function ( ) { beforeEach ( function ( ) { return localforage . ready ( ) . then ( ( ) => localforage . clear ( ) ) . then ( ( ) => { const setItemCallObservable = localforage . newObservable ( { setItem : true , } ) ; setItemSubscription = setItemCallObservable . subscribe ( { next : ( change ) => setItemObservableLogs . push ( formatChangeArg ( change ) ) , error : err => { errorCallCount ++ ; console . error ( 'Found an error!' , err ) ; } , complete : ( ) => { completeCallCount ++ ; } , } ) ; const clearCallObservable = localforage . newObservable ( { clear : true , } ) ; clearSubscription = clearCallObservable . subscribe ( { next : ( change ) => clearObservableLogs . push ( formatChangeArg ( change ) ) , error : err => { errorCallCount ++ ; console . error ( 'Found an error!' , err ) ; } , complete : ( ) => { completeCallCount ++ ; } , } ) ; } ) ; } ) ; it ( 'should observe properly' , function ( ) { return runTestScenario ( ) . then ( function ( ) { setItemSubscription . unsubscribe ( ) ; clearSubscription . unsubscribe ( ) ; return localforage . clear ( ) ; } ) . then ( function ( ) { expect ( setItemObservableLogs ) . to . deep . equal ( [ `setItem('UserProfile', '{"UserName":"user1","Password":"12345"}')` , `setItem('UserProfile', '{"UserName":"user1","Password":"67890"}')` , `setItem('test1', 'value1')` , `setItem('test2', 'value2')` , `setItem('test2', 'value2b')` , `setItem('test3', 'value3')` , ] ) ; expect ( clearObservableLogs ) . to . deep . equal ( [ 'clear()' ] ) ; expect ( errorCallCount ) . to . equal ( 0 ) ; expect ( completeCallCount ) . to . equal ( 0 ) ; } ) ; } ) ; } ) ; describe ( 'Given a setItem & a clear observable & subscription w/o changeDetection' , function ( ) { beforeEach ( function ( ) { return localforage . ready ( ) . then ( ( ) => localforage . clear ( ) ) . then ( ( ) => { const setItemCallObservable = localforage . newObservable ( { setItem : true , changeDetection : false , } ) ; setItemSubscription = setItemCallObservable . subscribe ( { next : ( change ) => setItemObservableLogs . push ( formatChangeArg ( change ) ) , error : err => { errorCallCount ++ ; console . error ( 'Found an error!' , err ) ; } , complete : ( ) => { completeCallCount ++ ; } , } ) ; const clearCallObservable = localforage . newObservable ( { clear : true , changeDetection : false , } ) ; clearSubscription = clearCallObservable . subscribe ( { next : ( change ) => clearObservableLogs . push ( formatChangeArg ( change ) ) , error : err => { errorCallCount ++ ; console . error ( 'Found an error!' , err ) ; } , complete : ( ) => { completeCallCount ++ ; } , } ) ; } ) ; } ) ; it ( 'should observe properly' , function ( ) { return runTestScenario ( ) . then ( function ( ) { setItemSubscription . unsubscribe ( ) ; clearSubscription . unsubscribe ( ) ; return localforage . clear ( ) ; } ) . then ( function ( ) { expect ( setItemObservableLogs ) . to . deep . equal ( [ `setItem('UserProfile', '{"UserName":"user1","Password":"12345"}')` , `setItem('UserProfile', '{"UserName":"user1","Password":"67890"}')` , `setItem('UserProfile', '{"UserName":"user1","Password":"67890"}')` , `setItem('test1', 'value1')` , `setItem('test2', 'value2')` , `setItem('test2', 'value2b')` , `setItem('test2', 'value2b')` , `setItem('test3', 'value3')` , ] ) ; expect ( clearObservableLogs ) . to . deep . equal ( [ 'clear()' , 'clear()' , ] ) ; expect ( errorCallCount ) . to . equal ( 0 ) ; expect ( completeCallCount ) . to . equal ( 0 ) ; } ) ; } ) ; } ) ; } ) ;