summaryrefslogtreecommitdiff
path: root/spec/javascripts/diffs/components/compare_versions_dropdown_spec.js
blob: df160d7a36383e8e6c42c6e4c6efe8a3b755b85f (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
import { shallowMount, createLocalVue } from '@vue/test-utils';
import CompareVersionsDropdown from '~/diffs/components/compare_versions_dropdown.vue';
import diffsMockData from '../mock_data/merge_request_diffs';
import TimeAgo from '~/vue_shared/components/time_ago_tooltip.vue';

const localVue = createLocalVue();
const targetBranch = { branchName: 'tmp-wine-dev', versionIndex: -1 };
const startVersion = { version_index: 4 };
const mergeRequestVersion = {
  version_path: '123',
};
const baseVersionPath = '/gnuwget/wget2/merge_requests/6/diffs?diff_id=37';

describe('CompareVersionsDropdown', () => {
  let wrapper;

  const findSelectedVersion = () => wrapper.find('.dropdown-menu-toggle');
  const findVersionsListElements = () => wrapper.findAll('li');
  const findLinkElement = index =>
    findVersionsListElements()
      .at(index)
      .find('a');
  const findLastLink = () => findLinkElement(findVersionsListElements().length - 1);

  const createComponent = (props = {}) => {
    wrapper = shallowMount(localVue.extend(CompareVersionsDropdown), {
      localVue,
      propsData: { ...props },
    });
  };

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

  describe('selected version name', () => {
    it('shows latest version when latest is selected', () => {
      createComponent({
        mergeRequestVersion,
        startVersion,
        otherVersions: diffsMockData,
      });

      expect(findSelectedVersion().text()).toBe('latest version');
    });

    it('shows target branch name for base branch', () => {
      createComponent({
        targetBranch,
      });

      expect(findSelectedVersion().text()).toBe('tmp-wine-dev');
    });

    it('shows correct version for non-base and non-latest branches', () => {
      createComponent({
        startVersion,
        targetBranch,
      });

      expect(findSelectedVersion().text()).toBe(`version ${startVersion.version_index}`);
    });
  });

  describe('target versions list', () => {
    it('should have the same length as otherVersions if merge request version is present', () => {
      createComponent({
        mergeRequestVersion,
        otherVersions: diffsMockData,
      });

      expect(findVersionsListElements().length).toEqual(diffsMockData.length);
    });

    it('should have an otherVersions length plus 1 if no merge request version is present', () => {
      createComponent({
        targetBranch,
        otherVersions: diffsMockData,
      });

      expect(findVersionsListElements().length).toEqual(diffsMockData.length + 1);
    });

    it('should have base branch link as active on base branch', () => {
      createComponent({
        targetBranch,
        otherVersions: diffsMockData,
      });

      expect(findLastLink().classes()).toContain('is-active');
    });

    it('should have correct branch link as active if start version present', () => {
      createComponent({
        targetBranch,
        startVersion,
        otherVersions: diffsMockData,
      });

      expect(findLinkElement(0).classes()).toContain('is-active');
    });

    it('should render a correct base version link', () => {
      createComponent({
        baseVersionPath,
        otherVersions: diffsMockData.slice(1),
        targetBranch,
      });

      expect(findLastLink().attributes('href')).toEqual(baseVersionPath);
      expect(findLastLink().text()).toContain('(base)');
    });

    it('should not render commits count if no showCommitsCount is passed', () => {
      createComponent({
        otherVersions: diffsMockData,
        targetBranch,
      });

      const commitsCount = diffsMockData[0].commits_count;

      expect(findLinkElement(0).text()).not.toContain(`${commitsCount} commit`);
    });

    it('should render correct commits count if showCommitsCount is passed', () => {
      createComponent({
        otherVersions: diffsMockData,
        targetBranch,
        showCommitCount: true,
      });

      const commitsCount = diffsMockData[0].commits_count;

      expect(findLinkElement(0).text()).toContain(`${commitsCount} commit`);
    });

    it('should render correct commit sha', () => {
      createComponent({
        otherVersions: diffsMockData,
        targetBranch,
      });

      const commitShaElement = findLinkElement(0).find('.commit-sha');

      expect(commitShaElement.text()).toBe(diffsMockData[0].short_commit_sha);
    });

    it('should render correct time-ago ', () => {
      createComponent({
        otherVersions: diffsMockData,
        targetBranch,
      });

      const timeAgoElement = findLinkElement(0).find(TimeAgo);

      expect(timeAgoElement.exists()).toBe(true);
      expect(timeAgoElement.props('time')).toBe(diffsMockData[0].created_at);
    });
  });
});