From c9dbfc570a4db5746b3c488edf52eabd438c4669 Mon Sep 17 00:00:00 2001 From: nemo-shen Date: Mon, 6 Dec 2021 00:39:52 +0800 Subject: [PATCH] test(Toast): add demo test --- example/app.json | 3 +- example/pages/toast/index.js | 51 +-- example/pages/toast/index.wxml | 21 +- example/pages/toast/index.wxss | 0 packages/toast/demo/index.json | 8 + packages/toast/demo/index.ts | 59 ++++ packages/toast/demo/index.wxml | 20 ++ .../test/__snapshots__/demo.spec.ts.snap | 305 ++++++++++++++++++ packages/toast/test/demo.spec.ts | 11 + 9 files changed, 407 insertions(+), 71 deletions(-) delete mode 100644 example/pages/toast/index.wxss create mode 100644 packages/toast/demo/index.json create mode 100644 packages/toast/demo/index.ts create mode 100644 packages/toast/demo/index.wxml create mode 100644 packages/toast/test/__snapshots__/demo.spec.ts.snap create mode 100644 packages/toast/test/demo.spec.ts diff --git a/example/app.json b/example/app.json index 98e3d1f3..b9fcb959 100644 --- a/example/app.json +++ b/example/app.json @@ -129,9 +129,10 @@ "van-tabbar": "./dist/tabbar/index", "van-tabbar-demo": "./dist/tabbar/demo/index", "van-tabbar-item": "./dist/tabbar-item/index", - "van-tag": "./dist/tag/index",packages/tag/demo/index.less, + "van-tag": "./dist/tag/index", "van-tag-demo": "./dist/tag/demo/index", "van-toast": "./dist/toast/index", + "van-toast-demo": "./dist/toast/demo/index", "van-transition": "./dist/transition/index", "van-tree-select": "./dist/tree-select/index", "van-datetime-picker": "./dist/datetime-picker/index", diff --git a/example/pages/toast/index.js b/example/pages/toast/index.js index 7b30a623..cc11dfda 100644 --- a/example/pages/toast/index.js +++ b/example/pages/toast/index.js @@ -1,52 +1,3 @@ import Page from '../../common/page'; -import Toast from '../../dist/toast/toast'; -Page({ - showToast() { - Toast('提示内容'); - }, - - showLongToast() { - Toast('这是一条长文字提示,超过一定字数就会换行'); - }, - - showLoadingToast() { - Toast.loading({ message: '加载中...', forbidClick: true }); - }, - - showCustomLoadingToast() { - Toast.loading({ - message: '加载中...', - forbidClick: true, - loadingType: 'spinner', - }); - }, - - showSuccessToast() { - Toast.success('成功文案'); - }, - - showFailToast() { - Toast.fail('失败提示'); - }, - - showCustomizedToast() { - const text = (second) => `倒计时 ${second} 秒`; - const toast = Toast.loading({ - duration: 0, - forbidClick: true, - message: text(3), - }); - - let second = 3; - const timer = setInterval(() => { - second--; - if (second) { - toast.setData({ message: text(second) }); - } else { - clearInterval(timer); - Toast.clear(); - } - }, 1000); - }, -}); +Page(); diff --git a/example/pages/toast/index.wxml b/example/pages/toast/index.wxml index 67b83ce0..99081a39 100644 --- a/example/pages/toast/index.wxml +++ b/example/pages/toast/index.wxml @@ -1,20 +1 @@ - - 文字提示 - 长文字提示 - - - - 加载提示 - 自定义加载图标 - - - - 成功提示 - 失败提示 - - - - 动态更新提示 - - - + diff --git a/example/pages/toast/index.wxss b/example/pages/toast/index.wxss deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/toast/demo/index.json b/packages/toast/demo/index.json new file mode 100644 index 00000000..0b85298b --- /dev/null +++ b/packages/toast/demo/index.json @@ -0,0 +1,8 @@ +{ + "component": true, + "usingComponents": { + "van-toast": "../../toast/index", + "van-button": "../../button/index", + "demo-block": "../../../example/components/demo-block/index" + } +} diff --git a/packages/toast/demo/index.ts b/packages/toast/demo/index.ts new file mode 100644 index 00000000..08fc6520 --- /dev/null +++ b/packages/toast/demo/index.ts @@ -0,0 +1,59 @@ +import { VantComponent } from '../../common/component'; +import Toast from '../../toast/toast'; + +VantComponent({ + methods: { + showToast() { + Toast({ context: this, message: '提示内容' }); + }, + + showLongToast() { + Toast({ + context: this, + message: '这是一条长文字提示,超过一定字数就会换行', + }); + }, + + showLoadingToast() { + Toast.loading({ context: this, message: '加载中...', forbidClick: true }); + }, + + showCustomLoadingToast() { + Toast.loading({ + context: this, + message: '加载中...', + forbidClick: true, + loadingType: 'spinner', + }); + }, + + showSuccessToast() { + Toast.success({ context: this, message: '成功文案' }); + }, + + showFailToast() { + Toast.fail({ context: this, message: '失败提示' }); + }, + + showCustomizedToast() { + const text = (second) => `倒计时 ${second} 秒`; + const toast = Toast.loading({ + context: this, + duration: 0, + forbidClick: true, + message: text(3), + }); + + let second = 3; + const timer = setInterval(() => { + second--; + if (second) { + toast?.setData({ message: text(second) }); + } else { + clearInterval(timer); + Toast.clear(); + } + }, 1000); + }, + }, +}); diff --git a/packages/toast/demo/index.wxml b/packages/toast/demo/index.wxml new file mode 100644 index 00000000..67b83ce0 --- /dev/null +++ b/packages/toast/demo/index.wxml @@ -0,0 +1,20 @@ + + 文字提示 + 长文字提示 + + + + 加载提示 + 自定义加载图标 + + + + 成功提示 + 失败提示 + + + + 动态更新提示 + + + diff --git a/packages/toast/test/__snapshots__/demo.spec.ts.snap b/packages/toast/test/__snapshots__/demo.spec.ts.snap new file mode 100644 index 00000000..e2c2a69d --- /dev/null +++ b/packages/toast/test/__snapshots__/demo.spec.ts.snap @@ -0,0 +1,305 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render demo and match snapshot 1`] = ` +
+ + + + 文字提示 + + + + + 文字提示 + + + + + + + 长文字提示 + + + + + + + + + 加载提示 + + + + + 加载提示 + + + + + + + 自定义加载图标 + + + + + + + + + 成功/失败提示 + + + + + 成功提示 + + + + + + + 失败提示 + + + + + + + + + 动态更新提示 + + + + + 动态更新提示 + + + + + + + + +
+`; diff --git a/packages/toast/test/demo.spec.ts b/packages/toast/test/demo.spec.ts new file mode 100644 index 00000000..4c3798cb --- /dev/null +++ b/packages/toast/test/demo.spec.ts @@ -0,0 +1,11 @@ +import path from 'path'; +import simulate from 'miniprogram-simulate'; + +test('should render demo and match snapshot', () => { + const id = simulate.load(path.resolve(__dirname, '../demo/index'), { + rootPath: path.resolve(__dirname, '../../'), + }); + const comp = simulate.render(id); + comp.attach(document.createElement('parent-wrapper')); + expect(comp.toJSON()).toMatchSnapshot(); +});