index.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import Coupon from '../../coupon';
  2. import CouponList from '..';
  3. import CouponCell from '../../coupon-cell';
  4. import { later, mount } from '../../../test/utils';
  5. const coupon = {
  6. id: 1,
  7. discount: 0,
  8. denominations: 150,
  9. originCondition: 0,
  10. reason: '',
  11. value: 150,
  12. name: 'name',
  13. description: 'description',
  14. startAt: 1489104000,
  15. endAt: 1514592000
  16. };
  17. const coupon2 = {
  18. ...coupon,
  19. denominations: 100
  20. };
  21. const coupon3 = {
  22. ...coupon,
  23. denominations: 123
  24. };
  25. const emptyCoupon = {
  26. id: 0,
  27. discount: 0,
  28. denominations: 0,
  29. originCondition: 0,
  30. startAt: 1489104000,
  31. endAt: 1514592000
  32. };
  33. const discountCoupon = {
  34. ...coupon,
  35. id: 2,
  36. discount: 88,
  37. denominations: 0,
  38. originCondition: 50,
  39. value: 12,
  40. description: ''
  41. };
  42. const discountCoupon2 = {
  43. ...coupon,
  44. id: 2,
  45. discount: 90,
  46. denominations: 0,
  47. originCondition: 50,
  48. value: 12,
  49. description: ''
  50. };
  51. const disabledCoupon = {
  52. ...coupon,
  53. id: 3,
  54. reason: 'reason'
  55. };
  56. const disabledDiscountCoupon = {
  57. ...discountCoupon,
  58. discount: 10,
  59. id: 4,
  60. reason: ''
  61. };
  62. test('render disabled coupon', () => {
  63. const wrapper = mount(Coupon, {
  64. propsData: {
  65. coupon: disabledCoupon,
  66. disabled: true
  67. }
  68. });
  69. expect(wrapper).toMatchSnapshot();
  70. });
  71. test('render coupon list', async () => {
  72. const wrapper = mount(CouponList, {
  73. propsData: {
  74. chosenCoupon: 1,
  75. coupons: [emptyCoupon, coupon, coupon2, coupon3, discountCoupon, discountCoupon2],
  76. disabledCoupons: [disabledCoupon, disabledDiscountCoupon]
  77. }
  78. });
  79. await later();
  80. expect(wrapper).toMatchSnapshot();
  81. });
  82. test('render empty coupon list', () => {
  83. const wrapper = mount(CouponList, {
  84. propsData: {
  85. coupons: [],
  86. disabledCoupons: []
  87. }
  88. });
  89. wrapper.findAll('.van-tab').at(1).trigger('click');
  90. expect(wrapper).toMatchSnapshot();
  91. });
  92. test('exchange coupon', () => {
  93. const wrapper = mount(CouponList);
  94. const exchange = wrapper.find('.van-coupon-list__exchange');
  95. wrapper.setData({
  96. currentCode: '1',
  97. displayedCouponIndex: 1
  98. });
  99. exchange.trigger('click');
  100. wrapper.setProps({ code: '2' });
  101. exchange.trigger('click');
  102. expect(wrapper.emitted('exchange')[0][0]).toBe('1');
  103. expect(wrapper.emitted('exchange')[1][0]).toBe('2');
  104. expect(wrapper.emitted('input')[0][0]).toBe('1');
  105. expect(wrapper.emitted('input')[1][0]).toBe('');
  106. expect(wrapper.emitted('input')[2][0]).toBe('2');
  107. });
  108. test('render coupon cell', () => {
  109. const onClick = jest.fn();
  110. const wrapper = mount(CouponCell, {
  111. context: {
  112. on: {
  113. click: onClick
  114. }
  115. }
  116. });
  117. expect(wrapper).toMatchSnapshot();
  118. wrapper.trigger('click');
  119. expect(onClick).toHaveBeenCalledTimes(1);
  120. });
  121. test('render coupon cell with coupon', () => {
  122. const wrapper = mount(CouponCell, {
  123. propsData: {
  124. coupons: [{ value: 100 }],
  125. chosenCoupon: 0
  126. }
  127. });
  128. expect(wrapper).toMatchSnapshot();
  129. });