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

mr_widget_merged_spec.js « states « components « vue_merge_request_widget « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e44e2834a0e53a05a575b47ae619c16994a081ab (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
import { getByRole } from '@testing-library/dom';
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import { OPEN_REVERT_MODAL, OPEN_CHERRY_PICK_MODAL } from '~/projects/commit/constants';
import modalEventHub from '~/projects/commit/event_hub';
import MergedComponent from '~/vue_merge_request_widget/components/states/mr_widget_merged.vue';
import eventHub from '~/vue_merge_request_widget/event_hub';

describe('MRWidgetMerged', () => {
  let wrapper;
  const targetBranch = 'foo';
  const mr = {
    isRemovingSourceBranch: false,
    cherryPickInForkPath: false,
    canCherryPickInCurrentMR: true,
    revertInForkPath: false,
    canRevertInCurrentMR: true,
    canRemoveSourceBranch: true,
    sourceBranchRemoved: true,
    metrics: {
      mergedBy: {
        name: 'Administrator',
        username: 'root',
        webUrl: 'http://localhost:3000/root',
        avatarUrl:
          'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon',
      },
      mergedAt: 'Jan 24, 2018 1:02pm UTC',
      readableMergedAt: '',
      closedBy: {},
      closedAt: 'Jan 24, 2018 1:02pm UTC',
      readableClosedAt: '',
    },
    updatedAt: 'mergedUpdatedAt',
    shortMergeCommitSha: '958c0475',
    mergeCommitSha: '958c047516e182dfc52317f721f696e8a1ee85ed',
    mergeCommitPath:
      'http://localhost:3000/root/nautilus/commit/f7ce827c314c9340b075657fd61c789fb01cf74d',
    sourceBranch: 'bar',
    targetBranch,
  };

  const service = {
    removeSourceBranch: () => nextTick(),
  };

  const createComponent = (customMrFields = {}) => {
    wrapper = mount(MergedComponent, {
      propsData: {
        mr: {
          ...mr,
          ...customMrFields,
        },
        service,
      },
    });
  };

  beforeEach(() => {
    jest.spyOn(document, 'dispatchEvent');
    jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
  });

  const findButtonByText = (text) =>
    wrapper.findAll('button').wrappers.find((w) => w.text() === text);
  const findRemoveSourceBranchButton = () => findButtonByText('Delete source branch');

  describe('remove source branch button', () => {
    it('is displayed when sourceBranchRemoved is false', () => {
      createComponent({ sourceBranchRemoved: false });

      expect(findRemoveSourceBranchButton().exists()).toBe(true);
    });

    it('is not displayed when sourceBranchRemoved is true', () => {
      createComponent({ sourceBranchRemoved: true });

      expect(findRemoveSourceBranchButton()).toBe(undefined);
    });

    it('is not displayed when canRemoveSourceBranch is true', () => {
      createComponent({ sourceBranchRemoved: false, canRemoveSourceBranch: false });

      expect(findRemoveSourceBranchButton()).toBe(undefined);
    });

    it('is not displayed when is making request', async () => {
      createComponent({ sourceBranchRemoved: false, canRemoveSourceBranch: true });

      await findRemoveSourceBranchButton().trigger('click');

      expect(findRemoveSourceBranchButton()).toBe(undefined);
    });

    it('is not displayed when all are true', () => {
      createComponent({
        isRemovingSourceBranch: true,
        sourceBranchRemoved: false,
        canRemoveSourceBranch: true,
      });

      expect(findRemoveSourceBranchButton()).toBe(undefined);
    });
  });

  it('should set flag and call service then request main component to update the widget when branch is removed', async () => {
    createComponent({ sourceBranchRemoved: false });
    jest.spyOn(service, 'removeSourceBranch').mockResolvedValue({
      data: {
        message: 'Branch was deleted',
      },
    });

    await findRemoveSourceBranchButton().trigger('click');

    await waitForPromises();

    const args = eventHub.$emit.mock.calls[0];

    expect(args[0]).toEqual('MRWidgetUpdateRequested');
    expect(args[1]).not.toThrow();
  });

  it('calls dispatchDocumentEvent to load in the modal component', () => {
    createComponent();

    expect(document.dispatchEvent).toHaveBeenCalledWith(new CustomEvent('merged:UpdateActions'));
  });

  it('emits event to open the revert modal on revert button click', () => {
    createComponent();
    const eventHubSpy = jest.spyOn(modalEventHub, '$emit');

    getByRole(wrapper.element, 'button', { name: /Revert/i }).click();

    expect(eventHubSpy).toHaveBeenCalledWith(OPEN_REVERT_MODAL);
  });

  it('emits event to open the cherry-pick modal on cherry-pick button click', () => {
    createComponent();
    const eventHubSpy = jest.spyOn(modalEventHub, '$emit');

    getByRole(wrapper.element, 'button', { name: /Cherry-pick/i }).click();

    expect(eventHubSpy).toHaveBeenCalledWith(OPEN_CHERRY_PICK_MODAL);
  });

  it('has merged by information', () => {
    createComponent();

    expect(wrapper.text()).toContain('Merged by');
    expect(wrapper.text()).toContain('Administrator');
  });

  it('shows revert and cherry-pick buttons', () => {
    createComponent();

    expect(wrapper.text()).toContain('Revert');
    expect(wrapper.text()).toContain('Cherry-pick');
  });

  it('should use mergedEvent mergedAt as tooltip title', () => {
    createComponent();

    expect(wrapper.find('time').attributes('title')).toBe('Jan 24, 2018 1:02pm UTC');
  });
});