| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import * as widgets from '../../lib'; |
| 5 |
import * as Backbone from 'backbone'; |
| 6 |
import * as base from '@jupyter-widgets/base'; |
| 7 |
import { WidgetModel, WidgetView } from '@jupyter-widgets/base'; |
| 8 |
import { ManagerBase } from '@jupyter-widgets/base-manager'; |
| 9 |
|
| 10 |
let numComms = 0; |
| 11 |
|
| 12 |
export class MockComm { |
| 13 |
constructor() { |
| 14 |
this.comm_id = `mock-comm-id-${numComms}`; |
| 15 |
numComms += 1; |
| 16 |
} |
| 17 |
on_open(fn: Function): void { |
| 18 |
this._on_open = fn; |
| 19 |
} |
| 20 |
on_close(fn: Function): void { |
| 21 |
this._on_close = fn; |
| 22 |
} |
| 23 |
on_msg(fn: Function): void { |
| 24 |
this._on_msg = fn; |
| 25 |
} |
| 26 |
_process_msg(msg: any): any { |
| 27 |
if (this._on_msg) { |
| 28 |
return this._on_msg(msg); |
| 29 |
} else { |
| 30 |
return Promise.resolve(); |
| 31 |
} |
| 32 |
} |
| 33 |
open(): string { |
| 34 |
if (this._on_open) { |
| 35 |
this._on_open(); |
| 36 |
} |
| 37 |
return ''; |
| 38 |
} |
| 39 |
close(): string { |
| 40 |
if (this._on_close) { |
| 41 |
this._on_close(); |
| 42 |
} |
| 43 |
return ''; |
| 44 |
} |
| 45 |
send(): string { |
| 46 |
return ''; |
| 47 |
} |
| 48 |
comm_id: string; |
| 49 |
target_name: string; |
| 50 |
_on_msg: Function | null = null; |
| 51 |
_on_open: Function | null = null; |
| 52 |
_on_close: Function | null = null; |
| 53 |
} |
| 54 |
|
| 55 |
class TestWidget extends base.WidgetModel { |
| 56 |
defaults(): Backbone.ObjectHash { |
| 57 |
return { |
| 58 |
...super.defaults(), |
| 59 |
_model_module: 'test-widgets', |
| 60 |
_model_name: 'TestWidget', |
| 61 |
_model_module_version: '1.0.0', |
| 62 |
_view_module: 'test-widgets', |
| 63 |
_view_name: 'TestWidgetView', |
| 64 |
_view_module_version: '1.0.0', |
| 65 |
_view_count: null as any, |
| 66 |
}; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
class TestWidgetView extends base.WidgetView { |
| 71 |
render(): void { |
| 72 |
this._rendered += 1; |
| 73 |
super.render(); |
| 74 |
} |
| 75 |
remove(): void { |
| 76 |
this._removed += 1; |
| 77 |
super.remove(); |
| 78 |
} |
| 79 |
_removed = 0; |
| 80 |
_rendered = 0; |
| 81 |
} |
| 82 |
|
| 83 |
const testWidgets = { TestWidget, TestWidgetView }; |
| 84 |
|
| 85 |
export class DummyManager extends ManagerBase { |
| 86 |
constructor() { |
| 87 |
super(); |
| 88 |
this.el = window.document.createElement('div'); |
| 89 |
} |
| 90 |
|
| 91 |
protected loadClass( |
| 92 |
className: string, |
| 93 |
moduleName: string, |
| 94 |
moduleVersion: string |
| 95 |
): Promise<typeof WidgetModel | typeof WidgetView> { |
| 96 |
if (moduleName === '@jupyter-widgets/controls') { |
| 97 |
if ((widgets as any)[className]) { |
| 98 |
return Promise.resolve((widgets as any)[className]); |
| 99 |
} else { |
| 100 |
return Promise.reject(`Cannot find class ${className}`); |
| 101 |
} |
| 102 |
} else if (moduleName === 'test-widgets') { |
| 103 |
if ((testWidgets as any)[className]) { |
| 104 |
return Promise.resolve((testWidgets as any)[className]); |
| 105 |
} else { |
| 106 |
return Promise.reject(`Cannot find class ${className}`); |
| 107 |
} |
| 108 |
} else { |
| 109 |
return Promise.reject(`Cannot find module ${moduleName}`); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
_get_comm_info(): Promise<{}> { |
| 114 |
return Promise.resolve({}); |
| 115 |
} |
| 116 |
|
| 117 |
_create_comm(): Promise<MockComm> { |
| 118 |
return Promise.resolve(new MockComm()); |
| 119 |
} |
| 120 |
|
| 121 |
el: HTMLElement; |
| 122 |
testClasses: { [key: string]: any } = testWidgets; |
| 123 |
} |