|
| 1 | + |
| 2 | +import React from 'react'; |
| 3 | +import { assert, expect } from 'chai'; |
| 4 | +import sinon from 'sinon'; |
| 5 | +import { mount, shallow } from 'enzyme'; |
| 6 | +import DatePicker from '../../lib/DatePicker'; |
| 7 | +import {nextTime} from '../../lib/time'; |
| 8 | + |
| 9 | +describe('时间选择器组件DatePicker', () => { |
| 10 | + describe('测试初始化的过程:', () => { |
| 11 | + it('测试传入_setOpacity的参数', () => { |
| 12 | + const spyFunction = sinon.spy(DatePicker.prototype, '_setOpacity'); |
| 13 | + const datePicker = mount( |
| 14 | + <DatePicker /> |
| 15 | + ); |
| 16 | + |
| 17 | + const list = [ |
| 18 | + spyFunction.getCall(0).args[0], |
| 19 | + spyFunction.getCall(1).args[0], |
| 20 | + spyFunction.getCall(2).args[0], |
| 21 | + spyFunction.getCall(3).args[0], |
| 22 | + spyFunction.getCall(4).args[0], |
| 23 | + ]; |
| 24 | + |
| 25 | + const expectList = [45, 22.5, 0, -22.5, -45]; |
| 26 | + expect(list).to.deep.equals(expectList); |
| 27 | + spyFunction.restore(); |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + |
| 32 | + describe('测试方法:', () => { |
| 33 | + it('_setOpacity应该返回相应的值,当传入不同的临界值', () => { |
| 34 | + const datePicker = mount( |
| 35 | + <DatePicker /> |
| 36 | + ); |
| 37 | + const inst = datePicker.instance(); |
| 38 | + |
| 39 | + const test1 = [0, 1]; |
| 40 | + const test2 = [40, 0]; |
| 41 | + const test3 = [-40, 0]; |
| 42 | + |
| 43 | + expect(inst._setOpacity(test1[0])).to.equals(test1[1]); |
| 44 | + expect(inst._setOpacity(test2[0])).to.equals(test2[1]); |
| 45 | + expect(inst._setOpacity(test3[0])).to.equals(test3[1]); |
| 46 | + }) |
| 47 | + |
| 48 | + it('应该滑动到上一个日期,当调用_moveToNext方法', () => { |
| 49 | + const spyFunction = sinon.spy(); |
| 50 | + const datePicker = mount( |
| 51 | + <DatePicker |
| 52 | + onSelect={spyFunction}/> |
| 53 | + ); |
| 54 | + datePicker.instance()._moveToNext(-1); |
| 55 | + datePicker.find('.datepicker-finish-btn').simulate('click'); |
| 56 | + sinon.assert.calledWith(spyFunction, nextTime(new Date(), -1)); |
| 57 | + }) |
| 58 | + |
| 59 | + |
| 60 | + it('应该向_moveToNext传入-1, 当滑动到上一个日期', () => { |
| 61 | + const spyFunction = sinon.spy(DatePicker.prototype, '_moveToNext'); |
| 62 | + const datePicker = mount( |
| 63 | + <DatePicker /> |
| 64 | + ); |
| 65 | + const touchstartEvent = { |
| 66 | + type: 'touchstart', |
| 67 | + targetTouches: [{ pageY: 0 }], |
| 68 | + }; |
| 69 | + const touchendEvent = { |
| 70 | + type: 'touchend', |
| 71 | + changedTouches: [{ pageY: 50 }], |
| 72 | + }; |
| 73 | + |
| 74 | + |
| 75 | + datePicker.find('.datepicker-content').simulate('touchStart', touchstartEvent); |
| 76 | + datePicker.find('.datepicker-content').simulate('touchEnd', touchendEvent); |
| 77 | + sinon.assert.calledWith(spyFunction, -1); |
| 78 | + spyFunction.restore(); |
| 79 | + }) |
| 80 | + |
| 81 | + }) |
| 82 | + |
| 83 | + describe('测试交互事件:', () => { |
| 84 | + it('应该触发handleContent3次, 在触摸屏幕之后', () => { |
| 85 | + const spyFunction = sinon.spy(DatePicker.prototype, 'handleContentTouch'); |
| 86 | + const datePicker = mount( |
| 87 | + <DatePicker /> |
| 88 | + ); |
| 89 | + |
| 90 | + datePicker.find('.datepicker-content').simulate('touchStart'); |
| 91 | + datePicker.find('.datepicker-content').simulate('touchMove'); |
| 92 | + datePicker.find('.datepicker-content').simulate('touchEnd'); |
| 93 | + sinon.assert.callCount(spyFunction, 3); |
| 94 | + spyFunction.restore(); |
| 95 | + }) |
| 96 | + |
| 97 | + it('应该触发onSelect, 在点击完成按钮之后', () => { |
| 98 | + const spyFunction = sinon.spy(); |
| 99 | + const datePicker = mount( |
| 100 | + <DatePicker |
| 101 | + onSelect={spyFunction}/> |
| 102 | + ); |
| 103 | + datePicker.find('.datepicker-finish-btn').simulate('click'); |
| 104 | + sinon.assert.calledOnce(spyFunction); |
| 105 | + }) |
| 106 | + |
| 107 | + |
| 108 | + it('应该向onSelect传入上一个日期, 当向下滑动超过touchLen', () => { |
| 109 | + const spyFunction = sinon.spy(); |
| 110 | + const datePicker = mount( |
| 111 | + <DatePicker |
| 112 | + onSelect={spyFunction}/> |
| 113 | + ); |
| 114 | + const touchstartEvent = { |
| 115 | + type: 'touchstart', |
| 116 | + targetTouches: [{ pageY: 0 }], |
| 117 | + }; |
| 118 | + const touchendEvent = { |
| 119 | + type: 'touchend', |
| 120 | + changedTouches: [{ pageY: 50 }], |
| 121 | + }; |
| 122 | + |
| 123 | + datePicker.find('.datepicker-content').simulate('touchStart', touchstartEvent); |
| 124 | + datePicker.find('.datepicker-content').simulate('touchEnd', touchendEvent); |
| 125 | + datePicker.find('.datepicker-content').simulate('transitionEnd'); |
| 126 | + datePicker.find('.datepicker-finish-btn').simulate('click'); |
| 127 | + sinon.assert.calledWith(spyFunction, nextTime(new Date(), -1)); |
| 128 | + }) |
| 129 | + |
| 130 | + it('应该向onSelect传入下一个日期, 当向上滑动超过touchLen', () => { |
| 131 | + const spyFunction = sinon.spy(); |
| 132 | + const datePicker = mount( |
| 133 | + <DatePicker |
| 134 | + maxDate={nextTime(new Date(), 1)} |
| 135 | + onSelect={spyFunction}/> |
| 136 | + ); |
| 137 | + const touchstartEvent = { |
| 138 | + type: 'touchstart', |
| 139 | + targetTouches: [{ pageY: 0 }], |
| 140 | + }; |
| 141 | + const touchendEvent = { |
| 142 | + type: 'touchend', |
| 143 | + changedTouches: [{ pageY: -50 }], |
| 144 | + }; |
| 145 | + |
| 146 | + datePicker.find('.datepicker-content').simulate('touchStart', touchstartEvent); |
| 147 | + datePicker.find('.datepicker-content').simulate('touchEnd', touchendEvent); |
| 148 | + datePicker.find('.datepicker-content').simulate('transitionEnd'); |
| 149 | + datePicker.find('.datepicker-finish-btn').simulate('click'); |
| 150 | + sinon.assert.calledWith(spyFunction, nextTime(new Date(), 1)); |
| 151 | + }) |
| 152 | + |
| 153 | + it('应该恢复当前日期, 但滑动超过touchLen但上一个日期小于minDate', () => { |
| 154 | + const spyFunction = sinon.spy(); |
| 155 | + const datePicker = mount( |
| 156 | + <DatePicker |
| 157 | + minDate={nextTime(new Date())} |
| 158 | + onSelect={spyFunction}/> |
| 159 | + ); |
| 160 | + const touchstartEvent = { |
| 161 | + type: 'touchstart', |
| 162 | + targetTouches: [{ pageY: 0 }], |
| 163 | + }; |
| 164 | + const touchendEvent = { |
| 165 | + type: 'touchend', |
| 166 | + changedTouches: [{ pageY: 50 }], |
| 167 | + }; |
| 168 | + |
| 169 | + datePicker.find('.datepicker-content').simulate('touchStart', touchstartEvent); |
| 170 | + datePicker.find('.datepicker-content').simulate('touchEnd', touchendEvent); |
| 171 | + datePicker.find('.datepicker-content').simulate('transitionEnd'); |
| 172 | + datePicker.find('.datepicker-finish-btn').simulate('click'); |
| 173 | + sinon.assert.calledWith(spyFunction, nextTime(new Date(), 0)); |
| 174 | + }) |
| 175 | + }) |
| 176 | + |
| 177 | +}); |
0 commit comments