Browse Source

Up CommandAbstract

pull/5399/head
Artur Arseniev 3 years ago
parent
commit
c1623cea31
  1. 60
      test/specs/commands/view/CommandAbstract.js
  2. 61
      test/specs/commands/view/CommandAbstract.ts

60
test/specs/commands/view/CommandAbstract.js

@ -1,60 +0,0 @@
import CommandAbstract from 'commands/view/CommandAbstract';
import Editor from 'editor/model/Editor';
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;
});
test('callRun returns result when no "abort" option specified', () => {
const runStub = sinon.stub(command, 'run').returns('result');
const result = command.callRun(editor);
expect(editorTriggerSpy.callCount).toEqual(3);
expect(editorTriggerSpy.getCall(0).args).toEqual(['run:test:before', {}]);
expect(editorTriggerSpy.getCall(1).args).toEqual(['run:test', 'result', {}]);
expect(editorTriggerSpy.getCall(2).args).toEqual(['run', 'test', 'result', {}]);
expect(result).toEqual('result');
expect(runStub.calledOnce).toEqual(true);
});
test('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);
});
test('callStop returns result', () => {
const stopStub = sinon.stub(command, 'stop').returns('stopped');
const result = command.callStop(editor);
expect(editorTriggerSpy.callCount).toEqual(3);
expect(editorTriggerSpy.getCall(0).args).toEqual(['stop:test:before', {}]);
expect(editorTriggerSpy.getCall(1).args).toEqual(['stop:test', 'stopped', {}]);
expect(editorTriggerSpy.getCall(2).args).toEqual(['stop', 'test', 'stopped', {}]);
expect(result).toEqual('stopped');
expect(stopStub.calledOnce).toEqual(true);
});
});

61
test/specs/commands/view/CommandAbstract.ts

@ -0,0 +1,61 @@
import CommandAbstract from '../../../../src/commands/view/CommandAbstract';
import Editor from '../../../../src/editor';
describe('CommandAbstract', () => {
let editor: Editor;
let command: CommandAbstract;
beforeEach(() => {
editor = new Editor();
command = new CommandAbstract({});
command.id = 'test';
});
test('callRun returns result when no "abort" option specified', () => {
const returnValue = 'result';
const triggerSpy = jest.spyOn(editor, 'trigger');
const runSpy = jest.spyOn(command, 'run');
runSpy.mockReturnValue(returnValue as any);
const result = command.callRun(editor);
expect(triggerSpy.mock.calls.length).toBe(3);
expect(triggerSpy.mock.calls[0]).toEqual(['run:test:before', {}]);
expect(triggerSpy.mock.calls[1]).toEqual(['run:test', returnValue, {}]);
expect(triggerSpy.mock.calls[2]).toEqual(['run', 'test', returnValue, {}]);
expect(runSpy).toBeCalledTimes(1);
expect(result).toEqual(returnValue);
});
test('callRun returns undefined when "abort" option is specified', () => {
const returnValue = 'result';
const opts = { abort: true };
const triggerSpy = jest.spyOn(editor, 'trigger');
const runSpy = jest.spyOn(command, 'run');
runSpy.mockReturnValue(returnValue as any);
const result = command.callRun(editor, opts);
expect(triggerSpy.mock.calls.length).toBe(2);
expect(triggerSpy.mock.calls[0]).toEqual(['run:test:before', opts]);
expect(triggerSpy.mock.calls[1]).toEqual(['abort:test', opts]);
expect(runSpy).toBeCalledTimes(0);
expect(result).toEqual(undefined);
});
test('callStop returns result', () => {
const returnValue = 'stopped';
const triggerSpy = jest.spyOn(editor, 'trigger');
const runSpy = jest.spyOn(command, 'stop');
runSpy.mockReturnValue(returnValue as any);
const result = command.callStop(editor);
expect(triggerSpy.mock.calls.length).toBe(3);
expect(triggerSpy.mock.calls[0]).toEqual(['stop:test:before', {}]);
expect(triggerSpy.mock.calls[1]).toEqual(['stop:test', returnValue, {}]);
expect(triggerSpy.mock.calls[2]).toEqual(['stop', 'test', returnValue, {}]);
expect(runSpy).toBeCalledTimes(1);
expect(result).toEqual(returnValue);
});
});
Loading…
Cancel
Save