From 02ce0ff948d2d363dade652664e27cfc5b0f4a62 Mon Sep 17 00:00:00 2001 From: carolin913n Date: Wed, 16 Mar 2022 14:00:47 +0800 Subject: [PATCH 1/2] test: unit test support --- package.json | 4 +- .../__snapshots__/button.test.tsx.snap | 430 ++++++++++++++++++ src/button/__tests__/button.test.tsx | 14 + test/config/jest.base.conf.js | 6 +- test/config/jest.unit.conf.js | 2 +- test/ssr/__snapshots__/ssr.test.js.snap | 156 +++++++ test/ssr/ssr.test.js | 6 +- test/utils/test-examples.ts | 17 +- 8 files changed, 620 insertions(+), 15 deletions(-) create mode 100644 src/button/__tests__/__snapshots__/button.test.tsx.snap create mode 100644 src/button/__tests__/button.test.tsx diff --git a/package.json b/package.json index 65b22079..289a7dfe 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "lint": "eslint --ext .ts,.tsx ./ --max-warnings 0", "lint:fix": "eslint --ext .ts,.tsx ./ --max-warnings 0 --fix", "test": "echo \"no test specified, work in progress\"", - "test:update": "npm run test:unit-update && npm run test:node-update", + "test:update": "npm run test:unit-update", "test:unit": "bash -c 'jest --coverage --config test/config/jest.unit.conf.js ${1}' -- ", "test:unit-update": "jest --config test/config/jest.unit.conf.js --updateSnapshot --coverage", "test:node": "jest --config test/config/jest.ssr.conf.js", @@ -160,4 +160,4 @@ "path": "./node_modules/cz-conventional-changelog" } } -} +} \ No newline at end of file diff --git a/src/button/__tests__/__snapshots__/button.test.tsx.snap b/src/button/__tests__/__snapshots__/button.test.tsx.snap new file mode 100644 index 00000000..731c7f66 --- /dev/null +++ b/src/button/__tests__/__snapshots__/button.test.tsx.snap @@ -0,0 +1,430 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`base.jsx 1`] = ` + + + + + + + + + +`; + +exports[`icon.jsx 1`] = ` + + + +`; + +exports[`index.jsx 1`] = ` + +
+
+

+ Button 按钮 +

+

+ 按钮用于开启一个闭环的操作任务,如“删除”对象、“购买”商品等。 +

+
+
+

+ 01 基础按钮 +

+

+ 基础类型分为主按钮、次按钮、文字按钮。 +

+
+ + + + + + + +
+
+ +
+ +
+
+

+ 02 图标按钮 +

+

+ 图标按钮分为图标加文字形式、纯图标形式 +

+
+ +
+
+
+

+ 03 按钮状态 +

+

+ 按钮禁用态 +

+
+ + + + + + + +
+
+ +
+ +
+
+

+ 04 尺寸 +

+

+ 提供大、中、小 3 种不同大小按钮 +

+
+ + +
+ +
+
+
+

+ 05 形状 +

+

+

+ + +
+
+
+
+`; + +exports[`shape.jsx 1`] = ` + + + + +`; + +exports[`size.jsx 1`] = ` + + + +
+ +
+`; + +exports[`test.jsx 1`] = ` + +
+ + +
+
+ + +
+
+`; diff --git a/src/button/__tests__/button.test.tsx b/src/button/__tests__/button.test.tsx new file mode 100644 index 00000000..fc37fe8d --- /dev/null +++ b/src/button/__tests__/button.test.tsx @@ -0,0 +1,14 @@ +import React from 'react'; +import { testExamples, render } from '@test/utils'; +import Button from '../Button'; + +// 测试组件代码 Example 快照 +testExamples(__dirname); + +describe('Button 组件测试', () => { + const ButtonText = '按钮组件'; + test('content', async () => { + const { queryByText } = render("`; + +exports[`ssr snapshot test renders ./src/button/_example/icon.jsx correctly 1`] = `""`; + +exports[`ssr snapshot test renders ./src/button/_example/index.jsx correctly 1`] = `"

