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

revision_dropdown_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: 455cd84afd460c550f0b8bda8661cb125d7177dd (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
import { GlDropdown, GlDropdownItem, GlSearchBoxByType } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import AxiosMockAdapter from 'axios-mock-adapter';
import { nextTick } from 'vue';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK } from '~/lib/utils/http_status';
import RevisionDropdown from '~/projects/compare/components/revision_dropdown.vue';
import { revisionDropdownDefaultProps as defaultProps } from './mock_data';

jest.mock('~/alert');

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

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

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

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

  const findGlDropdown = () => wrapper.findComponent(GlDropdown);
  const findSearchBox = () => wrapper.findComponent(GlSearchBoxByType);
  const findBranchesDropdownItem = () =>
    wrapper.findAllComponents('[data-testid="branches-dropdown-item"]');
  const findTagsDropdownItem = () =>
    wrapper.findAllComponents('[data-testid="tags-dropdown-item"]');

  it('sets hidden input', () => {
    createComponent();
    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(HTTP_STATUS_OK, {
      Branches,
      Tags,
    });

    createComponent();

    expect(findBranchesDropdownItem()).toHaveLength(0);
    expect(findTagsDropdownItem()).toHaveLength(0);

    await waitForPromises();

    expect(findBranchesDropdownItem()).toHaveLength(Branches.length);
    expect(findTagsDropdownItem()).toHaveLength(Tags.length);

    Branches.forEach((branch, index) => {
      expect(findBranchesDropdownItem().at(index).text()).toBe(branch);
    });

    Tags.forEach((tag, index) => {
      expect(findTagsDropdownItem().at(index).text()).toBe(tag);
    });
  });

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

    createComponent();
    await waitForPromises();

    expect(createAlert).toHaveBeenCalled();
  });

  it('makes a new request when refsProjectPath is changed', async () => {
    jest.spyOn(axios, 'get');

    const newRefsProjectPath = 'new-selected-project-path';

    createComponent();

    wrapper.setProps({
      ...defaultProps,
      refsProjectPath: newRefsProjectPath,
    });

    await waitForPromises();
    expect(axios.get).toHaveBeenLastCalledWith(newRefsProjectPath);
  });

  describe('search', () => {
    it('shows alert message on error', async () => {
      axiosMock.onGet('some/invalid/path').replyOnce(HTTP_STATUS_NOT_FOUND);

      createComponent();
      await waitForPromises();

      expect(createAlert).toHaveBeenCalled();
    });

    it('makes request with search param', async () => {
      jest.spyOn(axios, 'get').mockResolvedValue({
        data: {
          Branches: [],
          Tags: [],
        },
      });

      const mockSearchTerm = 'foobar';
      createComponent();
      findSearchBox().vm.$emit('input', mockSearchTerm);
      await waitForPromises();

      expect(axios.get).toHaveBeenCalledWith(
        defaultProps.refsProjectPath,
        expect.objectContaining({
          params: {
            search: mockSearchTerm,
          },
        }),
      );
    });
  });

  describe('GlDropdown component', () => {
    it('renders props', () => {
      createComponent();
      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', () => {
      createComponent();
      expect(findGlDropdown().props('text')).toBe(defaultProps.paramsBranch);
    });
  });

  it('emits `selectRevision` event when another revision is selected', async () => {
    jest.spyOn(axios, 'get').mockResolvedValue({
      data: {
        Branches: ['some-branch'],
        Tags: [],
      },
    });

    createComponent();
    await nextTick();

    findGlDropdown().findAllComponents(GlDropdownItem).at(0).vm.$emit('click');

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