index.spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import Tab from '..';
  2. import Tabs from '../../tabs';
  3. import { mount, later, triggerDrag } from '../../../test/utils';
  4. function createWrapper(options = {}) {
  5. return mount({
  6. template: `
  7. <tabs
  8. :color="color"
  9. :type="type"
  10. :sticky="sticky"
  11. :swipeable="swipeable"
  12. :line-width="lineWidth"
  13. :lazy-render="lazyRender"
  14. @change="onChange"
  15. >
  16. ${options.extraTemplate || ''}
  17. <tab :title="title1">Text</tab>
  18. <tab>
  19. <span slot="title">title2</span>
  20. Text
  21. </tab>
  22. <tab title="title3" disabled>Text</tab>
  23. </tabs>
  24. `,
  25. components: {
  26. Tab,
  27. Tabs
  28. },
  29. data() {
  30. return {
  31. title1: 'title1',
  32. color: '#f44',
  33. type: 'line',
  34. swipeable: true,
  35. sticky: true,
  36. lineWidth: 2,
  37. lazyRender: true
  38. };
  39. },
  40. ...options
  41. });
  42. }
  43. test('click to switch tab', async () => {
  44. const onChange = jest.fn();
  45. const wrapper = createWrapper({
  46. methods: {
  47. onChange
  48. }
  49. });
  50. await later();
  51. expect(wrapper).toMatchSnapshot();
  52. const tabs = wrapper.findAll('.van-tab');
  53. tabs.at(1).trigger('click');
  54. tabs.at(2).trigger('click');
  55. await later();
  56. expect(wrapper).toMatchSnapshot();
  57. expect(onChange).toHaveBeenCalledTimes(1);
  58. });
  59. test('swipe to switch tab', async () => {
  60. const onChange = jest.fn();
  61. const wrapper = createWrapper({
  62. methods: {
  63. onChange
  64. }
  65. });
  66. const content = wrapper.find('.van-tabs__content');
  67. await later();
  68. expect(wrapper).toMatchSnapshot();
  69. triggerDrag(content, -100, 0);
  70. expect(wrapper).toMatchSnapshot();
  71. expect(onChange).toHaveBeenCalledTimes(1);
  72. triggerDrag(content, -100, 0);
  73. expect(wrapper).toMatchSnapshot();
  74. triggerDrag(content, 100, 0);
  75. triggerDrag(content, 100, 0);
  76. expect(wrapper).toMatchSnapshot();
  77. await later();
  78. wrapper.destroy();
  79. });
  80. test('change tabs data', async () => {
  81. const wrapper = createWrapper();
  82. expect(wrapper).toMatchSnapshot();
  83. wrapper.setData({
  84. swipeable: false,
  85. sticky: false,
  86. type: 'card',
  87. color: 'blue',
  88. title1: 'new title1'
  89. });
  90. await later();
  91. expect(wrapper).toMatchSnapshot();
  92. });
  93. test('lazy render', async () => {
  94. const wrapper = createWrapper();
  95. expect(wrapper).toMatchSnapshot();
  96. wrapper.setData({
  97. lazyRender: false
  98. });
  99. await later();
  100. expect(wrapper).toMatchSnapshot();
  101. });
  102. test('render nav-left & nav-right slot', async () => {
  103. const wrapper = createWrapper({
  104. extraTemplate: `
  105. <template v-slot:nav-left>Nav Left</template>
  106. <template v-slot:nav-right>Nav Right</template>
  107. `
  108. });
  109. expect(wrapper).toMatchSnapshot();
  110. });