Button 按钮

按钮用于开启一个闭环的操作任务,如“删除”对象、“购买”商品等。

01 基础按钮

基础类型分为主按钮、次按钮、文字按钮。

02 图标按钮

图标按钮分为图标加文字形式、纯图标形式

03 按钮状态

按钮禁用态

04 尺寸

提供大、中、小 3 种不同大小按钮


05 形状

"`; + +exports[`ssr snapshot test renders ./src/button/_example/shape.jsx correctly 1`] = `""`; + +exports[`ssr snapshot test renders ./src/button/_example/size.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/button/_example/test.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/cell/_example/base.jsx correctly 1`] = `"

Grid 宫格

一行内容/功能的垂直排列方式。一行项目左侧为主要内容展示区域,右侧可增加更多操作内容。

01 类型

单行单元格

单行标题
单行标题 *
单行标题
辅助信息
单行标题
单行标题
辅助信息
单行标题
8
单行标题
右侧switch
单行标题

多行单元格

多行标题
一段很长很长的内容文字
多行带图标
一段很长很长的内容文字
多行带头像
一段很长很长的内容文字
多行带图片
一段很长很长的内容文字
多行标题
一段很长很长的内容文字,长文本自动换行,该选项的描述是一段很长的内容
多行高度不定,长文本自动换行,该选项的描述是一段很长的内容
一段很长很长的内容文字,长文本自动换行,该选项的描述是一段很长的内容一段很长很长的内容文字,长文本自动换行,该选项的描述是一段很长的内容
"`; + +exports[`ssr snapshot test renders ./src/cell/_example/multiple.jsx correctly 1`] = `"
多行标题
一段很长很长的内容文字
多行带图标
一段很长很长的内容文字
多行带头像
一段很长很长的内容文字
多行带图片
一段很长很长的内容文字
多行标题
一段很长很长的内容文字,长文本自动换行,该选项的描述是一段很长的内容
多行高度不定,长文本自动换行,该选项的描述是一段很长的内容
一段很长很长的内容文字,长文本自动换行,该选项的描述是一段很长的内容一段很长很长的内容文字,长文本自动换行,该选项的描述是一段很长的内容
"`; + +exports[`ssr snapshot test renders ./src/cell/_example/single.jsx correctly 1`] = `"
单行标题
单行标题 *
单行标题
辅助信息
单行标题
单行标题
辅助信息
单行标题
8
单行标题
右侧switch
单行标题
"`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/base.jsx correctly 1`] = `""`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/disable.jsx correctly 1`] = `""`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/group.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/icon.jsx correctly 1`] = `""`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/indeterminate.jsx correctly 1`] = `""`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/index.jsx correctly 1`] = `"

Checkbox 多选框

用于预设的一组选项中执行多项选择,并呈现选择结果。

基础多选框

右侧多选框

多选框禁用态

半选状态

带全选的多选框


限制选择数量

自定义图标

"`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/max.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/checkbox/_example/right.jsx correctly 1`] = `""`; + +exports[`ssr snapshot test renders ./src/count-down/_example/base.jsx correctly 1`] = `"

CountDown倒计时

用于实时展示倒计时数值。

类型

时分秒
带毫秒
带方形底
带圆形底
带单位
无底色带单位

规格

纯数字
带圆形底
带方形底
带单位
无底色带单位
"`; + +exports[`ssr snapshot test renders ./src/count-down/_example/specs.jsx correctly 1`] = `"
纯数字
带圆形底
带方形底
带单位
无底色带单位
"`; + +exports[`ssr snapshot test renders ./src/count-down/_example/type.jsx correctly 1`] = `"
时分秒
带毫秒
带方形底
带圆形底
带单位
无底色带单位
"`; + +exports[`ssr snapshot test renders ./src/divider/_example/base.jsx correctly 1`] = `"

Divider 分割线

用于分割、组织、细化有一定逻辑的组织元素内容和页面结构。

01 类型

直线拉通

虚线拉通

左右间距

右侧拉通

定义左侧距离

垂直分割

文字信息
文字信息
文字信息

