From 79585178ef6c7c0db6eb56e2fbf057787386b6b3 Mon Sep 17 00:00:00 2001 From: tianhao Date: Tue, 16 Jun 2026 13:15:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(test):=20=E6=B5=8B=E8=AF=95=E9=9A=94?= =?UTF-8?q?=E7=A6=BB=20=E2=80=94=20beforeEach=E6=B8=85=E7=90=86DOM+?= =?UTF-8?q?=E9=92=A9=E5=AD=90;=20=E4=BF=AE=E5=A4=8D=E5=90=88=E8=A7=84?= =?UTF-8?q?=E6=80=A7=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/index.test.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) 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();