UNPKG

sprotty

Version:

A next-gen framework for graphical views

103 lines (86 loc) 4.04 kB
/******************************************************************************** * Copyright (c) 2017-2019 TypeFox and others. * * 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: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ import 'reflect-metadata'; import { expect, describe, it } from 'vitest'; import { Container } from 'inversify'; import { TYPES } from '../../base/types'; import { ConsoleLogger } from '../../utils/logging'; import { SModelRootImpl } from '../../base/model/smodel'; import { EMPTY_ROOT, IModelFactory } from '../../base/model/smodel-factory'; import { CommandExecutionContext } from '../../base/commands/command'; import { AnimationFrameSyncer } from '../../base/animations/animation-frame-syncer'; import { BringToFrontCommand } from './zorder'; import defaultModule from '../../base/di.config'; import { SNodeImpl } from '../../graph/sgraph'; import { BringToFrontAction } from 'sprotty-protocol'; function getNode(nodeId: string, model: SModelRootImpl) { return <SNodeImpl>model.index.getById(nodeId); } function getNodeIndex(nodeId: string, model: SModelRootImpl) { return model.children.indexOf(getNode(nodeId, model)); } describe('BringToFrontCommand', () => { const container = new Container(); container.load(defaultModule); const graphFactory = container.get<IModelFactory>(TYPES.IModelFactory); const myNode0 = {id: 'node0', type: 'node:circle', x: 100, y: 100, selected: true}; const myNode1 = {id: 'node1', type: 'node:circle', x: 200, y: 200, selected: false}; const initialModel = graphFactory.createRoot({ id: 'graph', type: 'graph', children: [myNode1, myNode0] // myNode0 is selected, so put at the end }); const lastIndex = initialModel.children.length - 1; // Create the z-order action const myZOrderAction = BringToFrontAction.create(['node1']); // Create the z-order command const cmd = new BringToFrontCommand(myZOrderAction); // Global so we can carry-over the model, as it's updated, // from test case to test case (i,e, select, undo, redo) let newModel: SModelRootImpl; const context: CommandExecutionContext = { root: graphFactory.createRoot(EMPTY_ROOT), modelFactory: graphFactory, duration: 0, modelChanged: undefined!, logger: new ConsoleLogger(), syncer: new AnimationFrameSyncer() }; it('execute() works as expected', () => { // Execute command context.root = initialModel; newModel = cmd.execute(context); // The selected node is moved at the end of the array expect(lastIndex).to.equal(getNodeIndex('node1', newModel)); expect(0).to.equal(getNodeIndex('node0', newModel)); }); it('undo() works as expected', () => { // Test 'undo' context.root = newModel; newModel = cmd.undo(context); // The selected node is moved at the end of the array expect(lastIndex).to.equal(getNodeIndex('node0', newModel)); expect(0).to.equal(getNodeIndex('node1', newModel)); }); it('redo() works as expected', () => { // Test 'redo' context.root = newModel; newModel = cmd.redo(context); // The selected node is moved at the end of the array expect(lastIndex).to.equal(getNodeIndex('node1', newModel)); expect(0).to.equal(getNodeIndex('node0', newModel)); }); });