import * as widgets from '@jupyter-widgets/base' ; import * as services from '@jupyterlab/services' ; import { DummyManager } from './dummy-manager' ; let numComms = 0 ; export class MockComm implements widgets . IClassicComm { constructor ( ) { this . comm_id = `mock-comm-id-${ numComms }` ; numComms += 1 ; } on_close ( fn ) { this . _on_close = fn ; } on_msg ( fn ) { this . _on_msg = fn ; } _process_msg ( msg ) { if ( this . _on_msg ) { return this . _on_msg ( msg ) ; } else { return Promise . resolve ( ) ; } } open ( data ? , metadata ? , buffers ? ) { if ( this . _on_open ) { this . _on_open ( ) ; } return '' ; } close ( data ? , metadata ? , buffers ? ) { if ( this . _on_close ) { this . _on_close ( ) ; } return '' ; } send ( data ? , metadata ? , buffers ? ) { return '' ; } comm_id : string ; target_name : string ; _on_msg : Function | null = null ; _on_close : Function | null = null ; _on_open : Function | null = null ; } export interface IConstructor < T > { new ( attributes ? , options ? ) : T ; } export function createTestModel < T extends widgets . WidgetModel > ( constructor , attributes ? , widget_manager ? ) { const id = widgets . uuid ( ) ; const modelOptions = { widget_manager : widget_manager || new DummyManager ( ) , model_id : id , } ; return new constructor ( attributes , modelOptions ) ; } export async function createTestModelFromSerialized < T extends widgets . WidgetModel > ( constructor , state ? , widget_manager ? ) { widget_manager = widget_manager || new DummyManager ( ) ; const attributes = await ( constructor as any ) . _deserialize_state ( state , widget_manager ) ; return createTestModel ( constructor , attributes , widget_manager ) ; } export function createTestView < T extends widgets . WidgetView > ( model , viewCtor ) { const mgr = model . widget_manager as DummyManager ; mgr . testClasses [ model . get ( '_view_name' ) ] = viewCtor ; return model . widget_manager . create_view ( model , undefined ) as any ; }