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

job_artifacts_table_spec.js « components « artifacts « ci « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1cbb1a714c950697463228202db7536cf4148ec2 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
import {
  GlLoadingIcon,
  GlTable,
  GlLink,
  GlBadge,
  GlPagination,
  GlModal,
  GlFormCheckbox,
} from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import getJobArtifactsResponse from 'test_fixtures/graphql/ci/artifacts/graphql/queries/get_job_artifacts.query.graphql.json';
import CiIcon from '~/vue_shared/components/ci_icon.vue';
import waitForPromises from 'helpers/wait_for_promises';
import JobArtifactsTable from '~/ci/artifacts/components/job_artifacts_table.vue';
import ArtifactsTableRowDetails from '~/ci/artifacts/components/artifacts_table_row_details.vue';
import ArtifactDeleteModal from '~/ci/artifacts/components/artifact_delete_modal.vue';
import ArtifactsBulkDelete from '~/ci/artifacts/components/artifacts_bulk_delete.vue';
import BulkDeleteModal from '~/ci/artifacts/components/bulk_delete_modal.vue';
import JobCheckbox from '~/ci/artifacts/components/job_checkbox.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import getJobArtifactsQuery from '~/ci/artifacts/graphql/queries/get_job_artifacts.query.graphql';
import bulkDestroyArtifactsMutation from '~/ci/artifacts/graphql/mutations/bulk_destroy_job_artifacts.mutation.graphql';
import { getIdFromGraphQLId, convertToGraphQLId } from '~/graphql_shared/utils';
import { TYPENAME_PROJECT } from '~/graphql_shared/constants';
import {
  ARCHIVE_FILE_TYPE,
  JOBS_PER_PAGE,
  I18N_FETCH_ERROR,
  INITIAL_CURRENT_PAGE,
  I18N_BULK_DELETE_ERROR,
  SELECTED_ARTIFACTS_MAX_COUNT,
} from '~/ci/artifacts/constants';
import { totalArtifactsSizeForJob } from '~/ci/artifacts/utils';
import { createAlert } from '~/alert';

jest.mock('~/alert');

Vue.use(VueApollo);

