summaryrefslogtreecommitdiff
path: root/spec/frontend/artifacts/components/artifacts_table_row_details_spec.js
blob: c6ad13462f9072b2e4a30cfead6286ba93781a58 (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
import { GlModal } from '@gitlab/ui';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import getJobArtifactsResponse from 'test_fixtures/graphql/artifacts/graphql/queries/get_job_artifacts.query.graphql.json';
import waitForPromises from 'helpers/wait_for_promises';
import ArtifactsTableRowDetails from '~/artifacts/components/artifacts_table_row_details.vue';
import ArtifactRow from '~/artifacts/components/artifact_row.vue';
import ArtifactDeleteModal from '~/artifacts/components/artifact_delete_modal.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import destroyArtifactMutation from '~/artifacts/graphql/mutations/destroy_artifact.mutation.graphql';
import { I18N_DESTROY_ERROR, I18N_MODAL_TITLE } from '~/artifacts/constants';
import { createAlert } from '~/flash';

jest.mock('~/flash');

const { artifacts } = getJobArtifactsResponse.data.project.jobs.nodes[0];
const refetchArtifacts = jest.fn();

Vue.use(VueApollo);

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

  const findModal = () => wrapper.findComponent(GlModal);

  const createComponent = (
    handlers = {
      destroyArtifactMutation: jest.fn(),
    },
  ) => {
    requestHandlers = handlers;
    wrapper = mountExtended(ArtifactsTableRowDetails, {
      apolloProvider: createMockApollo([
        [destroyArtifactMutation, requestHandlers.destroyArtifactMutation],
      ]),
      propsData: {
        artifacts,
        refetchArtifacts,
        queryVariables: {},
      },
      data() {
        return { deletingArtifactId: null };
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
  });

  describe('passes correct props', () => {
    beforeEach(() => {
      createComponent();
    });

    it('to the artifact rows', () => {
      [0, 1, 2].forEach((index) => {
        expect(wrapper.findAllComponents(ArtifactRow).at(index).props()).toMatchObject({
          artifact: artifacts.nodes[index],
        });
      });
    });
  });

  describe('when the artifact row emits the delete event', () => {
    it('shows the artifact delete modal', async () => {
      createComponent();
      await waitForPromises();

      expect(findModal().props('visible')).toBe(false);

      await wrapper.findComponent(ArtifactRow).vm.$emit('delete');

      expect(findModal().props('visible')).toBe(true);
      expect(findModal().props('title')).toBe(I18N_MODAL_TITLE(artifacts.nodes[0].name));
    });
  });

  describe('when the artifact delete modal emits its primary event', () => {
    it('triggers the destroyArtifact GraphQL mutation', async () => {
      createComponent();
      await waitForPromises();

      wrapper.findComponent(ArtifactRow).vm.$emit('delete');
      wrapper.findComponent(ArtifactDeleteModal).vm.$emit('primary');

      expect(requestHandlers.destroyArtifactMutation).toHaveBeenCalledWith({
        id: artifacts.nodes[0].id,
      });
    });

    it('displays a flash message and refetches artifacts when the mutation fails', async () => {
      createComponent({
        destroyArtifactMutation: jest.fn().mockRejectedValue(new Error('Error!')),
      });
      await waitForPromises();

      expect(wrapper.emitted('refetch')).toBeUndefined();

      wrapper.findComponent(ArtifactRow).vm.$emit('delete');
      wrapper.findComponent(ArtifactDeleteModal).vm.$emit('primary');
      await waitForPromises();

      expect(createAlert).toHaveBeenCalledWith({ message: I18N_DESTROY_ERROR });
      expect(wrapper.emitted('refetch')).toBeDefined();
    });
  });

  describe('when the artifact delete modal is cancelled', () => {
    it('does not trigger the destroyArtifact GraphQL mutation', async () => {
      createComponent();
      await waitForPromises();

      wrapper.findComponent(ArtifactRow).vm.$emit('delete');
      wrapper.findComponent(ArtifactDeleteModal).vm.$emit('cancel');

      expect(requestHandlers.destroyArtifactMutation).not.toHaveBeenCalled();
    });
  });
});