Skip to content

Commit

Permalink
CommandWrapper was not applying cloneMethod (#382)
Browse files Browse the repository at this point in the history
Related to #367
  • Loading branch information
dubzzz committed Jun 20, 2019
1 parent 0740fcf commit 68a4c78
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/check/model/commands/CommandWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cloneMethod, hasCloneMethod } from '../../symbols';
import { ICommand } from '../command/ICommand';

/** @hidden */
Expand All @@ -13,6 +14,8 @@ export class CommandWrapper<Model extends object, Real, RunResult, CheckAsync ex
return this.cmd.run(m, r);
}
clone(): CommandWrapper<Model, Real, RunResult, CheckAsync> {
if (hasCloneMethod(this.cmd))
return new CommandWrapper<Model, Real, RunResult, CheckAsync>(this.cmd[cloneMethod]());
return new CommandWrapper<Model, Real, RunResult, CheckAsync>(this.cmd);
}
toString(): string {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/check/model/commands/CommandWrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommandWrapper } from '../../../../../src/check/model/commands/CommandWrapper';
import { Command } from '../../../../../src/check/model/command/Command';
import { AsyncCommand } from '../../../../../src/check/model/command/AsyncCommand';
import { cloneMethod } from '../../../../../src/check/symbols';

type Model = {};
type Real = {};
Expand Down Expand Up @@ -85,4 +86,34 @@ describe('CommandWrapper', () => {
await expect(wrapper.run({}, {})).rejects.toMatch('failure message');
expect(wrapper.hasRan).toBe(true);
});
it('Should clone cloneable commands on clone', async () => {
const cloneMethodOut = {};
const cloneMethodMock = jest.fn();
cloneMethodMock.mockReturnValueOnce(cloneMethodOut);
const cmd = new (class implements Command<Model, Real> {
check = jest.fn();
run = jest.fn();
toString = jest.fn();
[cloneMethod] = cloneMethodMock;
})();

const wrapper = new CommandWrapper(cmd);
const cloned = wrapper.clone();

expect(cloneMethodMock).toBeCalledTimes(1);
expect(cloned.cmd).toBe(cloneMethodOut);
});
it('Should keep same ref for non cloneable commands on clone', async () => {
const cmd = new (class implements Command<Model, Real> {
check = jest.fn();
run = jest.fn();
toString = jest.fn();
})();

const wrapper = new CommandWrapper(cmd);
const cloned = wrapper.clone();

expect(cloned).not.toBe(wrapper);
expect(cloned.cmd).toBe(wrapper.cmd);
});
});

0 comments on commit 68a4c78

Please sign in to comment.