summaryrefslogtreecommitdiff
path: root/spec/frontend/ci_lint/components/ci_lint_spec.js
blob: e617cca499d15b749b8f5ddac28db5f14438e1e2 (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
import { shallowMount } from '@vue/test-utils';
import EditorLite from '~/vue_shared/components/editor_lite.vue';
import CiLint from '~/ci_lint/components/ci_lint.vue';
import lintCIMutation from '~/ci_lint/graphql/mutations/lint_ci.mutation.graphql';

describe('CI Lint', () => {
  let wrapper;

  const endpoint = '/namespace/project/-/ci/lint';
  const content =
    "test_job:\n  stage: build\n  script: echo 'Building'\n  only:\n    - web\n    - chat\n    - pushes\n  allow_failure: true  ";

  const createComponent = () => {
    wrapper = shallowMount(CiLint, {
      data() {
        return {
          content,
        };
      },
      propsData: {
        endpoint,
        helpPagePath: '/help/ci/lint#pipeline-simulation',
      },
      mocks: {
        $apollo: {
          mutate: jest.fn(),
        },
      },
    });
  };

  const findEditor = () => wrapper.find(EditorLite);
  const findValidateBtn = () => wrapper.find('[data-testid="ci-lint-validate"]');
  const findClearBtn = () => wrapper.find('[data-testid="ci-lint-clear"]');

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

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

  it('displays the editor', () => {
    expect(findEditor().exists()).toBe(true);
  });

  it('validate action calls mutation correctly', () => {
    findValidateBtn().vm.$emit('click');

    expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
      mutation: lintCIMutation,
      variables: { content, dry: false, endpoint },
    });
  });

  it('validate action calls mutation with dry run', async () => {
    const dryRunEnabled = true;

    await wrapper.setData({ dryRun: dryRunEnabled });

    findValidateBtn().vm.$emit('click');

    expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
      mutation: lintCIMutation,
      variables: { content, dry: dryRunEnabled, endpoint },
    });
  });

  it('content is cleared on clear action', async () => {
    expect(findEditor().props('value')).toBe(content);

    await findClearBtn().vm.$emit('click');

    expect(findEditor().props('value')).toBe('');
  });
});