|
| 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