@eclipse-emfcloud/model-manager
Version:
Command-based model editing with undo/redo.
192 lines • 8.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// *****************************************************************************
// Copyright (C) 2023-2024 STMicroelectronics.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: MIT License which is
// available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: EPL-2.0 OR MIT
// *****************************************************************************
const chai_1 = __importStar(require("chai"));
const chai_like_1 = __importDefault(require("chai-like"));
const sinon_1 = require("sinon");
const impl_1 = require("../../impl");
const patch_1 = require("../../patch");
const command_stack_1 = require("../command-stack");
const model_manager_1 = require("../model-manager");
chai_1.default.use(chai_like_1.default);
const workingCopyManager = {
isOpen: () => true,
open: () => undefined,
commit: () => undefined,
cancel: () => undefined,
getModel: () => ({}),
getWorkingCopy: () => ({}),
};
const fakeModelManager = (sandbox) => {
const result = {};
result.getModel = sandbox.fake((modelId) => ({
name: modelId,
}));
result.setModel = sandbox.fake();
result.getModelId = sandbox.fake((model) => model.name);
result.getModelIds = sandbox.fake(() => ['A Model']);
result.removeModel = sandbox.fake((modelId) => ({
name: modelId,
}));
result.getCommandStack = sandbox.fake.returns(new impl_1.CoreCommandStackImpl(workingCopyManager));
result.subscribe = sandbox.fake.returns({
subscribed: true,
});
return result;
};
/** Cast an object's method as a verifiable Sinon spy. */
const verify = (func) => func;
describe('ModelManagerImpl', () => {
let sandbox;
let core;
let modelManager;
const model = { name: 'a' };
beforeEach(() => {
sandbox = (0, sinon_1.createSandbox)();
core = fakeModelManager(sandbox);
modelManager = (0, model_manager_1.createModelManager)(core);
});
afterEach(() => {
sandbox.restore();
});
it('createModelManager()', () => {
(0, chai_1.expect)(modelManager).to.haveOwnProperty('delegate').that.equals(core);
});
it('createModelManager() with default delegate', () => {
const myModelManager = (0, model_manager_1.createModelManager)();
(0, chai_1.expect)(myModelManager)
.to.haveOwnProperty('delegate')
.that.is.an.instanceOf(impl_1.CoreModelManagerImpl);
});
it('getModel()', () => {
const result = modelManager.getModel('a');
(0, chai_1.expect)(verify(core.getModel).calledWith('a')).to.be.true;
(0, chai_1.expect)(result).to.be.like(model);
});
it('setModel()', () => {
modelManager.setModel('a', model);
(0, chai_1.expect)(verify(core.setModel).calledWith('a', model)).to.be.true;
});
it('getModelId()', () => {
const result = modelManager.getModelId(model);
(0, chai_1.expect)(verify(core.getModelId).calledWith(model)).to.be.true;
(0, chai_1.expect)(result).to.be.equal('a');
});
it('getModelIds()', () => {
const result = modelManager.getModelIds();
(0, chai_1.expect)(verify(core.getModelIds).called).to.be.true;
(0, chai_1.expect)(result).to.eql(['A Model']);
});
it('removeModel()', () => {
const result = modelManager.removeModel('a');
(0, chai_1.expect)(verify(core.removeModel).calledWith('a')).to.be.true;
(0, chai_1.expect)(result).to.be.like(model);
});
it('subscribe()', () => {
const result = modelManager.subscribe();
(0, chai_1.expect)(verify(core.subscribe).called).to.be.true;
(0, chai_1.expect)(verify(core.subscribe).neverCalledWithMatch(sinon_1.match.defined), 'subscribe was given an defined argument').to.be.true;
(0, chai_1.expect)(result).to.be.equal(verify(core.subscribe).lastCall.returnValue);
});
it('subscribe() with model ID', () => {
const result = modelManager.subscribe('a');
(0, chai_1.expect)(verify(core.subscribe).calledWith('a')).to.be.true;
(0, chai_1.expect)(result).to.be.equal(verify(core.subscribe).lastCall.returnValue);
});
describe('getCommandStack()', () => {
it('creates a stack', () => {
const stack = modelManager.getCommandStack('my.stack');
(0, chai_1.expect)(stack, 'no stack provided').to.exist;
(0, chai_1.expect)(stack, 'wrong kind of stack').to.be.an.instanceOf(command_stack_1.CommandStackImpl);
(0, chai_1.expect)(stack, 'stack has wrong editing context').to.be.like({
editingContext: 'my.stack',
});
(0, chai_1.expect)(verify(core.getCommandStack).called, 'delegate not retrieved').to
.be.true;
(0, chai_1.expect)(stack, 'wrong delegate stack')
.to.have.property('delegate')
.that.is.equal(verify(core.getCommandStack).firstCall.returnValue);
});
it('allows empty id', () => {
const stack1 = modelManager.getCommandStack('');
const stack2 = modelManager.getCommandStack('my.stack');
(0, chai_1.expect)(stack2, 'no stack provided for empty ID').to.exist;
(0, chai_1.expect)(stack2, 'should not be the same stack').not.to.equal(stack1);
});
});
describe('getCommandStackIds', () => {
it('no command stacks', () => {
const stackIds = modelManager.getCommandStackIds();
(0, chai_1.expect)(verify(core.getCommandStack).called, 'delegate not retrieved').to
.be.true;
(0, chai_1.expect)(stackIds, 'unexpected command stacks').to.be.empty;
});
it('has command stacks', async () => {
const stack1 = modelManager.getCommandStack('stack.1');
let stackIds = modelManager.getCommandStackIds();
(0, chai_1.expect)(stackIds, 'command stack having no history was returned').to.be
.empty;
await stack1.execute((0, patch_1.createModelUpdaterCommand)('Test', 'testing', (model) => {
model.name = 'Alice';
}));
stackIds = modelManager.getCommandStackIds();
(0, chai_1.expect)(stackIds, 'command stack having a history was not returned').to.eql(['stack.1']);
});
});
describe('command stack options', () => {
it('default options', () => {
const stack = modelManager.getCommandStack('stack.1');
(0, chai_1.expect)(stack)
.to.have.property('options')
.that.is.like({ keepHistory: true });
});
it('keepHistory option false', () => {
const stack = modelManager.getCommandStack('stack.1', {
keepHistory: false,
});
(0, chai_1.expect)(stack)
.to.have.property('options')
.that.is.like({ keepHistory: false });
});
});
});
//# sourceMappingURL=model-manager-impl.spec.js.map