diff --git a/src/commands/view/CommandAbstract.js b/src/commands/view/CommandAbstract.js index 39a5ca65e..3c21dc5c8 100644 --- a/src/commands/view/CommandAbstract.js +++ b/src/commands/view/CommandAbstract.js @@ -107,6 +107,7 @@ module.exports = Backbone.View.extend({ const result = this.run(editor, editor, options); editor.trigger(`run:${id}`, result, options); + return result; }, /** @@ -120,6 +121,7 @@ module.exports = Backbone.View.extend({ editor.trigger(`stop:${id}:before`, options); const result = this.stop(editor, editor, options); editor.trigger(`stop:${id}`, result, options); + return result; }, /** diff --git a/test/specs/commands/index.js b/test/specs/commands/index.js index dc09a0b61..46ce61755 100644 --- a/test/specs/commands/index.js +++ b/test/specs/commands/index.js @@ -1,5 +1,6 @@ var Commands = require('commands'); var Models = require('./model/CommandModels'); +var CommandAbstract = require('./view/CommandAbstract'); describe('Commands', () => { describe('Main', () => { @@ -61,3 +62,4 @@ describe('Commands', () => { }); Models.run(); +CommandAbstract.run(); diff --git a/test/specs/commands/view/CommandAbstract.js b/test/specs/commands/view/CommandAbstract.js new file mode 100644 index 000000000..33c6959df --- /dev/null +++ b/test/specs/commands/view/CommandAbstract.js @@ -0,0 +1,83 @@ +const CommandAbstract = require('commands/view/CommandAbstract'); +const Editor = require('editor/model/Editor'); + +module.exports = { + run() { + describe('CommandAbstract', () => { + let editor, editorTriggerSpy, command; + + beforeEach(() => { + editor = new Editor(); + editorTriggerSpy = sinon.spy(editor, 'trigger'); + + command = new CommandAbstract(); + command.id = 'test'; + }); + + afterEach(() => { + command = null; + editorTriggerSpy = null; + editor = null; + }); + + it('callRun returns result when no "abort" option specified', () => { + const runStub = sinon.stub(command, 'run').returns('result'); + + const result = command.callRun(editor); + + expect(editorTriggerSpy.calledTwice).toEqual(true); + expect(editorTriggerSpy.getCall(0).args).toEqual([ + 'run:test:before', + {} + ]); + expect(editorTriggerSpy.getCall(1).args).toEqual([ + 'run:test', + 'result', + {} + ]); + + expect(result).toEqual('result'); + expect(runStub.calledOnce).toEqual(true); + }); + + it('callRun returns undefined when "abort" option is specified', () => { + const runStub = sinon.stub(command, 'run').returns('result'); + + const result = command.callRun(editor, { abort: true }); + + expect(editorTriggerSpy.calledTwice).toEqual(true); + expect(editorTriggerSpy.getCall(0).args).toEqual([ + 'run:test:before', + { abort: true } + ]); + expect(editorTriggerSpy.getCall(1).args).toEqual([ + 'abort:test', + { abort: true } + ]); + + expect(result).toEqual(undefined); + expect(runStub.notCalled).toEqual(true); + }); + + it('callStop returns result', () => { + const stopStub = sinon.stub(command, 'stop').returns('stopped'); + + const result = command.callStop(editor); + + expect(editorTriggerSpy.calledTwice).toEqual(true); + expect(editorTriggerSpy.getCall(0).args).toEqual([ + 'stop:test:before', + {} + ]); + expect(editorTriggerSpy.getCall(1).args).toEqual([ + 'stop:test', + 'stopped', + {} + ]); + + expect(result).toEqual('stopped'); + expect(stopStub.calledOnce).toEqual(true); + }); + }); + } +};