文件
mynode/packages/app-mobile/components/EditorToolbar/utils/useSaveToolbarButtons.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

76 行
2.6 KiB
TypeScript

import { renderHook, act, waitFor } from '../../../utils/testing/testingLibrary';
import { setupDatabase, switchClient } from '@joplin/lib/testing/test-utils';
import useSaveToolbarButtons from './useSaveToolbarButtons';
import { ReorderableItem } from './useToolbarEditorState';
import { ToolbarButtonInfo } from '@joplin/lib/services/commands/ToolbarButtonUtils';
import Setting from '@joplin/lib/models/Setting';
const mockItem = (name: string): ReorderableItem => ({
commandName: name,
buttonInfo: {
type: 'button',
name,
title: name,
tooltip: name,
iconName: `icon-${name}`,
enabled: true,
visible: true,
onClick: jest.fn(),
} as ToolbarButtonInfo,
});
describe('useSaveToolbarButtons', () => {
beforeEach(async () => {
await setupDatabase(0);
await switchClient(0);
Setting.setValue('editor.toolbarButtons', []);
});
it('should not save on initial mount', async () => {
const isReinitializing = { current: false };
renderHook(() => useSaveToolbarButtons([mockItem('textBold')], isReinitializing));
// Wait a tick to ensure the effect has run
await act(async () => {});
expect(Setting.value('editor.toolbarButtons')).toEqual([]);
});
it('should save when enabledItems changes after initial mount', async () => {
const isReinitializing = { current: false };
const { rerender } = renderHook(
({ items }: { items: ReorderableItem[] }) => useSaveToolbarButtons(items, isReinitializing),
{ initialProps: { items: [mockItem('textBold')] } },
);
rerender({ items: [mockItem('textBold'), mockItem('textItalic')] });
await waitFor(() => {
expect(Setting.value('editor.toolbarButtons')).toEqual(['textBold', 'textItalic']);
});
});
it('should not save when isReinitializing is set, and should reset the flag', async () => {
const isReinitializing = { current: false };
const { rerender } = renderHook(
({ items }: { items: ReorderableItem[] }) => useSaveToolbarButtons(items, isReinitializing),
{ initialProps: { items: [mockItem('textBold')] } },
);
// First do a real save so the initial-mount skip is consumed
rerender({ items: [mockItem('textBold'), mockItem('textItalic')] });
await waitFor(() => {
expect(Setting.value('editor.toolbarButtons')).toEqual(['textBold', 'textItalic']);
});
// Now simulate reinitialize
isReinitializing.current = true;
rerender({ items: [mockItem('textCode')] });
// Give the effect time to run
await act(async () => {});
// Setting should be unchanged
expect(Setting.value('editor.toolbarButtons')).toEqual(['textBold', 'textItalic']);
// Flag should have been reset
expect(isReinitializing.current).toBe(false);
});
});