diff --git a/tests/index.test.js b/tests/index.test.js index 7632874..16ff67d 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -109,11 +109,14 @@ global.Audio = mockWindow.Audio; // 测试套件 describe('MetonaToast', () => { beforeEach(() => { - // 清除所有模拟调用 jest.clearAllMocks(); - - // 重置Toast状态 MeToast._toasts.clear(); + // 清理DOM残留 + if (typeof document !== 'undefined') { + document.querySelectorAll('.met-container').forEach(c => c.remove()); + } + // 清理钩子残留 + Toast._hooks.clear(); }); describe('版本信息', () => { @@ -1730,17 +1733,16 @@ describe('插件管理器', () => { describe('高级功能', () => { test('confirm 返回 Promise', async () => { - // confirm 创建 toast 并等待按钮点击,这里只验证 Promise 类型 const result = MeToast.confirm('确认?'); expect(result).toBeInstanceOf(Promise); - // 立即清理:点击取消按钮(如果有) + // jsdom中手动触发取消按钮 setTimeout(() => { const btn = document.querySelector('.met-cancel-btn'); if (btn) btn.click(); - }, 10); + }, 50); const ok = await result; expect(typeof ok).toBe('boolean'); - }); + }, 15000); test('confirm 非字符串参数返回 false', async () => { const result = await MeToast.confirm(123); @@ -1753,10 +1755,10 @@ describe('高级功能', () => { setTimeout(() => { const btn = document.querySelector('.met-cancel-btn'); if (btn) btn.click(); - }, 10); + }, 50); const val = await result; expect(val === null || typeof val === 'string').toBe(true); - }); + }, 15000); test('prompt 非字符串参数返回 null', async () => { const result = await MeToast.prompt(123); @@ -1790,9 +1792,10 @@ describe('高级功能', () => { expect(c.pause).toBeInstanceOf(Function); }); - test('queue 返回 Promise', () => { + test('queue 返回 thenable', () => { const result = MeToast.queue(['a', 'b', 'c'], { delay: 10, duration: 10 }); - expect(result).toBeInstanceOf(Promise); + expect(result.then).toBeInstanceOf(Function); + expect(result.cancel).toBeInstanceOf(Function); }); test('queue 非数组返回 resolved Promise', async () => { @@ -1854,15 +1857,13 @@ describe('Toast钩子系统', () => { test('完整生命周期钩子触发顺序', () => { const calls = []; - const record = (name) => jest.fn(() => calls.push(name)); - - const beforeShow = record('beforeShow'); - const afterShow = record('afterShow'); + const beforeShow = jest.fn(() => calls.push('beforeShow')); + const afterShow = jest.fn(() => calls.push('afterShow')); Toast.on('beforeShow', beforeShow); Toast.on('afterShow', afterShow); - const toast = MeToast.success('test'); + const toast = new Toast({ message: 'test' }); Toast.trigger('beforeShow', toast); Toast.trigger('afterShow', toast); @@ -1874,6 +1875,10 @@ describe('Toast钩子系统', () => { }); describe('边界情况', () => { + beforeEach(() => { + MeToast._toasts.clear(); + document.querySelectorAll('.met-container').forEach(c => c.remove()); + }); test('空消息 toast 不抛异常', () => { expect(() => MeToast.success('')).not.toThrow(); expect(() => MeToast.show()).not.toThrow();