1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const jsdom_1 = require("./test/jsdom");
|
19 | let disableJsDom = (0, jsdom_1.enableJSDOM)();
|
20 | const inversify_1 = require("inversify");
|
21 | const chai_1 = require("chai");
|
22 | const widget_manager_1 = require("./widget-manager");
|
23 | const widgets_1 = require("@phosphor/widgets");
|
24 | const logger_1 = require("../common/logger");
|
25 | const mock_logger_1 = require("../common/test/mock-logger");
|
26 | const common_1 = require("../common");
|
27 | disableJsDom();
|
28 | class TestWidgetFactory {
|
29 | constructor() {
|
30 | this.invocations = 0;
|
31 | this.id = 'test';
|
32 | }
|
33 | async createWidget(name) {
|
34 | this.invocations++;
|
35 | const result = new widgets_1.Widget;
|
36 | result.id = name;
|
37 | return result;
|
38 | }
|
39 | }
|
40 | describe('widget-manager', () => {
|
41 | let widgetManager;
|
42 | before(() => {
|
43 | disableJsDom = (0, jsdom_1.enableJSDOM)();
|
44 | });
|
45 | beforeEach(() => {
|
46 | const testContainer = new inversify_1.Container();
|
47 | const module = new inversify_1.ContainerModule(bind => {
|
48 | bind(logger_1.ILogger).to(mock_logger_1.MockLogger);
|
49 | (0, common_1.bindContributionProvider)(bind, widget_manager_1.WidgetFactory);
|
50 | bind(widget_manager_1.WidgetFactory).toConstantValue(new TestWidgetFactory());
|
51 | bind(widget_manager_1.WidgetManager).toSelf().inSingletonScope();
|
52 | });
|
53 | testContainer.load(module);
|
54 | widgetManager = testContainer.get(widget_manager_1.WidgetManager);
|
55 | });
|
56 | after(() => {
|
57 | disableJsDom();
|
58 | });
|
59 | it('creates and caches widgets', async () => {
|
60 | const wA = await widgetManager.getOrCreateWidget('test', 'widgetA');
|
61 | const wB = await widgetManager.getOrCreateWidget('test', 'widgetB');
|
62 | (0, chai_1.expect)(wA).not.equals(wB);
|
63 | (0, chai_1.expect)(wA).equals(await widgetManager.getOrCreateWidget('test', 'widgetA'));
|
64 | });
|
65 | describe('tryGetWidget', () => {
|
66 | it('Returns undefined if the widget has not been created', () => {
|
67 | (0, chai_1.expect)(widgetManager.tryGetWidget('test', 'widgetA')).to.be.undefined;
|
68 | });
|
69 | it('Returns undefined if the widget is created asynchronously and has not finished being created', () => {
|
70 | widgetManager.getOrCreateWidget('test', 'widgetA');
|
71 | (0, chai_1.expect)(widgetManager.tryGetWidget('test', 'widgetA')).to.be.undefined;
|
72 | });
|
73 | it('Returns the widget if the widget is created asynchronously and has finished being created', async () => {
|
74 | await widgetManager.getOrCreateWidget('test', 'widgetA');
|
75 | (0, chai_1.expect)(widgetManager.tryGetWidget('test', 'widgetA')).not.to.be.undefined;
|
76 | });
|
77 | });
|
78 | it('produces the same widget key regardless of object key order', () => {
|
79 | const options1 = {
|
80 | factoryId: 'a',
|
81 | key1: 1,
|
82 | key2: 2,
|
83 | };
|
84 | const options2 = {
|
85 | key2: 2,
|
86 | key1: 1,
|
87 | factoryId: 'a',
|
88 | };
|
89 | (0, chai_1.expect)(widgetManager['toKey'](options1)).equals(widgetManager['toKey'](options2));
|
90 | });
|
91 | });
|
92 |
|
\ | No newline at end of file |