fix: 移除 setInterval 轮询,改用纯事件驱动 selectionChange — 修复 CI 卡死问题
This commit is contained in:
+10
-17
@@ -2179,13 +2179,10 @@ describe('MarkdownEditor - v0.2.3 浮动工具栏', () => {
|
||||
const c = document.createElement('div');
|
||||
document.body.appendChild(c);
|
||||
const ed = new MarkdownEditor(c, { value: 'hello world' });
|
||||
// 浮动工具栏应被注入 CSS
|
||||
const style = document.getElementById('me-float-style');
|
||||
expect(style).not.toBeNull();
|
||||
// _floatingToolbar 在首次 selection 时才创建 DOM
|
||||
expect(ed._floatingToolbar).toBeNull();
|
||||
// _selectionTimer 已启动
|
||||
expect(ed._selectionTimer).not.toBeNull();
|
||||
expect(ed._floatingEnabled).toBe(true);
|
||||
ed.destroy();
|
||||
});
|
||||
|
||||
@@ -2194,8 +2191,7 @@ describe('MarkdownEditor - v0.2.3 浮动工具栏', () => {
|
||||
const c = document.createElement('div');
|
||||
document.body.appendChild(c);
|
||||
const ed = new MarkdownEditor(c, { value: 'hello', floatingToolbar: false });
|
||||
// 浮动工具栏应不启动(_selectionTimer 为 null)
|
||||
expect(ed._selectionTimer).toBeNull();
|
||||
expect(ed._floatingEnabled).toBe(false);
|
||||
expect(ed._floatingToolbar).toBeNull();
|
||||
ed.destroy();
|
||||
});
|
||||
@@ -2224,26 +2220,23 @@ describe('MarkdownEditor - v0.2.3 浮动工具栏', () => {
|
||||
expect(ed._floatingToolbar).not.toBeNull();
|
||||
ed.destroy();
|
||||
expect(ed._floatingToolbar).toBeNull();
|
||||
expect(ed._selectionTimer).toBeNull();
|
||||
});
|
||||
|
||||
test('selectionChange 事件触发', (done) => {
|
||||
test('selectionChange 事件触发', () => {
|
||||
document.body.innerHTML = '';
|
||||
const c = document.createElement('div');
|
||||
document.body.appendChild(c);
|
||||
const ed = new MarkdownEditor(c, { value: 'hello world' });
|
||||
const handler = jest.fn();
|
||||
ed.on('selectionChange', handler);
|
||||
// Fire keyup after setting selection — selectionChange is emitted synchronously in the handler
|
||||
ed.textarea.setSelectionRange(0, 5);
|
||||
// selectionChange 通过 interval 检测,等 300ms
|
||||
setTimeout(() => {
|
||||
expect(handler).toHaveBeenCalled();
|
||||
expect(handler.mock.calls[0][0]).toHaveProperty('start', 0);
|
||||
expect(handler.mock.calls[0][0]).toHaveProperty('end', 5);
|
||||
expect(handler.mock.calls[0][0]).toHaveProperty('text', 'hello');
|
||||
ed.destroy();
|
||||
done();
|
||||
}, 800);
|
||||
ed.textarea.dispatchEvent(new KeyboardEvent('keyup', { key: 'ArrowRight', bubbles: true }));
|
||||
expect(handler).toHaveBeenCalled();
|
||||
expect(handler.mock.calls[0][0]).toHaveProperty('start', 0);
|
||||
expect(handler.mock.calls[0][0]).toHaveProperty('end', 5);
|
||||
expect(handler.mock.calls[0][0]).toHaveProperty('text', 'hello');
|
||||
ed.destroy();
|
||||
});
|
||||
|
||||
test('cursorMove 事件在按键后触发', () => {
|
||||
|
||||
Reference in New Issue
Block a user