Skip to content

Commit c57da80

Browse files
committed
test: slider
1 parent 8d98526 commit c57da80

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`component slider unit test event check change event 1`] = `"<wx-view style=\\"margin-left:18px;margin-right:18px;\\" class=\\"main--cube-slider\\"><wx-view class=\\"main--cube-slider-tab-area\\" style=\\"padding-top:14px;padding-bottom:14px;\\"><wx-view class=\\"main--cube-slider-handle\\" style=\\"left:0%;\\"><wx-view class=\\"main--cube-slider-thumb-custom\\" style=\\"display:none;width:28px;height:28px;\\"></wx-view><wx-view class=\\"main--cube-slider-thumb\\" style=\\"width:28px;height:28px;\\"></wx-view></wx-view><wx-view class=\\"main--cube-slider-track\\" style=\\"\\"><wx-view class=\\"main--cube-slider-step\\" style=\\"width:0%;\\"></wx-view></wx-view></wx-view></wx-view>"`;
4+
5+
exports[`component slider unit test event check changing event 1`] = `"<wx-view style=\\"margin-left:18px;margin-right:18px;\\" class=\\"main--cube-slider\\"><wx-view class=\\"main--cube-slider-tab-area\\" style=\\"padding-top:14px;padding-bottom:14px;\\"><wx-view class=\\"main--cube-slider-handle\\" style=\\"left:0%;\\"><wx-view class=\\"main--cube-slider-thumb-custom\\" style=\\"display:none;width:28px;height:28px;\\"></wx-view><wx-view class=\\"main--cube-slider-thumb\\" style=\\"width:28px;height:28px;\\"></wx-view></wx-view><wx-view class=\\"main--cube-slider-track\\" style=\\"\\"><wx-view class=\\"main--cube-slider-step\\" style=\\"width:0%;\\"></wx-view></wx-view></wx-view></wx-view>"`;
6+
7+
exports[`component slider unit test props check props 1`] = `"<wx-view style=\\"margin-left:18px;margin-right:18px;\\" class=\\"main--cube-slider\\"><wx-view class=\\"main--cube-slider-tab-area\\" style=\\"padding-top:14px;padding-bottom:14px;\\"><wx-view class=\\"main--cube-slider-handle\\" style=\\"left:0%;\\"><wx-view class=\\"main--cube-slider-thumb-custom\\" style=\\"display:;width:28px;height:28px;\\"></wx-view></wx-view><wx-view class=\\"main--cube-slider-track\\" style=\\"background-color:#fff;\\"><wx-view class=\\"main--cube-slider-step\\" style=\\"width:0%;background-color:#ff7e33;\\"></wx-view></wx-view></wx-view><wx-view class=\\"main--cube-slider-value\\"><wx-text>50</wx-text></wx-view></wx-view>"`;
8+
9+
exports[`component slider unit test render check render 1`] = `"<wx-view style=\\"margin-left:18px;margin-right:18px;\\" class=\\"main--cube-slider\\"><wx-view class=\\"main--cube-slider-tab-area\\" style=\\"padding-top:14px;padding-bottom:14px;\\"><wx-view class=\\"main--cube-slider-handle\\" style=\\"left:0%;\\"><wx-view class=\\"main--cube-slider-thumb-custom\\" style=\\"display:none;width:28px;height:28px;\\"></wx-view><wx-view class=\\"main--cube-slider-thumb\\" style=\\"width:28px;height:28px;\\"></wx-view></wx-view><wx-view class=\\"main--cube-slider-track\\" style=\\"\\"><wx-view class=\\"main--cube-slider-step\\" style=\\"width:0%;\\"></wx-view></wx-view></wx-view></wx-view>"`;
10+
11+
exports[`component slider unit test render check render 2`] = `"<wx-view style=\\"margin-left:18px;margin-right:18px;\\" class=\\"main--cube-slider\\"><wx-view class=\\"main--cube-slider-tab-area\\" style=\\"padding-top:14px;padding-bottom:14px;\\"><wx-view class=\\"main--cube-slider-handle\\" style=\\"left:0%;\\"><wx-view class=\\"main--cube-slider-thumb-custom\\" style=\\"display:none;width:28px;height:28px;\\"></wx-view><wx-view class=\\"main--cube-slider-thumb\\" style=\\"width:28px;height:28px;\\"></wx-view></wx-view><wx-view class=\\"main--cube-slider-track\\" style=\\"\\"><wx-view class=\\"main--cube-slider-step\\" style=\\"width:0%;\\"></wx-view></wx-view></wx-view><wx-view class=\\"main--cube-slider-value\\"><wx-text>0</wx-text></wx-view></wx-view>"`;
12+
13+
exports[`component slider unit test render check render 3`] = `"<wx-view style=\\"margin-left:18px;margin-right:18px;\\" class=\\"main--cube-slider\\"><wx-view class=\\"main--cube-slider-tab-area\\" style=\\"padding-top:14px;padding-bottom:14px;\\"><wx-view class=\\"main--cube-slider-handle\\" style=\\"left:0%;\\"><wx-view class=\\"main--cube-slider-thumb-custom\\" style=\\"display:;width:28px;height:28px;\\"></wx-view></wx-view><wx-view class=\\"main--cube-slider-track\\" style=\\"\\"><wx-view class=\\"main--cube-slider-step\\" style=\\"width:0%;\\"></wx-view></wx-view></wx-view></wx-view>"`;
14+
15+
exports[`component slider unit test slot check matchSnapshot 1`] = `"<main><wx-view style=\\"margin-left:18px;margin-right:18px;\\" class=\\"main--cube-slider\\"><wx-view class=\\"main--cube-slider-tab-area\\" style=\\"padding-top:14px;padding-bottom:14px;\\"><wx-view class=\\"main--cube-slider-handle\\" style=\\"left:30%;\\"><wx-view class=\\"main--cube-slider-thumb-custom\\" style=\\"display:;width:28px;height:28px;\\"><wx-view class=\\"slider-thumb\\">123</wx-view></wx-view></wx-view><wx-view class=\\"main--cube-slider-track\\" style=\\"\\"><wx-view class=\\"main--cube-slider-step\\" style=\\"width:30%;\\"></wx-view></wx-view></wx-view></wx-view></main>"`;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
const simulate = require('@mpxjs/miniprogram-simulate')
2+
3+
describe('component slider unit test', function () {
4+
const componentId = simulate.loadMpx('src/components/slider/index.mpx')
5+
6+
function newComponent(props) {
7+
const component = simulate.render(componentId, props)
8+
const parent = document.createElement('parent')
9+
component.attach(parent) // 会触发 attach 生命周期
10+
return component
11+
}
12+
13+
async function snapTest(props) {
14+
const component = newComponent(props)
15+
await simulate.sleep(10)
16+
expect(component.dom.innerHTML).toMatchSnapshot()
17+
}
18+
19+
describe('render check', () => {
20+
it('render', async () => {
21+
await snapTest()
22+
await snapTest({
23+
showValue: true
24+
})
25+
await snapTest({
26+
customContent: true
27+
})
28+
})
29+
})
30+
31+
describe('event check', () => {
32+
it('change event', async () => {
33+
const component = newComponent()
34+
expect(component.dom.innerHTML).toMatchSnapshot()
35+
const change = jest.fn()
36+
// 触发组件树中的节点自定义事件
37+
component.addEventListener('change', change)
38+
const tabArea = component.querySelector('.cube-slider-tab-area')
39+
tabArea.dispatchEvent('tap')
40+
await simulate.sleep(10)
41+
expect(change).toHaveBeenCalled()
42+
})
43+
44+
it('changing event', async () => {
45+
const component = newComponent()
46+
expect(component.dom.innerHTML).toMatchSnapshot()
47+
const change = jest.fn()
48+
const changing = jest.fn()
49+
// 触发组件树中的节点自定义事件
50+
component.addEventListener('change', change)
51+
component.addEventListener('changing', changing)
52+
const sliderHandle = component.querySelector('.cube-slider-handle')
53+
sliderHandle.dispatchEvent('touchstart')
54+
sliderHandle.dispatchEvent('touchmove')
55+
sliderHandle.dispatchEvent('touchend')
56+
await simulate.sleep(10)
57+
expect(change).toHaveBeenCalled()
58+
expect(changing).toHaveBeenCalled()
59+
})
60+
})
61+
62+
describe('props check', () => {
63+
it('prop default', async () => {
64+
const DEFAULT_PROPS = {
65+
min: 0,
66+
max: 100,
67+
step: 1,
68+
disabled: false,
69+
value: 0,
70+
backgroundColor: '',
71+
activeColor: '',
72+
'block-size': 28,
73+
'block-color': '',
74+
showValue: false,
75+
customContent: false
76+
}
77+
const component = newComponent()
78+
const data = component.instance.data
79+
expect(data.min).toBe(DEFAULT_PROPS.min)
80+
expect(data.max).toBe(DEFAULT_PROPS.max)
81+
expect(data.step).toBe(DEFAULT_PROPS.step)
82+
expect(data.disabled).toBe(DEFAULT_PROPS.disabled)
83+
expect(data.currentValue).toBe(DEFAULT_PROPS.value)
84+
expect(data.backgroundColor).toBe(DEFAULT_PROPS.backgroundColor)
85+
expect(data.activeColor).toBe(DEFAULT_PROPS.activeColor)
86+
expect(data['block-size']).toBe(DEFAULT_PROPS['block-size'])
87+
expect(data['block-color']).toBe(DEFAULT_PROPS['block-color'])
88+
expect(data.showValue).toBe(DEFAULT_PROPS.showValue)
89+
expect(data.customContent).toBe(DEFAULT_PROPS.customContent)
90+
})
91+
92+
it('props', async () => {
93+
// 中横线的props不支持(实际上没问题),暂时没测,比如'block-size'、'block-color'
94+
const PROPS = {
95+
min: 50,
96+
max: 200,
97+
step: 2,
98+
disabled: true,
99+
value: 50,
100+
backgroundColor: '#fff',
101+
activeColor: '#ff7e33',
102+
showValue: true,
103+
customContent: true
104+
}
105+
const component = newComponent(PROPS)
106+
const data = component.instance.data
107+
await simulate.sleep(10)
108+
expect(component.dom.innerHTML).toMatchSnapshot()
109+
expect(data.min).toBe(PROPS.min)
110+
expect(data.max).toBe(PROPS.max)
111+
expect(data.step).toBe(PROPS.step)
112+
expect(data.disabled).toBe(PROPS.disabled)
113+
expect(data.currentValue).toBe(PROPS.value)
114+
expect(data.backgroundColor).toBe(PROPS.backgroundColor)
115+
expect(data.activeColor).toBe(PROPS.activeColor)
116+
expect(data.showValue).toBe(PROPS.showValue)
117+
expect(data.customContent).toBe(PROPS.customContent)
118+
})
119+
})
120+
121+
describe('slot check', () => {
122+
const text = '123'
123+
const component = simulate.render(simulate.load({
124+
usingComponents: {
125+
'cube-slider': componentId
126+
},
127+
template: `
128+
<cube-slider
129+
value="{{ 30 }}"
130+
custom-content="{{true}}"
131+
>
132+
<view class="slider-thumb">${text}</view>
133+
</cube-slider>
134+
`
135+
}))
136+
component.attach(document.createElement('parent'))
137+
138+
it('matchSnapshot', () => {
139+
expect(component.dom.innerHTML).toMatchSnapshot()
140+
})
141+
142+
it('check render correct contents', () => {
143+
const text = component.querySelector('.slider-thumb').dom.innerHTML
144+
expect(text).toBe(text)
145+
})
146+
})
147+
})

0 commit comments

Comments
 (0)