/**
 * Call this function to mount a logic file and optionally override default values.
 * Automatically DRYs out a lot of cruft for us, such as resetting context, creating the
 * nested defaults path obj (see https://kea.js.org/docs/api/context#resetcontext), and
 * returning an unmount function
 *
 * Example usage:
 *
 * import { LogicMounter } from '../../../__mocks__/kea_logic';
 * import { SomeLogic } from './';
 *
 * const { mount, unmount } = new LogicMounter(SomeLogic);
 *
 * it('some test', () => {
 *   mount({ someValue: 'hello' }, { someProp: 'world' });
 *   unmount();
 * });
 */ import { resetContext , LogicWrapper } from 'kea' ; type LogicFile = LogicWrapper < any > ; export class LogicMounter { private logicFile : LogicFile ; private unmountFn ! : Function ; constructor ( logicFile ) { this . logicFile = logicFile ; } public resetContext = ( values ? , props ? ) => { if ( ! values || ! Object . keys ( values ) . length ) { resetContext ( { } ) ; } else { let { path , key } = this . logicFile . inputs [ 0 ] ; if ( this . logicFile . _isKeaWithKey ) { key = key ( props ) ; path = path ( key ) ; } const defaults = path . reduceRight ( ( value , name ) => ( { [ name ] : value } ) , values ) ; resetContext ( { defaults } ) ; } } ; public mount = ( values ? , props ? ) => { this . resetContext ( values , props ) ; const logicWithProps = this . logicFile . build ( props ) ; this . unmountFn = logicWithProps . mount ( ) ; return logicWithProps ; } ; public unmount = ( ) => { this . unmountFn ( ) ; } ; /**
     * Some tests (e.g. async tests, tests that expect thrown errors) need to access
     * listener functions directly instead of calling `SomeLogic.actions.someListener`,
     * due to how Kea invokes/wraps action fns by design.
     *
     * Example usage:
     *
     * const { mount, getListeners } = new LogicMounter(SomeLogic);
     *
     * it('some test', async () => {
     *   mount();
     *   const { someListener } = getListeners({ values: { someMockValue: false } });
     *
     *   const mockBreakpoint = jest.fn();
     *   await someListener({ someMockArgument: true }, mockBreakpoint);
     * });
     */ public getListeners = ( listenersArgs = { } ) => { const { listeners } = this . logicFile . inputs [ 0 ] ; return typeof listeners === 'function' ? listeners ( listenersArgs ) : listeners ; } ; }