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