summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/smart_virtual_list_spec.js
blob: 8802a832781e3ec30af10cb021fef61d962c0d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { mount } from '@vue/test-utils';
import Vue from 'vue';
import SmartVirtualScrollList from '~/vue_shared/components/smart_virtual_list.vue';

describe('Toggle Button', () => {
  let vm;

  const createComponent = ({ length, remain }) => {
    const smartListProperties = {
      rtag: 'section',
      wtag: 'ul',
      wclass: 'test-class',
      // Size in pixels does not matter for our tests here
      size: 35,
      length,
      remain,
    };

    const Component = Vue.extend({
      components: {
        SmartVirtualScrollList,
      },
      smartListProperties,
      items: Array(length).fill(1),
      template: `
      <smart-virtual-scroll-list v-bind="$options.smartListProperties">
        <li v-for="(val, key) in $options.items" :key="key">{{ key + 1 }}</li>
      </smart-virtual-scroll-list>`,
    });

    return mount(Component).vm;
  };

  afterEach(() => {
    vm.$destroy();
  });

  describe('if the list is shorter than the maximum shown elements', () => {
    const listLength = 10;

    beforeEach(() => {
      vm = createComponent({ length: listLength, remain: 20 });
    });

    it('renders without the vue-virtual-scroll-list component', () => {
      expect(vm.$el.classList).not.toContain('js-virtual-list');
      expect(vm.$el.classList).toContain('js-plain-element');
    });

    it('renders list with provided tags and classes for the wrapper elements', () => {
      expect(vm.$el.tagName).toEqual('SECTION');
      expect(vm.$el.firstChild.tagName).toEqual('UL');
      expect(vm.$el.firstChild.classList).toContain('test-class');
    });

    it('renders all children list elements', () => {
      expect(vm.$el.querySelectorAll('li').length).toEqual(listLength);
    });
  });

  describe('if the list is longer than the maximum shown elements', () => {
    const maxItemsShown = 20;

    beforeEach(() => {
      vm = createComponent({ length: 1000, remain: maxItemsShown });
    });

    it('uses the vue-virtual-scroll-list component', () => {
      expect(vm.$el.classList).toContain('js-virtual-list');
      expect(vm.$el.classList).not.toContain('js-plain-element');
    });

    it('renders list with provided tags and classes for the wrapper elements', () => {
      expect(vm.$el.tagName).toEqual('SECTION');
      expect(vm.$el.firstChild.tagName).toEqual('UL');
      expect(vm.$el.firstChild.classList).toContain('test-class');
    });

    it('renders at max twice the maximum shown elements', () => {
      expect(vm.$el.querySelectorAll('li').length).toBeLessThanOrEqual(2 * maxItemsShown);
    });
  });
});