release: v0.2.1 — bug修复、代码去重、功能增强、覆盖率88.57%
This commit is contained in:
+76
-4
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* MetonaToast 单元测试
|
||||
* @module tests
|
||||
* @version 0.2.0
|
||||
* @version 0.2.1
|
||||
*/
|
||||
|
||||
import MeToast, { Toast, VERSION } from '../src/index';
|
||||
@@ -105,8 +105,8 @@ const mockWindow = {
|
||||
|
||||
describe('版本信息', () => {
|
||||
test('应该有正确的版本号', () => {
|
||||
expect(VERSION).toBe('0.2.0');
|
||||
expect(MeToast.version).toBe('0.2.0');
|
||||
expect(VERSION).toBe('0.2.1');
|
||||
expect(MeToast.version).toBe('0.2.1');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -526,7 +526,7 @@ const mockWindow = {
|
||||
test('应该能够获取状态信息', () => {
|
||||
MeToast.success('消息');
|
||||
const status = MeToast.getStatus();
|
||||
expect(status.version).toBe('0.2.0');
|
||||
expect(status.version).toBe('0.2.1');
|
||||
expect(status.toasts).toBeGreaterThanOrEqual(0);
|
||||
expect(status.theme).toBeDefined();
|
||||
expect(status.locale).toBeDefined();
|
||||
@@ -1212,3 +1212,75 @@ describe('新增功能', () => {
|
||||
expect(() => MeToast.destroy()).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
// ========== v0.2.1 新增功能测试 ==========
|
||||
|
||||
describe('v0.2.1 新增功能', () => {
|
||||
beforeEach(() => {
|
||||
MeToast._toasts.clear();
|
||||
const { _containerCache } = require('../src/toast.js');
|
||||
_containerCache.clear();
|
||||
});
|
||||
|
||||
test('beforeShow 钩子返回 false 阻止显示', () => {
|
||||
const handler = jest.fn(() => false);
|
||||
Toast.on('beforeShow', handler);
|
||||
const toast = MeToast.success('should-not-show');
|
||||
expect(handler).toHaveBeenCalled();
|
||||
// 钩子返回 false 后不应该创建 DOM 元素
|
||||
expect(toast.el).toBeNull();
|
||||
Toast.off('beforeShow', handler);
|
||||
});
|
||||
|
||||
test('onBeforeShow 配置返回 false 阻止显示', () => {
|
||||
const toast = MeToast.success({
|
||||
message: 'test',
|
||||
onBeforeShow: () => false,
|
||||
} as Record<string, unknown>);
|
||||
expect(toast.el).toBeNull();
|
||||
});
|
||||
|
||||
test('updatePosition 移动 Toast 到新位置', () => {
|
||||
const toast = MeToast.info('movable');
|
||||
expect(toast.config.position).toBe('top-right');
|
||||
toast.updatePosition('bottom-left');
|
||||
expect(toast.config.position).toBe('bottom-left');
|
||||
});
|
||||
|
||||
test('remove() 立即移除不触发离场动画', () => {
|
||||
const toast = MeToast.info('test');
|
||||
expect(MeToast.count()).toBe(1);
|
||||
toast.remove();
|
||||
expect(toast.el).toBeNull();
|
||||
expect(MeToast.count()).toBe(0);
|
||||
});
|
||||
|
||||
test('removeToast() 公开 API', () => {
|
||||
const toast = MeToast.warning('test');
|
||||
expect(MeToast.count()).toBe(1);
|
||||
MeToast.removeToast(toast.id);
|
||||
expect(MeToast.count()).toBe(0);
|
||||
});
|
||||
|
||||
test('removeToast 不存在的 id 不抛异常', () => {
|
||||
expect(() => MeToast.removeToast('nonexistent')).not.toThrow();
|
||||
});
|
||||
|
||||
test('RTL 语言自动翻转容器位置', () => {
|
||||
// 直接测试 _getContainer 的 RTL 翻转逻辑
|
||||
const { getLocaleDirection, getCurrentLocale, setCurrentLocale, addTranslations } = require('../src/i18n.js');
|
||||
const originalLocale = getCurrentLocale();
|
||||
// 先为 'ar' 注册空翻译数据,然后切换
|
||||
addTranslations('ar', { close: 'إغلاق' });
|
||||
setCurrentLocale('ar');
|
||||
try {
|
||||
const toast = new Toast({ message: 'test', position: 'top-left' });
|
||||
const container = toast._getContainer();
|
||||
// RTL 下 top-left 应翻转为 top-right
|
||||
expect(container.className).toContain('top-right');
|
||||
toast.close();
|
||||
} finally {
|
||||
setCurrentLocale(originalLocale);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user