文字+直线

文字信息

文字+虚线

文字信息

纯文字

没有更多了~
"`; + +exports[`ssr snapshot test renders ./src/divider/_example/content.jsx correctly 1`] = `"
文字信息
"`; + +exports[`ssr snapshot test renders ./src/divider/_example/normal.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/divider/_example/normal-dashed.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/divider/_example/pure-content.jsx correctly 1`] = `"
没有更多了~
"`; + +exports[`ssr snapshot test renders ./src/divider/_example/vertical.jsx correctly 1`] = `"
文字信息
文字信息
文字信息
"`; + +exports[`ssr snapshot test renders ./src/grid/_example/badge.tsx correctly 1`] = `"
标题文字
8
标题文字
new
标题文字
···
标题文字
"`; + +exports[`ssr snapshot test renders ./src/grid/_example/base.jsx correctly 1`] = `"

Grid 宫格

横向分割的点击单元,用作一组次级功能的入口

01 类型

一行三个(border)

标题文字
标题文字
标题文字

一行四个

标题文字
标题文字
标题文字
标题文字

一行五个

标题文字
标题文字
标题文字
标题文字
标题文字
标题文字
标题文字
标题文字
标题文字
标题文字

一行两个带说明宫格

标题文字
说明文字
标题文字
说明文字

一行三个带说明宫格

标题文字
说明文字
标题文字
说明文字
标题文字
说明文字

带徽标宫格

标题文字
8
标题文字
new
标题文字
···
标题文字
"`; + +exports[`ssr snapshot test renders ./src/grid/_example/border.jsx correctly 1`] = `"
标题文字
标题文字
标题文字
"`; + +exports[`ssr snapshot test renders ./src/grid/_example/layout.jsx correctly 1`] = `"
标题文字
说明文字
标题文字
说明文字
"`; + +exports[`ssr snapshot test renders ./src/grid/_example/normal.jsx correctly 1`] = `"
标题文字
标题文字
标题文字
标题文字
"`; + exports[`ssr snapshot test renders ./src/icon/_example/index.jsx correctly 1`] = `"test"`; + +exports[`ssr snapshot test renders ./src/image/_example/base.jsx correctly 1`] = `"
\\"这是一张图片\\"
"`; + +exports[`ssr snapshot test renders ./src/image/_example/error.jsx correctly 1`] = `"
失败默认提示
失败自定义提示
"`; + +exports[`ssr snapshot test renders ./src/image/_example/fit.jsx correctly 1`] = `"
cover
fill
scale-down
contain
"`; + +exports[`ssr snapshot test renders ./src/image/_example/index.jsx correctly 1`] = `"

Image 图片

用于图片展示

基础用法

\\"这是一张图片\\"

图片形状

通过shape属性可以设置图片形状,可选项有circle|round|square,分别代表圆形、圆角方形、方形

方形
圆角方形
圆形

图片填充

通过fit属性可以设置图片填充模式,可选项有contain|cover|fill|none|scale-down,效果与原生的object-fit属性一致

cover
fill
scale-down
contain

图片位置

通过position属性可以设置图片位置,效果与原生的object-position属性一致

top
right
bottom
left

图片懒加载

通过lazy属性来开启图片懒加载

懒加载图片

加载中提示

组件提供了默认的加载中提示,也支持通过loading插槽自定义提示内容

加载默认提示
加载自定义提示

加载失败提示

组件提供了默认的加载失败提示,也支持通过error插槽自定义提示内容

失败默认提示
失败自定义提示
"`; + +exports[`ssr snapshot test renders ./src/image/_example/lazy.jsx correctly 1`] = `"
懒加载图片
"`; + +exports[`ssr snapshot test renders ./src/image/_example/loading.jsx correctly 1`] = `"
加载默认提示
加载自定义提示
"`; + +exports[`ssr snapshot test renders ./src/image/_example/position.jsx correctly 1`] = `"
top
right
bottom
left
"`; + +exports[`ssr snapshot test renders ./src/image/_example/shape.jsx correctly 1`] = `"
方形
圆角方形
圆形
"`; + +exports[`ssr snapshot test renders ./src/message/_example/base.jsx correctly 1`] = `"

