summaryrefslogtreecommitdiff
path: root/spec/frontend/diffs/store/getters_versions_dropdowns_spec.js
blob: 6ea8f691c3ce1b6a3f9076a531d208f4eb021035 (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
import setWindowLocation from 'helpers/set_window_location_helper';
import {
  DIFF_COMPARE_BASE_VERSION_INDEX,
  DIFF_COMPARE_HEAD_VERSION_INDEX,
} from '~/diffs/constants';
import * as getters from '~/diffs/store/getters';
import state from '~/diffs/store/modules/diff_state';
import diffsMockData from '../mock_data/merge_request_diffs';

describe('Compare diff version dropdowns', () => {
  let localState;

  beforeEach(() => {
    localState = state();
    localState.mergeRequestDiff = {
      base_version_path: 'basePath',
      head_version_path: 'headPath',
      version_index: 1,
    };
    localState.targetBranchName = 'baseVersion';
    localState.mergeRequestDiffs = diffsMockData;
  });

  describe('selectedTargetIndex', () => {
    it('without startVersion', () => {
      expect(getters.selectedTargetIndex(localState)).toEqual(DIFF_COMPARE_BASE_VERSION_INDEX);
    });

    it('with startVersion', () => {
      const startVersion = { version_index: 1 };
      localState.startVersion = startVersion;
      expect(getters.selectedTargetIndex(localState)).toEqual(startVersion.version_index);
    });
  });

  it('selectedSourceIndex', () => {
    expect(getters.selectedSourceIndex(localState)).toEqual(
      localState.mergeRequestDiff.version_index,
    );
  });

  describe('diffCompareDropdownTargetVersions', () => {
    // diffCompareDropdownTargetVersions slices the array at the first position
    // and appends a "base" and "head" version at the end of the list so that
    // "base" and "head" appear at the bottom of the dropdown
    // this is also why we use diffsMockData[1] for the "first" version

    let expectedFirstVersion;
    let expectedBaseVersion;
    let expectedHeadVersion;
    const originalLocation = window.location.href;

    const setupTest = (includeDiffHeadParam) => {
      const diffHeadParam = includeDiffHeadParam ? '?diff_head=true' : '';

      setWindowLocation(diffHeadParam);

      expectedFirstVersion = {
        ...diffsMockData[1],
        href: expect.any(String),
        versionName: expect.any(String),
        selected: false,
      };

      expectedBaseVersion = {
        versionName: 'baseVersion',
        version_index: DIFF_COMPARE_BASE_VERSION_INDEX,
        href: 'basePath',
        isBase: true,
        selected: false,
      };

      expectedHeadVersion = {
        versionName: 'baseVersion',
        version_index: DIFF_COMPARE_HEAD_VERSION_INDEX,
        href: 'headPath',
        isHead: true,
        selected: false,
      };
    };

    const assertVersions = (targetVersions) => {
      // base and head should be the last two versions in that order
      const targetBaseVersion = targetVersions[targetVersions.length - 2];
      const targetHeadVersion = targetVersions[targetVersions.length - 1];
      expect(targetVersions[0]).toEqual(expectedFirstVersion);
      expect(targetBaseVersion).toEqual(expectedBaseVersion);
      expect(targetHeadVersion).toEqual(expectedHeadVersion);
    };

    afterEach(() => {
      setWindowLocation(originalLocation);
    });

    it('base version selected', () => {
      setupTest();
      expectedBaseVersion.selected = true;

      const targetVersions = getters.diffCompareDropdownTargetVersions(localState, getters);
      assertVersions(targetVersions);
    });

    it('head version selected', () => {
      setupTest(true);

      expectedHeadVersion.selected = true;

      const targetVersions = getters.diffCompareDropdownTargetVersions(localState, getters);
      assertVersions(targetVersions);
    });

    it('first version selected', () => {
      // NOTE: It should not be possible to have both "diff_head=true" and
      // have anything other than the head version selected, but the user could
      // manually add "?diff_head=true" to the url. In this instance we still
      // want the actual selected version to display as "selected"
      // Passing in "true" here asserts that first version is still selected
      // even if "diff_head" is present in the url
      setupTest(true);

      expectedFirstVersion.selected = true;
      localState.startVersion = expectedFirstVersion;

      const targetVersions = getters.diffCompareDropdownTargetVersions(localState, {
        selectedTargetIndex: expectedFirstVersion.version_index,
      });
      assertVersions(targetVersions);
    });
  });

  it('diffCompareDropdownSourceVersions', () => {
    const firstDiff = localState.mergeRequestDiffs[0];
    const expectedShape = {
      ...firstDiff,
      href: firstDiff.version_path,
      commitsText: `${firstDiff.commits_count} commits,`,
      isLatestVersion: true,
      versionName: 'latest version',
      selected: true,
    };

    const sourceVersions = getters.diffCompareDropdownSourceVersions(localState, {
      selectedSourceIndex: expectedShape.version_index,
    });
    expect(sourceVersions[0]).toEqual(expectedShape);
    expect(sourceVersions[1]).toMatchObject({
      selected: false,
      isLatestVersion: false,
    });
  });
});