diff --git a/src/check/model/commands/CommandWrapper.ts b/src/check/model/commands/CommandWrapper.ts index 1ea346295c2..9533e95527f 100644 --- a/src/check/model/commands/CommandWrapper.ts +++ b/src/check/model/commands/CommandWrapper.ts @@ -1,3 +1,4 @@ +import { cloneMethod, hasCloneMethod } from '../../symbols'; import { ICommand } from '../command/ICommand'; /** @hidden */ @@ -13,6 +14,8 @@ export class CommandWrapper { + if (hasCloneMethod(this.cmd)) + return new CommandWrapper(this.cmd[cloneMethod]()); return new CommandWrapper(this.cmd); } toString(): string { diff --git a/test/unit/check/model/commands/CommandWrapper.spec.ts b/test/unit/check/model/commands/CommandWrapper.spec.ts index 72f78f3634d..85ae003552a 100644 --- a/test/unit/check/model/commands/CommandWrapper.spec.ts +++ b/test/unit/check/model/commands/CommandWrapper.spec.ts @@ -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 = {}; @@ -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 { + 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 { + 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); + }); });