summaryrefslogtreecommitdiff
path: root/spec/frontend/artifacts_settings/components/keep_latest_artifact_checkbox_spec.js
blob: fe2886d6c9515facd06baddefc1d72ec27704aaf (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
import { GlFormCheckbox, GlLink } from '@gitlab/ui';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import UpdateKeepLatestArtifactProjectSetting from '~/artifacts_settings/graphql/mutations/update_keep_latest_artifact_project_setting.mutation.graphql';
import GetKeepLatestArtifactApplicationSetting from '~/artifacts_settings/graphql/queries/get_keep_latest_artifact_application_setting.query.graphql';
import GetKeepLatestArtifactProjectSetting from '~/artifacts_settings/graphql/queries/get_keep_latest_artifact_project_setting.query.graphql';
import KeepLatestArtifactCheckbox from '~/artifacts_settings/keep_latest_artifact_checkbox.vue';

const localVue = createLocalVue();
localVue.use(VueApollo);

const keepLatestArtifactProjectMock = {
  data: {
    project: {
      ciCdSettings: { keepLatestArtifact: true },
    },
  },
};

const keepLatestArtifactApplicationMock = {
  data: {
    ciApplicationSettings: {
      keepLatestArtifact: true,
    },
  },
};

const keepLatestArtifactMockResponse = {
  data: { ciCdSettingsUpdate: { errors: [], __typename: 'CiCdSettingsUpdatePayload' } },
};

describe('Keep latest artifact checkbox', () => {
  let wrapper;
  let apolloProvider;
  let requestHandlers;

  const fullPath = 'gitlab-org/gitlab';
  const helpPagePath = '/help/ci/pipelines/job_artifacts';

  const findCheckbox = () => wrapper.find(GlFormCheckbox);
  const findHelpLink = () => wrapper.find(GlLink);

  const createComponent = (handlers) => {
    requestHandlers = {
      keepLatestArtifactProjectQueryHandler: jest
        .fn()
        .mockResolvedValue(keepLatestArtifactProjectMock),
      keepLatestArtifactApplicationQueryHandler: jest
        .fn()
        .mockResolvedValue(keepLatestArtifactApplicationMock),
      keepLatestArtifactMutationHandler: jest
        .fn()
        .mockResolvedValue(keepLatestArtifactMockResponse),
      ...handlers,
    };

    apolloProvider = createMockApollo([
      [GetKeepLatestArtifactProjectSetting, requestHandlers.keepLatestArtifactProjectQueryHandler],
      [
        GetKeepLatestArtifactApplicationSetting,
        requestHandlers.keepLatestArtifactApplicationQueryHandler,
      ],
      [UpdateKeepLatestArtifactProjectSetting, requestHandlers.keepLatestArtifactMutationHandler],
    ]);

    wrapper = shallowMount(KeepLatestArtifactCheckbox, {
      provide: {
        fullPath,
        helpPagePath,
      },
      stubs: {
        GlFormCheckbox,
      },
      localVue,
      apolloProvider,
    });
  };

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

  describe('default', () => {
    beforeEach(() => {
      createComponent();
    });

    it('displays the checkbox and the help link', () => {
      expect(findCheckbox().exists()).toBe(true);
      expect(findHelpLink().exists()).toBe(true);
    });

    it('calls mutation on artifact setting change with correct payload', () => {
      findCheckbox().vm.$emit('change', false);

      expect(requestHandlers.keepLatestArtifactMutationHandler).toHaveBeenCalledWith({
        fullPath,
        keepLatestArtifact: false,
      });
    });
  });

  describe('when application keep latest artifact setting is enabled', () => {
    beforeEach(() => {
      createComponent();
    });

    it('sets correct setting value in checkbox with query result', async () => {
      await wrapper.vm.$nextTick();

      expect(wrapper.element).toMatchSnapshot();
    });

    it('checkbox is enabled when application setting is enabled', async () => {
      await wrapper.vm.$nextTick();

      expect(findCheckbox().attributes('disabled')).toBeUndefined();
    });
  });

  describe('when application keep latest artifact setting is disabled', () => {
    it('checkbox is disabled when application setting is disabled', async () => {
      createComponent({
        keepLatestArtifactApplicationQueryHandler: jest.fn().mockResolvedValue({
          data: {
            ciApplicationSettings: {
              keepLatestArtifact: false,
            },
          },
        }),
      });

      await wrapper.vm.$nextTick();

      expect(wrapper.element).toMatchSnapshot();
      expect(findCheckbox().attributes('disabled')).toBe('true');
    });
  });
});