Skip to content

Commit 33731ef

Browse files
authored
Merge pull request #43 from didi/feat-slider
Test slider
2 parents b037be1 + c61f807 commit 33731ef

4 files changed

Lines changed: 196 additions & 6 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 event check disabled event check 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>"`;
8+
9+
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>"`;
10+
11+
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>"`;
12+
13+
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>"`;
14+
15+
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>"`;
16+
17+
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: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
// disabled情况下不触发事件
62+
it('disabled event check', async () => {
63+
const component = newComponent({ disabled: true })
64+
expect(component.dom.innerHTML).toMatchSnapshot()
65+
const change = jest.fn()
66+
const change2 = jest.fn()
67+
const changing = jest.fn()
68+
// 触发组件树中的节点自定义事件
69+
component.addEventListener('change', change)
70+
component.addEventListener('changing', changing)
71+
const tabArea = component.querySelector('.cube-slider-tab-area')
72+
const sliderHandle = component.querySelector('.cube-slider-handle')
73+
tabArea.dispatchEvent('tap')
74+
sliderHandle.dispatchEvent('touchstart')
75+
sliderHandle.dispatchEvent('touchmove')
76+
sliderHandle.dispatchEvent('touchend')
77+
await simulate.sleep(10)
78+
expect(change).not.toHaveBeenCalled()
79+
expect(change2).not.toHaveBeenCalled()
80+
expect(changing).not.toHaveBeenCalled()
81+
})
82+
})
83+
84+
describe('props check', () => {
85+
it('prop default', async () => {
86+
const DEFAULT_PROPS = {
87+
min: 0,
88+
max: 100,
89+
step: 1,
90+
disabled: false,
91+
value: 0,
92+
backgroundColor: '',
93+
activeColor: '',
94+
'block-size': 28,
95+
'block-color': '',
96+
showValue: false,
97+
customContent: false
98+
}
99+
const component = newComponent()
100+
const data = component.instance.data
101+
expect(data.min).toBe(DEFAULT_PROPS.min)
102+
expect(data.max).toBe(DEFAULT_PROPS.max)
103+
expect(data.step).toBe(DEFAULT_PROPS.step)
104+
expect(data.disabled).toBe(DEFAULT_PROPS.disabled)
105+
expect(data.currentValue).toBe(DEFAULT_PROPS.value)
106+
expect(data.backgroundColor).toBe(DEFAULT_PROPS.backgroundColor)
107+
expect(data.activeColor).toBe(DEFAULT_PROPS.activeColor)
108+
expect(data['block-size']).toBe(DEFAULT_PROPS['block-size'])
109+
expect(data['block-color']).toBe(DEFAULT_PROPS['block-color'])
110+
expect(data.showValue).toBe(DEFAULT_PROPS.showValue)
111+
expect(data.customContent).toBe(DEFAULT_PROPS.customContent)
112+
})
113+
114+
it('props', async () => {
115+
// 中横线的props不支持(实际上没问题),暂时没测,比如'block-size'、'block-color'
116+
const PROPS = {
117+
min: 50,
118+
max: 200,
119+
step: 2,
120+
disabled: true,
121+
value: 50,
122+
backgroundColor: '#fff',
123+
activeColor: '#ff7e33',
124+
showValue: true,
125+
customContent: true
126+
}
127+
const component = newComponent(PROPS)
128+
const data = component.instance.data
129+
await simulate.sleep(10)
130+
expect(component.dom.innerHTML).toMatchSnapshot()
131+
expect(data.min).toBe(PROPS.min)
132+
expect(data.max).toBe(PROPS.max)
133+
expect(data.step).toBe(PROPS.step)
134+
expect(data.disabled).toBe(PROPS.disabled)
135+
expect(data.currentValue).toBe(PROPS.value)
136+
expect(data.backgroundColor).toBe(PROPS.backgroundColor)
137+
expect(data.activeColor).toBe(PROPS.activeColor)
138+
expect(data.showValue).toBe(PROPS.showValue)
139+
expect(data.customContent).toBe(PROPS.customContent)
140+
})
141+
})
142+
143+
describe('slot check', () => {
144+
const text = '123'
145+
const component = simulate.render(simulate.load({
146+
usingComponents: {
147+
'cube-slider': componentId
148+
},
149+
template: `
150+
<cube-slider
151+
value="{{ 30 }}"
152+
custom-content="{{true}}"
153+
>
154+
<view class="slider-thumb">${text}</view>
155+
</cube-slider>
156+
`
157+
}))
158+
component.attach(document.createElement('parent'))
159+
160+
it('matchSnapshot', () => {
161+
expect(component.dom.innerHTML).toMatchSnapshot()
162+
})
163+
164+
it('check render correct contents', () => {
165+
const text = component.querySelector('.slider-thumb').dom.innerHTML
166+
expect(text).toBe(text)
167+
})
168+
})
169+
})

packages/mpx-cube-ui/src/components/slider/index.mpx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
capture-catch:touchend="endHandler"
1919
>
2020
<!-- 滑块儿 -->
21-
<view class="cube-slider-thumb-custom" wx:style="{{ { display: customContent ? '' : 'none', ...thumbStyle } }}">
21+
<view class="cube-slider-thumb-custom" wx:style="{{ thumbCustomStyle }}">
2222
<!-- 自定义滑块儿 -->
2323
<slot></slot>
2424
</view>

packages/mpx-cube-ui/src/components/slider/slider.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { MOUNTED } from '@mpxjs/core'
21
import { createComponent } from '../../common/helper/create-component'
32

43
const EVENT_CHANGE = 'change' // 完成一次拖动后触发的事件
@@ -118,10 +117,12 @@ createComponent({
118117
immediate: true
119118
}
120119
},
121-
[MOUNTED]() {
122-
this.getRect().then(res => {
123-
this.startDragRect = res
124-
})
120+
lifetimes: {
121+
ready() {
122+
this.getRect().then(res => {
123+
this.startDragRect = res
124+
})
125+
}
125126
},
126127
computed: {
127128
sliderClass() {
@@ -166,6 +167,9 @@ createComponent({
166167
}
167168
return style
168169
},
170+
thumbCustomStyle() {
171+
return { display: this.customContent ? '' : 'none', ...this.thumbStyle }
172+
},
169173
trackStyle() {
170174
const style = {
171175
backgroundColor: this.backgroundColor || this.color

0 commit comments

Comments
 (0)