chore: v0.1.2 — 全面修复、优化与增强

修复:
- core.js: _resume 计时器 BUG (startedAt 被 _startTimer 覆盖导致暂停恢复失效)
- plugins.js: register 在 install 前展开导致 handler 实例丢失
- locales.js: zh-CN/en-US 大量重复 key 覆盖正确翻译值
- core.js: configure 改为原地更新避免 _config 引用断裂

优化:
- core.js: update() 类型变更时同步更新 DOM 类名和进度条颜色
- animations.js: getActiveCount 返回已注册动画数量
- themes.js: 移除 presetThemes 死代码和重复注册循环
- constants.js: THEMES.auto 从字符串改为规范化对象
- index.js: destroy 添加防重复调用保护

增强:
- core.js: 新增 onError 全局错误回调,钩子异常和定时器错误可被外部捕获
- styles.js: _resume 校准时间基准补偿注释
- 139 tests 全部通过
This commit is contained in:
2026-07-24 14:42:47 +08:00
parent eebd669b7f
commit f7d74d152d
17 changed files with 205 additions and 423 deletions
+64 -6
View File
@@ -1,7 +1,7 @@
/**
* MetonaToast 单元测试
* @module tests
* @version 0.1.1
* @version 0.1.2
*/
import MeToast, { Toast, VERSION } from '../src/index.js';
@@ -121,8 +121,8 @@ describe('MetonaToast', () => {
describe('版本信息', () => {
test('应该有正确的版本号', () => {
expect(VERSION).toBe('0.1.1');
expect(MeToast.version).toBe('0.1.1');
expect(VERSION).toBe('0.1.2');
expect(MeToast.version).toBe('0.1.2');
});
});
@@ -558,13 +558,52 @@ describe('MetonaToast', () => {
message: '消息',
duration: 5000,
});
toast._pause();
expect(toast.paused).toBe(true);
toast._resume();
expect(toast.paused).toBe(false);
});
test('暂停恢复应保持 remaining 时间连续性', () => {
const toast = MeToast.success({
message: '消息',
duration: 5000,
});
// 模拟计时开始
toast.config.duration = 5000;
// 直接设置内部状态模拟已运行 2 秒
toast.startedAt = Date.now() - 2000;
toast.remaining = 3000;
toast.paused = false;
const remBefore = toast.remaining;
toast._pause();
expect(toast.paused).toBe(true);
expect(toast.remaining).toBeLessThanOrEqual(remBefore);
const pausedRemaining = toast.remaining;
toast._resume();
expect(toast.paused).toBe(false);
// resume 后 remaining 应保持与暂停时一致(允许微小误差)
expect(toast.remaining).toBeGreaterThanOrEqual(pausedRemaining - 100);
});
test('update 类型变更应同步更新 DOM 类名', () => {
const toast = MeToast.info('test');
expect(toast.type).toBe('info');
toast.update({ type: 'error', message: 'changed' });
expect(toast.type).toBe('error');
// DOM 验证需要实际浏览器环境,此处验证状态变更
});
test('update 类型变更无 DOM 时不抛异常', () => {
const toast = new Toast({ message: 'test', type: 'info' });
expect(() => toast.update({ type: 'success', message: 'ok' })).not.toThrow();
expect(toast.type).toBe('success');
});
test('应该能够获取Toast配置', () => {
const toast = MeToast.success({
@@ -583,7 +622,7 @@ describe('MetonaToast', () => {
MeToast.success('消息');
const status = MeToast.getStatus();
expect(status.version).toBe('0.1.1');
expect(status.version).toBe('0.1.2');
expect(status.toasts).toBeGreaterThanOrEqual(0);
expect(status.theme).toBeDefined();
expect(status.locale).toBeDefined();
@@ -1391,4 +1430,23 @@ describe('新增功能', () => {
});
expect(t.config.render).toBeInstanceOf(Function);
});
test('onError 回调在钩子异常时触发', () => {
const errors = [];
MeToast.configure({ onError: (e) => errors.push(e) });
const badHandler = () => { throw new Error('test-error'); };
Toast.on('afterShow', badHandler);
const toast = MeToast.success('test');
Toast.trigger('afterShow', toast);
expect(errors.length).toBeGreaterThanOrEqual(1);
expect(errors[0].error.message).toBe('test-error');
Toast.off('afterShow', badHandler);
MeToast.configure({ onError: null });
});
test('destroy 重复调用不抛异常', () => {
MeToast.success('test');
MeToast.destroy();
expect(() => MeToast.destroy()).not.toThrow();
});
});