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

revision_dropdown_legacy_spec.js « components « compare « projects « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f64af1aa99483313a073b45b3f8ef0f9166c802e (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
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import AxiosMockAdapter from 'axios-mock-adapter';
import { nextTick } from 'vue';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import RevisionDropdown from '~/projects/compare/components/revision_dropdown_legacy.vue';

const defaultProps = {
  refsProjectPath: 'some/refs/path',
  revisionText: 'Target',
  paramsName: 'from',
  paramsBranch: 'main',
};

jest.mock('~/flash');

describe('RevisionDropdown component', () => {
  let wrapper;
  let axiosMock;

  const createComponent = (props = {}) => {
    wrapper = shallowMount(RevisionDropdown, {
      propsData: {
        ...defaultProps,
        ...props,
      },
    });
  };

  beforeEach(() => {
    axiosMock = new AxiosMockAdapter(axios);
    createComponent();
  });

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

  const findGlDropdown = () => wrapper.findComponent(GlDropdown);

  it('sets hidden input', () => {
    expect(wrapper.find('input[type="hidden"]').attributes('value')).toBe(
      defaultProps.paramsBranch,
    );
  });

  it('update the branches on success', async () => {
    const Branches = ['branch-1', 'branch-2'];
    const Tags = ['tag-1', 'tag-2', 'tag-3'];

    axiosMock.onGet(defaultProps.refsProjectPath).replyOnce(200, {
      Branches,
      Tags,
    });

    createComponent();

    await axios.waitForAll();

    expect(wrapper.vm.branches).toEqual(Branches);
    expect(wrapper.vm.tags).toEqual(Tags);
  });

  it('sets branches and tags to be an empty array when no tags or branches are given', async () => {
    axiosMock.onGet(defaultProps.refsProjectPath).replyOnce(200, {
      Branches: undefined,
      Tags: undefined,
    });

    await axios.waitForAll();

    expect(wrapper.vm.branches).toEqual([]);
    expect(wrapper.vm.tags).toEqual([]);
  });

  it('shows flash message on error', async () => {
    axiosMock.onGet('some/invalid/path').replyOnce(404);

    await wrapper.vm.fetchBranchesAndTags();
    expect(createFlash).toHaveBeenCalled();
  });

  describe('GlDropdown component', () => {
    it('renders props', () => {
      expect(wrapper.props()).toEqual(expect.objectContaining(defaultProps));
    });

    it('display default text', () => {
      createComponent({
        paramsBranch: null,
      });
      expect(findGlDropdown().props('text')).toBe('Select branch/tag');
    });

    it('display params branch text', () => {
      expect(findGlDropdown().props('text')).toBe(defaultProps.paramsBranch);
    });

    it('emits a "selectRevision" event when a revision is selected', async () => {
      const findGlDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
      const findFirstGlDropdownItem = () => findGlDropdownItems().at(0);

      // setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
      // eslint-disable-next-line no-restricted-syntax
      wrapper.setData({ branches: ['some-branch'] });

      await nextTick();

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

      expect(wrapper.emitted()).toEqual({
        selectRevision: [[{ direction: 'from', revision: 'some-branch' }]],
      });
    });
  });
});