index.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import Vue from 'vue';
  2. import ImagePreview from '..';
  3. import ImagePreviewVue from '../ImagePreview';
  4. import { mount, trigger, triggerDrag, transitionStub } from '../../../test/utils';
  5. transitionStub();
  6. function triggerZoom(el, x, y) {
  7. trigger(el, 'touchstart', 0, 0, { x, y });
  8. trigger(el, 'touchmove', -x / 4, -y / 4, { x, y });
  9. trigger(el, 'touchmove', -x / 3, -y / 3, { x, y });
  10. trigger(el, 'touchmove', -x / 2, -y / 2, { x, y });
  11. trigger(el, 'touchmove', -x, -y, { x, y });
  12. trigger(el, 'touchend', 0, 0, { touchList: [] });
  13. }
  14. const images = [
  15. 'https://img.yzcdn.cn/1.png',
  16. 'https://img.yzcdn.cn/2.png',
  17. 'https://img.yzcdn.cn/3.png'
  18. ];
  19. test('render image', () => {
  20. const wrapper = mount(ImagePreviewVue, {
  21. propsData: { images, value: true }
  22. });
  23. expect(wrapper).toMatchSnapshot();
  24. const swipe = wrapper.find('.van-swipe__track');
  25. triggerDrag(swipe, 500, 0);
  26. expect(wrapper.emitted('input')).toBeFalsy();
  27. triggerDrag(swipe, 0, 0);
  28. expect(wrapper.emitted('input')[0][0]).toBeFalsy();
  29. expect(wrapper.emitted('change')[0][0]).toEqual(2);
  30. });
  31. test('async close', () => {
  32. const wrapper = mount(ImagePreviewVue, {
  33. propsData: {
  34. images,
  35. value: true,
  36. asyncClose: true
  37. }
  38. });
  39. const swipe = wrapper.find('.van-swipe__track');
  40. triggerDrag(swipe, 0, 0);
  41. expect(wrapper.emitted('input')).toBeFalsy();
  42. wrapper.vm.close();
  43. expect(wrapper.emitted('input')[0][0]).toBeFalsy();
  44. });
  45. test('function call', done => {
  46. ImagePreview(images);
  47. ImagePreview(images.slice(0, 1));
  48. Vue.nextTick(() => {
  49. const wrapper = document.querySelector('.van-image-preview');
  50. const swipe = wrapper.querySelector('.van-swipe__track');
  51. triggerDrag(swipe, 0, 0);
  52. expect(wrapper.querySelectorAll('img').length).toEqual(1);
  53. done();
  54. });
  55. });
  56. test('function call options', done => {
  57. const onClose = jest.fn();
  58. const instance = ImagePreview({
  59. images,
  60. startPostion: 1,
  61. onClose
  62. });
  63. instance.$emit('input', true);
  64. expect(onClose).toHaveBeenCalledTimes(0);
  65. Vue.nextTick(() => {
  66. const wrapper = document.querySelector('.van-image-preview');
  67. const swipe = wrapper.querySelector('.van-swipe__track');
  68. triggerDrag(swipe, 0, 0);
  69. expect(onClose).toHaveBeenCalledTimes(1);
  70. expect(onClose).toHaveBeenCalledWith({ index: 0, url: 'https://img.yzcdn.cn/1.png' });
  71. done();
  72. });
  73. });
  74. test('register component', () => {
  75. Vue.use(ImagePreview);
  76. expect(Vue.component(ImagePreviewVue.name)).toBeTruthy();
  77. });
  78. test('zoom', async () => {
  79. const { getBoundingClientRect } = Element.prototype;
  80. Element.prototype.getBoundingClientRect = jest.fn(() => ({ width: 100 }));
  81. const wrapper = mount(ImagePreviewVue, {
  82. propsData: { images, value: true }
  83. });
  84. const image = wrapper.find('img');
  85. triggerZoom(image, 300, 300);
  86. triggerDrag(image, 300, 300);
  87. expect(wrapper).toMatchSnapshot();
  88. Element.prototype.getBoundingClientRect = getBoundingClientRect;
  89. });
  90. test('index slot', () => {
  91. const wrapper = mount({
  92. template: `
  93. <van-image-preview :value="true">
  94. <template v-slot:index>Custom Index</template>
  95. </van-image-preview>
  96. `
  97. });
  98. expect(wrapper).toMatchSnapshot();
  99. });