文件
mynode/packages/lib/services/commands/ToolbarButtonUtils.test.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

138 行
3.5 KiB
TypeScript

import { createStore } from 'redux';
import CommandService from '../CommandService';
import ToolbarButtonUtils from './ToolbarButtonUtils';
import reducer, { defaultState } from '../../reducer';
import stateToWhenClauseContext from './stateToWhenClauseContext';
import KeymapService from '../KeymapService';
import shim from '../../shim';
const createTestCommands = () => {
const simpleCommands = [
{ name: 'newNote', label: 'New Note' },
{ name: 'testCommand2', label: 'Test 2' },
].map(({ name, label }) => ({
declaration: {
name,
label: () => label,
iconName: 'fa-times',
},
runtime: () => ({
execute: () => Promise.resolve(),
}),
}));
const commands = [
...simpleCommands,
{
declaration: {
name: 'invisibleUnlessTrashSelected',
label: () => 'Invisible',
},
runtime: () => ({
execute: () => Promise.resolve(),
visibleCondition: 'inTrash',
}),
},
];
return commands;
};
describe('ToolbarButtonUtils', () => {
let keymapService: KeymapService;
beforeAll(() => {
const store = createStore(reducer);
CommandService.instance().initialize(store, false, stateToWhenClauseContext);
// Boot up the real service
KeymapService.destroyInstance();
keymapService = KeymapService.instance();
keymapService.initialize();
const commands = createTestCommands();
for (const command of commands) {
CommandService.instance().registerDeclaration(command.declaration);
}
CommandService.instance().registerCommands(commands);
});
test('should convert command names to toolbar buttons', () => {
const utils = new ToolbarButtonUtils(CommandService.instance());
const buttons = utils.commandsToToolbarButtons(
['newNote', 'testCommand2'],
stateToWhenClauseContext(defaultState),
keymapService,
);
const expectedTooltip = shim.isMac() ? 'New Note (Cmd+N)' : 'New Note (Ctrl+N)';
expect(buttons).toMatchObject([
{
type: 'button',
name: 'newNote',
tooltip: expectedTooltip,
enabled: true,
},
{
type: 'button',
name: 'testCommand2',
tooltip: 'Test 2',
enabled: true,
},
]);
});
test('should not repeat separators', () => {
const utils = new ToolbarButtonUtils(CommandService.instance());
const buttons = utils.commandsToToolbarButtons(
['testCommand2', '-', '-', '-', 'newNote'],
stateToWhenClauseContext(defaultState),
keymapService,
);
expect(buttons).toMatchObject([
{
type: 'button',
name: 'testCommand2',
},
{ type: 'separator' },
{
type: 'button',
name: 'newNote',
},
]);
});
test('should not show invisible commands', () => {
const utils = new ToolbarButtonUtils(CommandService.instance());
const defaultContext = stateToWhenClauseContext(defaultState);
expect(utils.commandsToToolbarButtons(
['invisibleUnlessTrashSelected'],
defaultContext,
keymapService,
)).toMatchObject([]);
expect(utils.commandsToToolbarButtons(
['invisibleUnlessTrashSelected'],
{ ...defaultContext, inTrash: true },
keymapService,
)).toMatchObject([
{ type: 'button', name: 'invisibleUnlessTrashSelected' },
]);
});
test('should support overriding the enabled state of individual buttons', () => {
const utils = new ToolbarButtonUtils(CommandService.instance());
const buttons = utils.commandsToToolbarButtons(
['newNote', 'testCommand2'],
stateToWhenClauseContext(defaultState),
null,
{ newNote: { enabled: false } },
);
expect(buttons).toMatchObject([
{ name: 'newNote', enabled: false },
{ name: 'testCommand2', enabled: true },
]);
});
});