Browse Source

feat: add run and stop command tests

carlos/505-improve-grapesjs-absolute-mode
Carlos 11 months ago
parent
commit
9d018ab615
  1. 3
      packages/core/src/commands/config/config.ts
  2. 1
      packages/core/src/commands/index.ts
  3. 4
      packages/core/src/commands/view/ComponentDrag.ts
  4. 47
      packages/core/test/specs/commands/index.ts

3
packages/core/src/commands/config/config.ts

@ -61,9 +61,6 @@ const config: () => CommandsConfig = () => ({
'core:component-drag': {
run: (options: CommandOptions) => ({
...options,
customElements: {
guideIndicator: false,
},
// addStyle: ({ componentView, matchedEl }) => {
// // TODO: test if you can conver to percentage
// // componentView.setStyle({ opacity: 0.5 });

1
packages/core/src/commands/index.ts

@ -388,7 +388,6 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
const id = command.id as string;
const editor = em.Editor;
// TODO: add test
if (!this.isActive(id) || options.force || !config.strict) {
const defaultOptionsRunFn = config.defaultOptions?.[id]?.run;
isFunction(defaultOptionsRunFn) && (options = defaultOptionsRunFn(options));

4
packages/core/src/commands/view/ComponentDrag.ts

@ -502,9 +502,7 @@ export default {
guideInfoStyle[isY ? 'top' : 'left'] = pos2;
guideInfoStyle[isY ? 'left' : 'top'] = `${posSecond}px`;
guideInfoStyle[isY ? 'width' : 'height'] = `${size}px`;
// TODO: this is the way to display the percentage?
const showPercentages = this.opts?.addStyle?.()?.showPercentages;
elGuideInfoCnt.innerHTML = showPercentages ? `${Math.round(sizePercent)}%` : `${Math.round(sizeRaw)}px`;
elGuideInfoCnt.innerHTML = `${Math.round(sizeRaw)}px`;
guideNearElement = {
guide: item,

47
packages/core/test/specs/commands/index.ts

@ -94,5 +94,52 @@ describe('Commands', () => {
expect(obj.isActive(commName)).toBe(false);
expect(Object.keys(obj.getActive()).length).toBe(0);
});
test('Run command with and without default options', () => {
const defaultOptions = { test: 'default' };
const options = { test: 'custom' };
const command = { run: jest.fn(), stop: jest.fn() };
obj.add(commName, command);
em.config.commands = {
defaultOptions: {
[commName]: {
run: (opts) => ({ ...opts, ...defaultOptions }),
},
},
};
// Test run command with default options
obj.run(commName, options);
expect(command.run).toHaveBeenCalledWith(em, { ...options, ...defaultOptions });
// Test run command without default options
em.config.commands.defaultOptions![commName].run = undefined;
obj.run(commName, options);
expect(command.run).toHaveBeenCalledWith(em, options);
});
test('Stop command with and without default options', () => {
const defaultOptions = { test: 'default' };
const options = { test: 'custom' };
const command = { run: jest.fn(), stop: jest.fn() };
obj.add(commName, command);
em.config.commands = {
defaultOptions: {
[commName]: {
stop: (opts) => ({ ...opts, ...defaultOptions }),
},
},
};
// Test stop command with default options
obj.run(commName, options);
obj.stop(commName, options);
expect(command.stop).toHaveBeenCalledWith(em, { ...options, ...defaultOptions });
// Test stop command without default options
em.config.commands.defaultOptions![commName].stop = undefined;
obj.stop(commName, options);
expect(command.stop).toHaveBeenCalledWith(em, options);
});
});
});

Loading…
Cancel
Save