Browse Source

Update tests

typed-command-runs
Artur Arseniev 5 months ago
parent
commit
1821da5fbe
  1. 8
      packages/core/src/commands/registry.ts
  2. 92
      packages/core/test/specs/commands/index.ts
  3. 86
      packages/core/test/types/commandRegistries.ts

8
packages/core/src/commands/registry.ts

@ -8,12 +8,12 @@ import type {
} from './view/CommandAbstract'; } from './view/CommandAbstract';
import type { FullscreenCommandRegistryRun, FullscreenCommandRegistryStop } from './view/Fullscreen'; import type { FullscreenCommandRegistryRun, FullscreenCommandRegistryStop } from './view/Fullscreen';
type CommandPublicHandler = (...args: any[]) => any; type CommandRegistryHandler = (...args: any[]) => any;
type CommandRegistryEntry<TRegistry, TId extends string> = TId extends keyof TRegistry type CommandRegistryEntry<TRegistry, TId extends string> = TId extends keyof TRegistry
? TRegistry[TId] extends CommandPublicHandler ? TRegistry[TId] extends CommandRegistryHandler
? TRegistry[TId] ? TRegistry[TId]
: CommandPublicHandler : CommandRegistryHandler
: CommandPublicHandler; : CommandRegistryHandler;
export interface CommandRegistryRun extends FullscreenCommandRegistryRun {} export interface CommandRegistryRun extends FullscreenCommandRegistryRun {}

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

@ -1,7 +1,99 @@
import type { Editor } from '../../../src';
import EditorModel from '../../../src/editor/model/Editor'; import EditorModel from '../../../src/editor/model/Editor';
import type Commands from '../../../src/commands'; import type Commands from '../../../src/commands';
import type { Command, CommandFunction, CommandOptions } from '../../../src/commands/view/CommandAbstract'; import type { Command, CommandFunction, CommandOptions } from '../../../src/commands/view/CommandAbstract';
interface MyCommandOptions {
value: number;
}
interface MyCommandResult {
done: boolean;
}
interface MyCommandStopOptions {
reason: string;
}
declare module '../../../src' {
interface CommandRegistryRun {
'my:command': (options: MyCommandOptions) => MyCommandResult;
'my:stateless': () => number;
}
interface CommandRegistryStop {
'my:command': (options: MyCommandStopOptions) => void;
}
}
const assertCommandTypes = () => {
const typedEditor = {} as Editor;
const fullscreenResult: void = typedEditor.runCommand('core:fullscreen');
typedEditor.runCommand('fullscreen', { target: document.body });
// @ts-expect-error Fullscreen target must be an element or selector string
typedEditor.runCommand('core:fullscreen', { target: 1 });
const customResult: MyCommandResult = typedEditor.runCommand('my:command', { value: 1 });
customResult.done;
typedEditor.stopCommand('my:command', { reason: 'done' });
// @ts-expect-error Missing required run options
typedEditor.runCommand('my:command');
// @ts-expect-error Stop options do not match the registry
typedEditor.stopCommand('my:command', { value: 1 });
const statelessResult: number = typedEditor.runCommand('my:stateless');
statelessResult.toFixed();
// @ts-expect-error Stateless commands should not accept options
typedEditor.runCommand('my:stateless', {});
typedEditor.Commands.add('my:command', {
run(_editor, _sender, options) {
options.value.toFixed();
return { done: true };
},
stop(_editor, _sender, options) {
options.reason.toUpperCase();
},
});
typedEditor.Commands.add('my:command', (_editor, _sender, options) => {
options.value.toFixed();
return { done: true };
});
typedEditor.Commands.add('my:stateless', () => 1);
typedEditor.Commands.add('my:command', {
run(_editor, _sender, options) {
// @ts-expect-error The command run options should come from the registry
options.reason.toUpperCase();
return { done: true };
},
});
typedEditor.Commands.config.defaultOptions = {
'my:command': {
run(options) {
return { ...options, value: options.value + 1 };
},
stop(options) {
return { ...options, reason: options.reason.toUpperCase() };
},
},
'core:fullscreen': {
run(options) {
return { ...options, target: options?.target ?? '.app' };
},
},
};
typedEditor.Commands.config.defaultOptions['my:command']?.run?.({ value: 1 });
return fullscreenResult;
};
void assertCommandTypes;
describe('Commands', () => { describe('Commands', () => {
describe('Main', () => { describe('Main', () => {
let em: EditorModel; let em: EditorModel;

86
packages/core/test/types/commandRegistries.ts

@ -1,86 +0,0 @@
import type { Editor } from '../../src';
interface MyCommandOptions {
value: number;
}
interface MyCommandResult {
done: boolean;
}
interface MyCommandStopOptions {
reason: string;
}
declare module '../../src' {
interface CommandRegistryRun {
'my:command': (options: MyCommandOptions) => MyCommandResult;
'my:stateless': () => number;
}
interface CommandRegistryStop {
'my:command': (options: MyCommandStopOptions) => void;
}
}
const editor = {} as Editor;
const fullscreenResult: void = editor.runCommand('core:fullscreen');
editor.runCommand('fullscreen', { target: document.body });
// @ts-expect-error Fullscreen target must be an element or selector string
editor.runCommand('core:fullscreen', { target: 1 });
const customResult: MyCommandResult = editor.runCommand('my:command', { value: 1 });
customResult.done;
editor.stopCommand('my:command', { reason: 'done' });
// @ts-expect-error Missing required run options
editor.runCommand('my:command');
// @ts-expect-error Stop options do not match the registry
editor.stopCommand('my:command', { value: 1 });
const statelessResult: number = editor.runCommand('my:stateless');
statelessResult.toFixed();
// @ts-expect-error Stateless commands should not accept options
editor.runCommand('my:stateless', {});
editor.Commands.add('my:command', {
run(_editor, _sender, options) {
options.value.toFixed();
return { done: true };
},
stop(_editor, _sender, options) {
options.reason.toUpperCase();
},
});
editor.Commands.add('my:command', (_editor, _sender, options) => {
options.value.toFixed();
return { done: true };
});
editor.Commands.add('my:stateless', () => 1);
editor.Commands.add('my:command', {
run(_editor, _sender, options) {
// @ts-expect-error The command run options should come from the registry
options.reason.toUpperCase();
return { done: true };
},
});
editor.Commands.config.defaultOptions = {
'my:command': {
run(options) {
return { ...options, value: options.value + 1 };
},
stop(options) {
return { ...options, reason: options.reason.toUpperCase() };
},
},
'core:fullscreen': {
run(options) {
return { ...options, target: options?.target ?? '.app' };
},
},
};
editor.Commands.config.defaultOptions['my:command']?.run?.({ value: 1 });
Loading…
Cancel
Save