describe('JobArtifactsTable component', () => {
  let wrapper;
  let requestHandlers;

  const mockToastShow = jest.fn();

  const findLoadingState = () => wrapper.findComponent(GlLoadingIcon);
  const findTable = () => wrapper.findComponent(GlTable);
  const findDetailsRows = () => wrapper.findAllComponents(ArtifactsTableRowDetails);
  const findDetailsInRow = (i) =>
    findTable().findAll('tbody tr').at(i).findComponent(ArtifactsTableRowDetails);

  const findCount = () => wrapper.findByTestId('job-artifacts-count');
  const findCountAt = (i) => wrapper.findAllByTestId('job-artifacts-count').at(i);

  const findDeleteModal = () => wrapper.findComponent(ArtifactDeleteModal);
  const findBulkDeleteModal = () => wrapper.findComponent(BulkDeleteModal);

  const findStatuses = () => wrapper.findAllByTestId('job-artifacts-job-status');
  const findSuccessfulJobStatus = () => findStatuses().at(0);
  const findFailedJobStatus = () => findStatuses().at(1);

  const findLinks = () => wrapper.findAllComponents(GlLink);
  const findJobLink = () => findLinks().at(0);
  const findPipelineLink = () => findLinks().at(1);
  const findRefLink = () => findLinks().at(2);
  const findCommitLink = () => findLinks().at(3);

  const findSize = () => wrapper.findByTestId('job-artifacts-size');
  const findCreated = () => wrapper.findByTestId('job-artifacts-created');

  const findDownloadButton = () => wrapper.findByTestId('job-artifacts-download-button');
  const findBrowseButton = () => wrapper.findByTestId('job-artifacts-browse-button');
  const findDeleteButton = () => wrapper.findByTestId('job-artifacts-delete-button');
  const findArtifactDeleteButton = () => wrapper.findByTestId('job-artifact-row-delete-button');

  // first checkbox is the "select all" checkbox in the table header
  const findSelectAllCheckbox = () => wrapper.findComponent(GlFormCheckbox);
  const findSelectAllCheckboxChecked = () => findSelectAllCheckbox().find('input').element.checked;
  const findSelectAllCheckboxIndeterminate = () =>
    findSelectAllCheckbox().find('input').element.indeterminate;
  const findSelectAllCheckboxDisabled = () =>
    findSelectAllCheckbox().find('input').element.disabled;
  const toggleSelectAllCheckbox = () =>
    findSelectAllCheckbox().vm.$emit('change', !findSelectAllCheckboxChecked());

  // first checkbox is a "select all", this finder should get the first job checkbox
  const findJobCheckbox = (i = 1) => wrapper.findAllComponents(GlFormCheckbox).at(i);
  const findAnyCheckbox = () => wrapper.findComponent(GlFormCheckbox);
  const findBulkDelete = () => wrapper.findComponent(ArtifactsBulkDelete);
  const findBulkDeleteContainer = () => wrapper.findByTestId('bulk-delete-container');

  const findPagination = () => wrapper.findComponent(GlPagination);
  const setPage = async (page) => {
    findPagination().vm.$emit('input', page);
    await waitForPromises();
  };

  const projectId = 'some/project/id';

  let enoughJobsToPaginate = [...getJobArtifactsResponse.data.project.jobs.nodes];
  while (enoughJobsToPaginate.length <= JOBS_PER_PAGE) {
    enoughJobsToPaginate = [
      ...enoughJobsToPaginate,
      ...getJobArtifactsResponse.data.project.jobs.nodes,
    ];
  }
  const getJobArtifactsResponseThatPaginates = {
    data: {
      project: {
        jobs: {
          nodes: enoughJobsToPaginate,
          pageInfo: { ...getJobArtifactsResponse.data.project.jobs.pageInfo, hasNextPage: true },
        },
      },
    },
  };

  const job = getJobArtifactsResponse.data.project.jobs.nodes[0];
  const archiveArtifact = job.artifacts.nodes.find(
    (artifact) => artifact.fileType === ARCHIVE_FILE_TYPE,
  );
  const job2 = getJobArtifactsResponse.data.project.jobs.nodes[1];

  const destroyedCount = job.artifacts.nodes.length;
  const destroyedIds = job.artifacts.nodes.map((node) => node.id);
  const bulkDestroyMutationHandler = jest.fn().mockResolvedValue({
    data: {
      bulkDestroyJobArtifacts: { errors: [], destroyedCount, destroyedIds },
    },
  });

  const allArtifacts = getJobArtifactsResponse.data.project.jobs.nodes
    .map((jobNode) => jobNode.artifacts.nodes.map((artifactNode) => artifactNode.id))
    .reduce((artifacts, jobArtifacts) => artifacts.concat(jobArtifacts));

  const maxSelectedArtifacts = new Array(SELECTED_ARTIFACTS_MAX_COUNT).fill('artifact-id');
  const maxSelectedArtifactsIncludingCurrentPage = [
    ...allArtifacts,
    ...new Array(SELECTED_ARTIFACTS_MAX_COUNT - allArtifacts.length).fill('artifact-id'),
  ];

  const createComponent = ({
    handlers = {
      getJobArtifactsQuery: jest.fn().mockResolvedValue(getJobArtifactsResponse),
      bulkDestroyArtifactsMutation: bulkDestroyMutationHandler,
    },
    data = {},
    canDestroyArtifacts = true,
  } = {}) => {
    requestHandlers = handlers;
    wrapper = mountExtended(JobArtifactsTable, {
      apolloProvider: createMockApollo([
        [getJobArtifactsQuery, requestHandlers.getJobArtifactsQuery],
        [bulkDestroyArtifactsMutation, requestHandlers.bulkDestroyArtifactsMutation],
      ]),
      provide: {
        projectPath: 'project/path',
        projectId,
        canDestroyArtifacts,
      },
      mocks: {
        $toast: {
          show: mockToastShow,
        },
      },
      data() {
        return data;
      },
    });
  };

  it('when loading, shows a loading state', () => {
    createComponent();

    expect(findLoadingState().exists()).toBe(true);
  });

  it('on error, shows an alert', async () => {
    createComponent({
      handlers: {
        getJobArtifactsQuery: jest.fn().mockRejectedValue(new Error('Error!')),
      },
    });

    await waitForPromises();

    expect(createAlert).toHaveBeenCalledWith({ message: I18N_FETCH_ERROR });
  });

  it('with data, renders the table', async () => {
    createComponent();

    await waitForPromises();

    expect(findTable().exists()).toBe(true);
  });

  describe('job details', () => {
    beforeEach(async () => {
      createComponent();

      await waitForPromises();
    });

    it('shows the artifact count', () => {
      expect(findCount().text()).toBe(`${job.artifacts.nodes.length} files`);
    });

    it('shows the job status as an icon for a successful job', () => {
      expect(findSuccessfulJobStatus().findComponent(CiIcon).exists()).toBe(true);
      expect(findSuccessfulJobStatus().findComponent(GlBadge).exists()).toBe(false);
    });

    it('shows the job status as a badge for other job statuses', () => {
      expect(findFailedJobStatus().findComponent(GlBadge).exists()).toBe(true);
      expect(findFailedJobStatus().findComponent(CiIcon).exists()).toBe(false);
    });

    it('shows links to the job, pipeline, ref, and commit', () => {
      expect(findJobLink().text()).toBe(job.name);
      expect(findJobLink().attributes('href')).toBe(job.webPath);

      expect(findPipelineLink().text()).toBe(`#${getIdFromGraphQLId(job.pipeline.id)}`);
      expect(findPipelineLink().attributes('href')).toBe(job.pipeline.path);

      expect(findRefLink().text()).toBe(job.refName);
      expect(findRefLink().attributes('href')).toBe(job.refPath);

      expect(findCommitLink().text()).toBe(job.shortSha);
      expect(findCommitLink().attributes('href')).toBe(job.commitPath);
    });

    it('shows the total size of artifacts', () => {
      expect(findSize().text()).toBe(totalArtifactsSizeForJob(job));
    });

    it('shows the created time', () => {
      expect(findCreated().text()).toBe('5 years ago');
    });

    describe('row expansion', () => {
      it('toggles the visibility of the row details', async () => {
        expect(findDetailsRows().length).toBe(0);

        findCount().trigger('click');
        await nextTick();

        expect(findDetailsRows().length).toBe(1);

        findCount().trigger('click');
        await nextTick();

        expect(findDetailsRows().length).toBe(0);
      });

      it('expands and collapses jobs', async () => {
        // both jobs start collapsed
        expect(findDetailsInRow(0).exists()).toBe(false);
        expect(findDetailsInRow(1).exists()).toBe(false);

        findCountAt(0).trigger('click');
        await nextTick();

        // first job is expanded, second row has its details
        expect(findDetailsInRow(0).exists()).toBe(false);
        expect(findDetailsInRow(1).exists()).toBe(true);
        expect(findDetailsInRow(2).exists()).toBe(false);

        findCountAt(1).trigger('click');
        await nextTick();

        // both jobs are expanded, each has details below it
        expect(findDetailsInRow(0).exists()).toBe(false);
        expect(findDetailsInRow(1).exists()).toBe(true);
        expect(findDetailsInRow(2).exists()).toBe(false);
        expect(findDetailsInRow(3).exists()).toBe(true);

        findCountAt(0).trigger('click');
        await nextTick();

        // first job collapsed, second job expanded
        expect(findDetailsInRow(0).exists()).toBe(false);
        expect(findDetailsInRow(1).exists()).toBe(false);
        expect(findDetailsInRow(2).exists()).toBe(true);
      });

      it('keeps the job expanded when an artifact is deleted', async () => {
        findCount().trigger('click');
        await waitForPromises();

        expect(findDetailsInRow(0).exists()).toBe(false);
        expect(findDetailsInRow(1).exists()).toBe(true);

        findArtifactDeleteButton().vm.$emit('click');
        await nextTick();

        expect(findDeleteModal().findComponent(GlModal).props('visible')).toBe(true);

        findDeleteModal().vm.$emit('primary');
        await waitForPromises();

        expect(findDetailsInRow(0).exists()).toBe(false);
        expect(findDetailsInRow(1).exists()).toBe(true);
      });
    });
  });

  describe('download button', () => {
    it('is a link to the download path for the archive artifact', async () => {
      createComponent();

      await waitForPromises();

      expect(findDownloadButton().attributes('href')).toBe(archiveArtifact.downloadPath);
    });

    it('is disabled when there is no download path', async () => {
      const jobWithoutDownloadPath = {
        ...job,
        hasArtifacts: true,
        archive: { downloadPath: null },
      };

      createComponent({
        handlers: { getJobArtifactsQuery: jest.fn() },
        data: { jobArtifacts: [jobWithoutDownloadPath] },
      });

      await waitForPromises();

      expect(findDownloadButton().attributes('disabled')).toBeDefined();
    });
  });

  describe('browse button', () => {
    it('is a link to the browse path for the job', async () => {
      createComponent();

      await waitForPromises();

      expect(findBrowseButton().attributes('href')).toBe(job.browseArtifactsPath);
    });

    it('is disabled when there is no browse path', async () => {
      const jobWithoutBrowsePath = {
        ...job,
        hasArtifacts: true,
        browseArtifactsPath: null,
      };

      createComponent({
        handlers: { getJobArtifactsQuery: jest.fn() },
        data: { jobArtifacts: [jobWithoutBrowsePath] },
      });

      await waitForPromises();

      expect(findBrowseButton().attributes('disabled')).toBeDefined();
    });

    it('is disabled when job has no metadata.gz', async () => {
      const jobWithoutMetadata = {
        ...job,
        hasArtifacts: true,
        artifacts: { nodes: [archiveArtifact] },
      };

      createComponent({
        handlers: { getJobArtifactsQuery: jest.fn() },
        data: { jobArtifacts: [jobWithoutMetadata] },
      });

      await waitForPromises();

      expect(findBrowseButton().attributes('disabled')).toBe('disabled');
    });

    it('is disabled when job has no artifacts', async () => {
      const jobWithoutArtifacts = {
        ...job,
        hasArtifacts: false,
        artifacts: { nodes: [] },
      };

      createComponent({
        handlers: { getJobArtifactsQuery: jest.fn() },
        data: { jobArtifacts: [jobWithoutArtifacts] },
      });

      await waitForPromises();

      expect(findBrowseButton().attributes('disabled')).toBe('disabled');
    });
  });

  describe('delete button', () => {
    const artifactsFromJob = job.artifacts.nodes.map((node) => node.id);

    beforeEach(async () => {
      createComponent({
        canDestroyArtifacts: true,
      });

      await waitForPromises();
    });

    it('opens the confirmation modal with the artifacts from the job', async () => {
      await findDeleteButton().vm.$emit('click');

      expect(findBulkDeleteModal().props()).toMatchObject({
        visible: true,
        artifactsToDelete: artifactsFromJob,
      });
    });

    it('on confirm, deletes the artifacts from the job and shows a toast', async () => {
      findDeleteButton().vm.$emit('click');
      findBulkDeleteModal().vm.$emit('primary');

      expect(bulkDestroyMutationHandler).toHaveBeenCalledWith({
        projectId: convertToGraphQLId(TYPENAME_PROJECT, projectId),
        ids: artifactsFromJob,
      });

      await waitForPromises();

      expect(mockToastShow).toHaveBeenCalledWith(
        `${artifactsFromJob.length} selected artifacts deleted`,
      );
    });

    it('does not clear selected artifacts on success', async () => {
      // select job 2 via checkbox
      findJobCheckbox(2).vm.$emit('change', true);

      // click delete button job 1
      findDeleteButton().vm.$emit('click');

      // job 2's artifacts should still be selected
      expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual(
        job2.artifacts.nodes.map((node) => node.id),
      );

      // confirm delete
      findBulkDeleteModal().vm.$emit('primary');

      // job 1's artifacts should be deleted
      expect(bulkDestroyMutationHandler).toHaveBeenCalledWith({
        projectId: convertToGraphQLId(TYPENAME_PROJECT, projectId),
        ids: artifactsFromJob,
      });

      await waitForPromises();

      // job 2's artifacts should still be selected
      expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual(
        job2.artifacts.nodes.map((node) => node.id),
      );
    });

    it('shows an alert and does not clear selected artifacts on error', async () => {
      createComponent({
        canDestroyArtifacts: true,
        handlers: {
          getJobArtifactsQuery: jest.fn().mockResolvedValue(getJobArtifactsResponse),
          bulkDestroyArtifactsMutation: jest.fn().mockRejectedValue(),
        },
      });
      await waitForPromises();

      // select job 2 via checkbox
      findJobCheckbox(2).vm.$emit('change', true);

      // click delete button job 1
      findDeleteButton().vm.$emit('click');

      // confirm delete
      findBulkDeleteModal().vm.$emit('primary');

      await waitForPromises();

      // job 2's artifacts should still be selected
      expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual(
        job2.artifacts.nodes.map((node) => node.id),
      );
      expect(createAlert).toHaveBeenCalledWith({
        captureError: true,
        error: expect.any(Error),
        message: I18N_BULK_DELETE_ERROR,
      });
    });

    it('is hidden when user does not have delete permission', async () => {
      createComponent({
        canDestroyArtifacts: false,
      });

      await waitForPromises();

      expect(findDeleteButton().exists()).toBe(false);
    });
  });

  describe('bulk delete', () => {
    const selectedArtifacts = job.artifacts.nodes.map((node) => node.id);

    beforeEach(async () => {
      createComponent({
        canDestroyArtifacts: true,
      });

      await waitForPromises();
    });

    it('shows selected artifacts when a job is checked', async () => {
      expect(findBulkDeleteContainer().exists()).toBe(false);

      await findJobCheckbox().vm.$emit('change', true);

      expect(findBulkDeleteContainer().exists()).toBe(true);
      expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual(selectedArtifacts);
    });

    it('disappears when selected artifacts are cleared', async () => {
      await findJobCheckbox().vm.$emit('change', true);

      expect(findBulkDeleteContainer().exists()).toBe(true);

      await findBulkDelete().vm.$emit('clearSelectedArtifacts');

      expect(findBulkDeleteContainer().exists()).toBe(false);
    });

    it('shows a modal to confirm bulk delete', async () => {
      findJobCheckbox().vm.$emit('change', true);
      findBulkDelete().vm.$emit('showBulkDeleteModal');

      await nextTick();

      expect(findBulkDeleteModal().props('visible')).toBe(true);
    });

    it('deletes the selected artifacts and shows a toast', async () => {
      findJobCheckbox().vm.$emit('change', true);
      findBulkDelete().vm.$emit('showBulkDeleteModal');
      findBulkDeleteModal().vm.$emit('primary');

      expect(bulkDestroyMutationHandler).toHaveBeenCalledWith({
        projectId: convertToGraphQLId(TYPENAME_PROJECT, projectId),
        ids: selectedArtifacts,
      });

      await waitForPromises();

      expect(mockToastShow).toHaveBeenCalledWith(
        `${selectedArtifacts.length} selected artifacts deleted`,
      );
    });

    it('clears selected artifacts on success', async () => {
      findJobCheckbox().vm.$emit('change', true);
      findBulkDelete().vm.$emit('showBulkDeleteModal');
      findBulkDeleteModal().vm.$emit('primary');

      await waitForPromises();

      expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual([]);
    });

    describe('select all checkbox', () => {
      describe('when no artifacts are selected', () => {
        it('is not checked', () => {
          expect(findSelectAllCheckboxChecked()).toBe(false);
          expect(findSelectAllCheckboxIndeterminate()).toBe(false);
        });

        it('selects all artifacts when toggled', async () => {
          toggleSelectAllCheckbox();

          await nextTick();

          expect(findSelectAllCheckboxChecked()).toBe(true);
          expect(findSelectAllCheckboxIndeterminate()).toBe(false);
          expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual(allArtifacts);
        });
      });

      describe('when some artifacts are selected', () => {
        beforeEach(async () => {
          findJobCheckbox().vm.$emit('change', true);

          await nextTick();
        });

        it('is indeterminate', () => {
          expect(findSelectAllCheckboxChecked()).toBe(true);
          expect(findSelectAllCheckboxIndeterminate()).toBe(true);
        });

        it('deselects all artifacts when toggled', async () => {
          toggleSelectAllCheckbox();

          await nextTick();

          expect(findSelectAllCheckboxChecked()).toBe(false);
          expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual([]);
        });
      });

      describe('when all artifacts are selected', () => {
        beforeEach(async () => {
          findJobCheckbox(1).vm.$emit('change', true);
          findJobCheckbox(2).vm.$emit('change', true);

          await nextTick();
        });

        it('is checked', () => {
          expect(findSelectAllCheckboxChecked()).toBe(true);
          expect(findSelectAllCheckboxIndeterminate()).toBe(false);
        });

        it('deselects all artifacts when toggled', async () => {
          toggleSelectAllCheckbox();

          await nextTick();

          expect(findSelectAllCheckboxChecked()).toBe(false);
          expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual([]);
        });
      });

      describe('when an artifact is selected on another page', () => {
        const otherPageArtifact = { id: 'gid://gitlab/Ci::JobArtifact/some/other/id' };

        beforeEach(async () => {
          // expand the first job row to access the details component
          findCount().trigger('click');

          await nextTick();

          // mock the selection of an artifact on another page by emitting a select event
          findDetailsInRow(1).vm.$emit('selectArtifact', otherPageArtifact, true);
        });

        it('is not checked even though an artifact is selected', () => {
          expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual([otherPageArtifact.id]);
          expect(findSelectAllCheckboxChecked()).toBe(false);
          expect(findSelectAllCheckboxIndeterminate()).toBe(false);
        });

        it('only toggles selection of visible artifacts, leaving the other artifact selected', async () => {
          toggleSelectAllCheckbox();

          await nextTick();

          expect(findSelectAllCheckboxChecked()).toBe(true);
          expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual([
            otherPageArtifact.id,
            ...allArtifacts,
          ]);

          toggleSelectAllCheckbox();

          await nextTick();

          expect(findSelectAllCheckboxChecked()).toBe(false);
          expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual([otherPageArtifact.id]);
        });
      });
    });

    describe('select all checkbox respects selected artifacts limit', () => {
      describe('when selecting all visible artifacts would exceed the limit', () => {
        const selectedArtifactsLength = SELECTED_ARTIFACTS_MAX_COUNT - 1;

        beforeEach(async () => {
          createComponent({
            canDestroyArtifacts: true,
            data: {
              selectedArtifacts: new Array(selectedArtifactsLength).fill('artifact-id'),
            },
          });

          await nextTick();
        });

        it('selects only up to the limit', async () => {
          expect(findSelectAllCheckboxChecked()).toBe(false);
          expect(findBulkDelete().props('selectedArtifacts')).toHaveLength(selectedArtifactsLength);

          toggleSelectAllCheckbox();

          await nextTick();

          expect(findSelectAllCheckboxChecked()).toBe(true);
          expect(findBulkDelete().props('selectedArtifacts')).toHaveLength(
            SELECTED_ARTIFACTS_MAX_COUNT,
          );
          expect(findBulkDelete().props('selectedArtifacts')).not.toContain(
            allArtifacts[allArtifacts.length - 1],
          );
        });
      });

      describe('when limit has been reached without artifacts on the current page', () => {
        beforeEach(async () => {
          createComponent({
            canDestroyArtifacts: true,
            data: { selectedArtifacts: maxSelectedArtifacts },
          });

          await nextTick();
        });

        it('passes isSelectedArtifactsLimitReached to bulk delete', () => {
          expect(findBulkDelete().props('isSelectedArtifactsLimitReached')).toBe(true);
        });

        it('passes isSelectedArtifactsLimitReached to job checkbox', () => {
          expect(wrapper.findComponent(JobCheckbox).props('isSelectedArtifactsLimitReached')).toBe(
            true,
          );
        });

        it('passes isSelectedArtifactsLimitReached to table row details', async () => {
          findCount().trigger('click');
          await nextTick();

          expect(findDetailsInRow(1).props('isSelectedArtifactsLimitReached')).toBe(true);
        });

        it('disables the select all checkbox', () => {
          expect(findSelectAllCheckboxDisabled()).toBe(true);
        });
      });

      describe('when limit has been reached including artifacts on the current page', () => {
        beforeEach(async () => {
          createComponent({
            canDestroyArtifacts: true,
            data: {
              selectedArtifacts: maxSelectedArtifactsIncludingCurrentPage,
            },
          });

          await nextTick();
        });

        describe('the select all checkbox', () => {
          it('is checked', () => {
            expect(findSelectAllCheckboxChecked()).toBe(true);
            expect(findSelectAllCheckboxIndeterminate()).toBe(false);
          });

          it('deselects all artifacts when toggled', async () => {
            expect(findBulkDelete().props('selectedArtifacts')).toHaveLength(
              SELECTED_ARTIFACTS_MAX_COUNT,
            );

            toggleSelectAllCheckbox();

            await nextTick();

            expect(findSelectAllCheckboxChecked()).toBe(false);
            expect(findBulkDelete().props('selectedArtifacts')).toHaveLength(
              SELECTED_ARTIFACTS_MAX_COUNT - allArtifacts.length,
            );
          });
        });
      });
    });

    it('shows an alert and does not clear selected artifacts on error', async () => {
      createComponent({
        canDestroyArtifacts: true,
        handlers: {
          getJobArtifactsQuery: jest.fn().mockResolvedValue(getJobArtifactsResponse),
          bulkDestroyArtifactsMutation: jest.fn().mockRejectedValue(),
        },
      });

      await waitForPromises();

      findJobCheckbox().vm.$emit('change', true);
      findBulkDelete().vm.$emit('showBulkDeleteModal');
      findBulkDeleteModal().vm.$emit('primary');

      await waitForPromises();

      expect(findBulkDelete().props('selectedArtifacts')).toStrictEqual(selectedArtifacts);
      expect(createAlert).toHaveBeenCalledWith({
        captureError: true,
        error: expect.any(Error),
        message: I18N_BULK_DELETE_ERROR,
      });
    });

    it('shows no checkboxes without permission', async () => {
      createComponent({
        canDestroyArtifacts: false,
      });

      await waitForPromises();

      expect(findAnyCheckbox().exists()).toBe(false);
    });
  });

  describe('pagination', () => {
    const { pageInfo } = getJobArtifactsResponseThatPaginates.data.project.jobs;
    const query = jest.fn().mockResolvedValue(getJobArtifactsResponseThatPaginates);

    beforeEach(async () => {
      createComponent({
        handlers: {
          getJobArtifactsQuery: query,
        },
        data: { pageInfo },
      });

      await nextTick();
    });

    it('renders pagination and passes page props', () => {
      expect(findPagination().props()).toMatchObject({
        value: INITIAL_CURRENT_PAGE,
        prevPage: Number(pageInfo.hasPreviousPage),
        nextPage: Number(pageInfo.hasNextPage),
      });

      expect(query).toHaveBeenCalledWith({
        projectPath: 'project/path',
        firstPageSize: JOBS_PER_PAGE,
        lastPageSize: null,
        nextPageCursor: '',
        prevPageCursor: '',
      });
    });

    it('updates query variables when going to previous page', async () => {
      await setPage(1);

      expect(query).toHaveBeenLastCalledWith({
        projectPath: 'project/path',
        firstPageSize: null,
        lastPageSize: JOBS_PER_PAGE,
        prevPageCursor: pageInfo.startCursor,
      });
      expect(findPagination().props('value')).toEqual(1);
    });

    it('updates query variables when going to next page', async () => {
      await setPage(2);

      expect(query).toHaveBeenLastCalledWith({
        projectPath: 'project/path',
        firstPageSize: JOBS_PER_PAGE,
        lastPageSize: null,
        prevPageCursor: '',
        nextPageCursor: pageInfo.endCursor,
      });
      expect(findPagination().props('value')).toEqual(2);
    });
  });
});