Message 消息通知

用于轻量级反馈或提示,不会打断用户操作。

01 类型

弹窗内容为纯文本、标题和副标题、带输入框

02 状态

弹窗状态为普通弹窗、警示提示弹窗、成功提示弹窗、错误提示弹窗。

"`; + +exports[`ssr snapshot test renders ./src/message/_example/status.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/message/_example/types.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/popup/_example/base.jsx correctly 1`] = `"

Popup 弹窗层

由其他控件触发,屏幕滑出或弹出一块自定义内容区域

类型

弹出层出现为止可能为顶部、底部、中部、左侧或右侧

"`; + +exports[`ssr snapshot test renders ./src/popup/_example/custom-animation.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/popup/_example/placement-bottom.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/popup/_example/placement-center.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/popup/_example/placement-left.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/popup/_example/placement-right.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/popup/_example/placement-top.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/radio/_example/base.jsx correctly 1`] = `"

基础使用

单选框基础使用方法

单选
单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示
单选不可用
可取消选择
直接被选中

自定义内容

内容区域不可点击

stroke line
自定义图标
右侧选择
"`; + +exports[`ssr snapshot test renders ./src/radio/_example/group.jsx correctly 1`] = `"

单选框组使用

单选框组使用方法

北京
上海
广州
深圳
北京
上海
广州
深圳
北京
上海
广州
深圳
北京
上海
广州
深圳
"`; + +exports[`ssr snapshot test renders ./src/radio/_example/index.jsx correctly 1`] = `"

基础使用

单选框基础使用方法

单选
单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示单选多行显示
单选不可用
可取消选择
直接被选中

自定义内容

内容区域不可点击

stroke line
自定义图标
右侧选择

单选框组使用

单选框组使用方法

北京
上海
广州
深圳
北京
上海
广州
深圳
北京
上海
广州
深圳
北京
上海
广州
深圳
"`; + +exports[`ssr snapshot test renders ./src/slider/_example/base.jsx correctly 1`] = `"

Slider 滑动选择器

滑动滑块来选择一个数值,在具体场景中也可以增加来刻度和展示数值来方便用户使用

01 类型

基础滑动选择器

带数值滑动选择器

60

起始非零滑动选择器

30

带刻度滑动选择器

区间滑动选择器

0
30
70
100

02 状态

滑动选择器禁用状态

60

带刻度滑动选择器

区间滑动选择器

0
30
70
100

03 规格

无标题滑动选择器

有标题滑动选择器

选择器标题
"`; + +exports[`ssr snapshot test renders ./src/slider/_example/mark.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/slider/_example/range.jsx correctly 1`] = `"
0
30
70
100
"`; + +exports[`ssr snapshot test renders ./src/slider/_example/value.jsx correctly 1`] = `"
65
"`; + +exports[`ssr snapshot test renders ./src/sticky/_example/base.jsx correctly 1`] = `"

Sticky 吸顶

用于常驻页面顶部的信息,操作展示

01 类型

基础吸顶

吸顶距离

指定容器

"`; + +exports[`ssr snapshot test renders ./src/swiper/_example/base.jsx correctly 1`] = `"
itme3
itme1
itme2
itme3
itme1
"`; + +exports[`ssr snapshot test renders ./src/swiper/_example/control.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/swiper/_example/direction.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/swiper/_example/index.jsx correctly 1`] = `"

Swiper 轮播图

用于循环轮播一组图片或内容,也可以滑动进行切换,轮播动效时间可以设置

01 类型

轮播图的多种样式

1/5
"`; + +exports[`ssr snapshot test renders ./src/swiper/_example/pagination-type.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/swiper/_example/test.jsx correctly 1`] = `"
"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/bottom.jsx correctly 1`] = `"

底部选项卡

标签页一
标签页二
标签页三
标签页四
内容内容
"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/horizontal.jsx correctly 1`] = `"

横向选项卡

