wheel.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import BScroll from 'scroll/index'
  2. describe('BScroll - wheel', () => {
  3. let wheels = []
  4. let wrapper = null
  5. beforeEach(() => {
  6. let style = document.createElement('style')
  7. style.type = 'text/css'
  8. const styleSheet = `
  9. ul {
  10. width: 100px;
  11. list-style: none;
  12. display: inline-block;
  13. margin: 0;
  14. }
  15. .wheel-wrapper {
  16. display: flex;
  17. overflow: hidden;
  18. }
  19. .wheel {
  20. flex: 1;
  21. height: 173px;
  22. }
  23. .wheel-scroll {
  24. padding: 0;
  25. margin-top: 68px;
  26. line-height: 36px;
  27. list-style: none;
  28. }
  29. .wheel-disabled-item {
  30. color: red
  31. }
  32. `
  33. style.appendChild(document.createTextNode(styleSheet))
  34. document.head.appendChild(style)
  35. wrapper = document.createElement('div')
  36. wrapper.className = 'wheel-wrapper'
  37. const listArr = []
  38. document.body.appendChild(wrapper)
  39. for (let i = 0; i < 3; i++) {
  40. let ulHTML = ''
  41. const ul = document.createElement('ul')
  42. ul.className = 'wheel-scroll'
  43. const scroller = document.createElement('div')
  44. scroller.className = 'wheel'
  45. for (let i = 0; i < 100; i++) {
  46. if (i === 0) {
  47. ulHTML += `<li class="wheel-item wheel-disabled-item">${i}</li>`
  48. } else {
  49. ulHTML += `<li class="wheel-item">${i}</li>`
  50. }
  51. }
  52. ul.innerHTML = ulHTML
  53. listArr.push(ul)
  54. scroller.appendChild(ul)
  55. wrapper.appendChild(scroller)
  56. wheels[i] = new BScroll(wrapper.children[i], {
  57. wheel: {
  58. selectedIndex: 0
  59. },
  60. probeType: 3
  61. })
  62. }
  63. })
  64. afterEach(() => {
  65. wheels = []
  66. wrapper = null
  67. document.body.removeChild(document.querySelector('.wheel-wrapper'))
  68. })
  69. it('the wheel wrapper class name should be use default value', () => {
  70. const wheel = wheels[0]
  71. expect(wheel.options.wheel.wheelWrapperClass).to.equal('wheel-scroll')
  72. expect(wheel.options.wheel.wheelItemClass).to.equal('wheel-item')
  73. })
  74. it('wheelTo', () => {
  75. const wheel = wheels[0]
  76. wheel.wheelTo(4)
  77. expect(wheel.getSelectedIndex())
  78. .to.equal(4)
  79. expect(wheel.y)
  80. .to.equal(-1 * wheel.itemHeight * 4)
  81. })
  82. it('getSelectedIndex', () => {
  83. const [firstWheel, secondWheel, thirdWheel] = [...wheels]
  84. expect(firstWheel.getSelectedIndex())
  85. .to.equal(1)
  86. expect(secondWheel.getSelectedIndex())
  87. .to.equal(1)
  88. expect(thirdWheel.getSelectedIndex())
  89. .to.equal(1)
  90. })
  91. it('will set selectedIndex to 0 when wheel.getSelectedIndex is undefined', () => {
  92. const firstWheel = new BScroll(wrapper.children[0], {
  93. wheel: {
  94. selectedIndex: undefined
  95. },
  96. probeType: 3
  97. })
  98. expect(firstWheel.options.startY).to.equal(0)
  99. })
  100. it('find nearest enable item', () => {
  101. const firstWheel = new BScroll(wrapper.children[0], {
  102. wheel: {
  103. selectedIndex: 0
  104. },
  105. probeType: 3
  106. })
  107. expect(firstWheel.selectedIndex).to.equal(1)
  108. })
  109. })