Welcome to mirror list, hosted at ThFree Co, Russian Federation.

table_registry_spec.js « components « registry « javascripts « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c7439206ef197d04d47ba37f593905787d869af (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import Vue from 'vue';
import tableRegistry from '~/registry/components/table_registry.vue';
import store from '~/registry/stores';
import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import { repoPropsData } from '../mock_data';

const [firstImage, secondImage] = repoPropsData.list;

describe('table registry', () => {
  let vm;
  const Component = Vue.extend(tableRegistry);
  const bulkDeletePath = 'path';

  const findDeleteBtn = () => vm.$el.querySelector('.js-delete-registry');
  const findDeleteBtnRow = () => vm.$el.querySelector('.js-delete-registry-row');
  const findSelectAllCheckbox = () => vm.$el.querySelector('.js-select-all-checkbox > input');
  const findAllRowCheckboxes = () =>
    Array.from(vm.$el.querySelectorAll('.js-select-checkbox input'));
  const confirmationModal = (child = '') => document.querySelector(`#${vm.modalId} ${child}`);

  const createComponent = () => {
    vm = mountComponentWithStore(Component, {
      store,
      props: {
        repo: repoPropsData,
      },
    });
  };

  const selectAllCheckboxes = () => vm.selectAll();
  const deselectAllCheckboxes = () => vm.deselectAll();

  beforeEach(() => {
    createComponent();
  });

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

  describe('rendering', () => {
    it('should render a table with the registry list', () => {
      expect(vm.$el.querySelectorAll('table tbody tr').length).toEqual(repoPropsData.list.length);
    });

    it('should render registry tag', () => {
      const textRendered = vm.$el
        .querySelector('.table tbody tr')
        .textContent.trim()
        // replace additional whitespace characters (e.g. new lines) with a single empty space
        .replace(/\s\s+/g, ' ');

      expect(textRendered).toContain(repoPropsData.list[0].tag);
      expect(textRendered).toContain(repoPropsData.list[0].shortRevision);
      expect(textRendered).toContain(repoPropsData.list[0].layers);
      expect(textRendered).toContain(repoPropsData.list[0].size);
    });
  });

  describe('multi select', () => {
    it('should support multiselect and selecting a row should enable delete button', done => {
      findSelectAllCheckbox().click();
      selectAllCheckboxes();

      expect(findSelectAllCheckbox().checked).toBe(true);

      Vue.nextTick(() => {
        expect(findDeleteBtn().disabled).toBe(false);
        done();
      });
    });

    it('selecting all checkbox should select all rows and enable delete button', done => {
      selectAllCheckboxes();

      Vue.nextTick(() => {
        const checkedValues = findAllRowCheckboxes().filter(x => x.checked);

        expect(checkedValues.length).toBe(repoPropsData.list.length);
        done();
      });
    });

    it('deselecting select all checkbox should deselect all rows and disable delete button', done => {
      selectAllCheckboxes();
      deselectAllCheckboxes();

      Vue.nextTick(() => {
        const checkedValues = findAllRowCheckboxes().filter(x => x.checked);

        expect(checkedValues.length).toBe(0);
        done();
      });
    });

    it('should delete multiple items when multiple items are selected', done => {
      selectAllCheckboxes();

      Vue.nextTick(() => {
        expect(vm.itemsToBeDeleted).toEqual([0, 1]);
        expect(findDeleteBtn().disabled).toBe(false);

        findDeleteBtn().click();
        spyOn(vm, 'multiDeleteItems').and.returnValue(Promise.resolve());

        Vue.nextTick(() => {
          const modal = confirmationModal();
          confirmationModal('.btn-danger').click();

          expect(modal).toExist();

          Vue.nextTick(() => {
            expect(vm.itemsToBeDeleted).toEqual([]);
            expect(vm.multiDeleteItems).toHaveBeenCalledWith({
              path: bulkDeletePath,
              items: [firstImage.tag, secondImage.tag],
            });
            done();
          });
        });
      });
    });
  });

  describe('delete registry', () => {
    beforeEach(() => {
      vm.itemsToBeDeleted = [0];
    });

    it('should be possible to delete a registry', done => {
      Vue.nextTick(() => {
        expect(vm.itemsToBeDeleted).toEqual([0]);
        expect(findDeleteBtn()).toBeDefined();
        expect(findDeleteBtn().disabled).toBe(false);
        expect(findDeleteBtnRow()).toBeDefined();
        done();
      });
    });

    it('should call deleteItems and reset itemsToBeDeleted when confirming deletion', done => {
      Vue.nextTick(() => {
        expect(vm.itemsToBeDeleted).toEqual([0]);
        expect(findDeleteBtn().disabled).toBe(false);
        findDeleteBtn().click();
        spyOn(vm, 'multiDeleteItems').and.returnValue(Promise.resolve());

        Vue.nextTick(() => {
          confirmationModal('.btn-danger').click();

          expect(vm.itemsToBeDeleted).toEqual([]);
          expect(vm.multiDeleteItems).toHaveBeenCalledWith({
            path: bulkDeletePath,
            items: [firstImage.tag],
          });
          done();
        });
      });
    });
  });

  describe('pagination', () => {
    it('should be possible to change the page', () => {
      expect(vm.$el.querySelector('.gl-pagination')).toBeDefined();
    });
  });

  describe('modal content', () => {
    it('should show the singular title and image name when deleting a single image', done => {
      findDeleteBtnRow().click();

      Vue.nextTick(() => {
        expect(vm.modalTitle).toBe('Remove image');
        expect(vm.modalDescription).toContain(firstImage.tag);
        done();
      });
    });

    it('should show the plural title and image count when deleting more than one image', done => {
      selectAllCheckboxes();
      vm.setModalDescription();

      Vue.nextTick(() => {
        expect(vm.modalTitle).toBe('Remove images');
        expect(vm.modalDescription).toContain('<b>2</b> images');
        done();
      });
    });
  });
});