index.spec.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import Rate from '..';
  2. import { mount, triggerDrag } from '../../../test/utils';
  3. test('change event', () => {
  4. const onInput = jest.fn();
  5. const onChange = jest.fn();
  6. const wrapper = mount(Rate, {
  7. context: {
  8. on: {
  9. input: onInput,
  10. change: onChange
  11. }
  12. }
  13. });
  14. const item4 = wrapper.findAll('.van-rate__icon').at(3);
  15. item4.trigger('click');
  16. expect(onInput).toHaveBeenCalledWith(4);
  17. expect(onChange).toHaveBeenCalledWith(4);
  18. });
  19. test('allow half', () => {
  20. const onInput = jest.fn();
  21. const onChange = jest.fn();
  22. const wrapper = mount(Rate, {
  23. propsData: {
  24. allowHalf: true
  25. },
  26. context: {
  27. on: {
  28. input: onInput,
  29. change: onChange
  30. }
  31. }
  32. });
  33. const item4 = wrapper.findAll('.van-rate__icon--half').at(3);
  34. item4.trigger('click');
  35. expect(onInput).toHaveBeenCalledWith(3.5);
  36. expect(onChange).toHaveBeenCalledWith(3.5);
  37. });
  38. test('disabled', () => {
  39. const onInput = jest.fn();
  40. const onChange = jest.fn();
  41. const wrapper = mount(Rate, {
  42. propsData: {
  43. disabled: true
  44. },
  45. context: {
  46. on: {
  47. input: onInput,
  48. change: onChange
  49. }
  50. }
  51. });
  52. const item4 = wrapper.findAll('.van-rate__item').at(3);
  53. item4.trigger('click');
  54. expect(onInput).toHaveBeenCalledTimes(0);
  55. expect(onChange).toHaveBeenCalledTimes(0);
  56. });
  57. test('touchmove', () => {
  58. const onChange = jest.fn();
  59. const wrapper = mount(Rate, {
  60. context: {
  61. on: {
  62. change: onChange
  63. }
  64. }
  65. });
  66. triggerDrag(wrapper, 100, 0);
  67. const icons = wrapper.findAll('.van-icon');
  68. document.elementFromPoint = function (x) {
  69. const index = Math.round(x / 20);
  70. if (index < icons.length) {
  71. return icons.at(index).element;
  72. }
  73. };
  74. triggerDrag(wrapper, 100, 0);
  75. expect(onChange).toHaveBeenNthCalledWith(1, 2);
  76. expect(onChange).toHaveBeenNthCalledWith(2, 3);
  77. expect(onChange).toHaveBeenNthCalledWith(3, 4);
  78. });