From b0a0a3fe6be57d9f88da1a21514996566cba1e1a Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 24 Aug 2018 18:53:11 +0200 Subject: [PATCH] Add tests for new methods in Command --- src/commands/index.js | 2 +- test/specs/commands/index.js | 87 ++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/commands/index.js b/src/commands/index.js index 08e104409..66dc08b10 100644 --- a/src/commands/index.js +++ b/src/commands/index.js @@ -286,7 +286,7 @@ module.exports = () => { * commands.stop('myCommand', { someOption: 1 }); */ stop(id, options = {}) { - return this.runCommand(this.get(id), options); + return this.stopCommand(this.get(id), options); }, /** diff --git a/test/specs/commands/index.js b/test/specs/commands/index.js index c5928d1d0..42c5e4fcc 100644 --- a/test/specs/commands/index.js +++ b/test/specs/commands/index.js @@ -1,13 +1,47 @@ +import Backbone from 'backbone'; var Commands = require('commands'); var Models = require('./model/CommandModels'); var CommandAbstract = require('./view/CommandAbstract'); -describe('Commands', () => { +describe.only('Commands', () => { describe('Main', () => { - let obj; + let obj, + commSimple, + commRunOnly, + commFunc, + commName = 'commandTest', + commResultRun = 'Run executed', + commResultStop = 'Stop executed'; + + const mockEditor = { + get(id) { + switch (id) { + case 'Canvas': + return { + getElement: () => ({}), + getWrapperEl: () => ({}), + getFrameEl: () => ({}), + getToolsEl: () => ({}), + getBody: () => ({}) + }; + case 'Editor': + return { ...Backbone.Events }; + default: + } + return null; + } + }; beforeEach(() => { - obj = new Commands().init(); + commSimple = { + run: () => commResultRun, + stop: () => commResultStop + }; + commRunOnly = { + run: () => commResultRun + }; + commFunc = () => commResultRun; + obj = new Commands().init({ em: mockEditor }); }); afterEach(() => { @@ -19,8 +53,11 @@ describe('Commands', () => { }); test('Push new command', () => { - var comm = { test: 'test' }; + const comm = { test: 'test' }; + const len = Object.keys(obj.getAll()).length; obj.add('test', comm); + expect(obj.has('test')).toBe(true); + expect(Object.keys(obj.getAll()).length).toBe(len + 1); expect(obj.get('test').test).toEqual('test'); }); @@ -58,6 +95,48 @@ describe('Commands', () => { test('Commands module should not have toLoad property', () => { expect(obj.toLoad).toBeUndefined(); }); + + test('Run simple command and check if the state is tracked', () => { + // Add the command + obj.add(commName, commSimple); + expect(obj.isActive(commName)).toBe(false); + + // Start the command + let result = obj.run(commName); + expect(result).toBe(commResultRun); + expect(obj.isActive(commName)).toBe(true); + expect(Object.keys(obj.getActive()).length).toBe(1); + + // Stop the command + result = obj.stop(commName); + expect(result).toBe(commResultStop); + expect(obj.isActive(commName)).toBe(false); + expect(Object.keys(obj.getActive()).length).toBe(0); + }); + + test('Run command only with run method, ensure is not tracked', () => { + // Add the command + obj.add(commName, commRunOnly); + expect(obj.isActive(commName)).toBe(false); + + // Start the command + let result = obj.run(commName); + expect(result).toBe(commResultRun); + expect(obj.isActive(commName)).toBe(false); + expect(Object.keys(obj.getActive()).length).toBe(0); + }); + + test('Run function command, ensure is not tracked', () => { + // Add the command + obj.add(commName, commFunc); + expect(obj.isActive(commName)).toBe(false); + + // Start the command + let result = obj.run(commName); + expect(result).toBe(commResultRun); + expect(obj.isActive(commName)).toBe(false); + expect(Object.keys(obj.getActive()).length).toBe(0); + }); }); });