"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/index.jsx correctly 1`] = `"

Tabs 选项卡

用于切换不同场景

横向选项卡

  • 标签1
    标签2222
  • 标签一
    标签二
    tab禁用
    这个标签有点长
  • 标签页一
    标签页二
    标签页三
    标签页四

竖向选项卡

top='left' | top = 'right

  • 标签1
    标签222
    标签3
    标签4
  • 标签页一
    标签页二
    标签页三
    标签页四
    内容内容

可滚动的选项卡

元素过多时可滚动,点击可自动调整滚动位置

标签页一
标签页二
标签页三
标签页四
标签页五
标签页六

不同尺寸

大 中 小

  • 标签页一
    标签页二
    标签页三
    标签页四
  • 标签页一
    标签页二
    标签页三
    标签页四
  • 标签页一
    标签页二
    标签页三
    标签页四

不显示激活线

  • 标签页一
    标签页二
    标签页三
    标签页四

底部选项卡

标签页一
标签页二
标签页三
标签页四
内容内容
"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/noline.jsx correctly 1`] = `"

不显示激活线

"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/scroll.jsx correctly 1`] = `"

可滚动的选项卡

元素过多时可滚动,点击可自动调整滚动位置

标签页一
标签页二
标签页三
标签页四
标签页五
标签页六
"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/size.jsx correctly 1`] = `"

不同尺寸

大 中 小

"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/test.jsx correctly 1`] = `"
test1
"`; + +exports[`ssr snapshot test renders ./src/tabs/_example/vetical.jsx correctly 1`] = `"

竖向选项卡

top='left' | top = 'right

"`; diff --git a/test/ssr/ssr.test.js b/test/ssr/ssr.test.js index 324040ad..9f15289a 100644 --- a/test/ssr/ssr.test.js +++ b/test/ssr/ssr.test.js @@ -15,8 +15,10 @@ function ssrSnapshotTest() { it(`renders ${file} correctly`, async () => { const demo = require(`../.${file}`); const RealDemoComp = demo.default ? demo.default : demo; - const ElementImageHtml = renderToString(); - expect(ElementImageHtml).toMatchSnapshot(); + if (typeof RealDemoComp === 'function') { + const ElementImageHtml = renderToString(); + expect(ElementImageHtml).toMatchSnapshot(); + } }, 2000); }); }); diff --git a/test/utils/test-examples.ts b/test/utils/test-examples.ts index 8fed4b9c..4f7a3016 100644 --- a/test/utils/test-examples.ts +++ b/test/utils/test-examples.ts @@ -25,14 +25,15 @@ export function testExamples(dirname: string, overrides: TestExampleOverrides = } const Example = require(path.join(exampleDir, exampleFilename)).default; + if (Example) { + // prettier-ignore + const runner = overrides[exampleFilename] + || (() => { + const { asFragment } = render(React.createElement(Example)); + expect(asFragment()).toMatchSnapshot(); + }); - // prettier-ignore - const runner = overrides[exampleFilename] - || (() => { - const { asFragment } = render(React.createElement(Example)); - expect(asFragment()).toMatchSnapshot(); - }); - - test(exampleFilename, runner); + test(exampleFilename, runner); + } } } From b3bb328c1df2a4457f55504478dbcc5233844e3b Mon Sep 17 00:00:00 2001 From: carolin913n Date: Wed, 16 Mar 2022 14:19:45 +0800 Subject: [PATCH 2/2] fix(): popconfirm locale text --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 289a7dfe..5d5765f0 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "postsite:preview": "cp _site/index.html _site/404.html", "lint": "eslint --ext .ts,.tsx ./ --max-warnings 0", "lint:fix": "eslint --ext .ts,.tsx ./ --max-warnings 0 --fix", - "test": "echo \"no test specified, work in progress\"", + "test": "npm run test:unit", "test:update": "npm run test:unit-update", "test:unit": "bash -c 'jest --coverage --config test/config/jest.unit.conf.js ${1}' -- ", "test:unit-update": "jest --config test/config/jest.unit.conf.js --updateSnapshot --coverage",