Browse Source

refactor: improve command option handling and test clarity

carlos/505-improve-grapesjs-absolute-mode
Carlos 2 years ago
parent
commit
acca8e0edf
  1. 62
      packages/core/test/specs/commands/index.ts

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

@ -1,6 +1,6 @@
import Commands from '../../../src/commands';
import EditorModel from '../../../src/editor/model/Editor'; import EditorModel from '../../../src/editor/model/Editor';
import { Command, CommandFunction } from '../../../src/commands/view/CommandAbstract'; import type Commands from '../../../src/commands';
import type { Command, CommandFunction, CommandOptions } from '../../../src/commands/view/CommandAbstract';
describe('Commands', () => { describe('Commands', () => {
describe('Main', () => { describe('Main', () => {
@ -95,54 +95,34 @@ describe('Commands', () => {
expect(Object.keys(obj.getActive()).length).toBe(0); expect(Object.keys(obj.getActive()).length).toBe(0);
}); });
test('Run command and check if defaultOptions are passed or not', () => { test('Run command and check if none, custom, and default options are passed', () => {
const defaultOptions = { key: 'defaultValue' }; const customOptions = { customValue: 'customValue' };
const customOptions = { key: 'customValue' }; const defaultOptions = { defaultValue: 'defaultValue' };
const mergedOptions = { ...defaultOptions, ...customOptions };
const comm = { // Create a function that returns the options
run: jest.fn(() => commResultRun), // Mock the run method const runFn = (_editor: any, _sender: any, options: any) => options;
};
// Add the command // Add the command
obj.add(commName, comm); obj.add(commName, { run: runFn });
expect(obj.isActive(commName)).toBe(false);
// Run the command without defaultOptions // Run the command without custom options
let result = obj.run(commName, customOptions); let result = obj.run(commName);
expect(result).toBe(commResultRun); expect(result).toEqual({});
expect(comm.run).toHaveBeenCalledWith(
expect.any(Object), // The `em` object // Run the command with custom options
expect.objectContaining(customOptions), result = obj.run(commName, customOptions);
); expect(result).toEqual(customOptions);
expect(obj.isActive(commName)).toBe(false);
// Configure defaultOptions // Set default options for the command
em.config.commands = { obj.config.defaultOptions = {
defaultOptions: { [commName]: {
[commName]: { run: (options: CommandOptions) => ({ ...options, ...defaultOptions }),
run: (opts) => ({ ...defaultOptions, ...opts }), // Merge defaultOptions with provided options
},
}, },
}; };
// Run the command without custom options // Run the command with default options
result = obj.run(commName);
expect(result).toBe(commResultRun);
expect(comm.run).toHaveBeenCalledWith(
expect.any(Object), // The `em` object
expect.objectContaining(defaultOptions),
);
expect(obj.isActive(commName)).toBe(false);
// Run the command with custom options
result = obj.run(commName, customOptions); result = obj.run(commName, customOptions);
expect(result).toBe(commResultRun); expect(result).toEqual({ ...customOptions, ...defaultOptions });
expect(comm.run).toHaveBeenCalledWith(
expect.any(Object), // The `em` object
expect.objectContaining(mergedOptions),
);
expect(obj.isActive(commName)).toBe(false);
}); });
}); });
}); });

Loading…
Cancel
Save