UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const jsdom_1 = require("./test/jsdom");
19let disableJsDom = (0, jsdom_1.enableJSDOM)();
20const inversify_1 = require("inversify");
21const chai_1 = require("chai");
22const widget_manager_1 = require("./widget-manager");
23const widgets_1 = require("@phosphor/widgets");
24const logger_1 = require("../common/logger");
25const mock_logger_1 = require("../common/test/mock-logger");
26const common_1 = require("../common");
27disableJsDom();
28class 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}
40describe('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//# sourceMappingURL=widget-manager.spec.js.map
\No newline at end of file