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

switch_editors_view_spec.js « switch_editors « components « ide « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7a958391fea45f9d773a84938b5a17de815713d7 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { GlButton, GlEmptyState, GlLink } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { logError } from '~/lib/logger';
import { __ } from '~/locale';
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
import SwitchEditorsView, {
  MSG_ERROR_ALERT,
  MSG_CONFIRM,
  MSG_TITLE,
  MSG_LEARN_MORE,
  MSG_DESCRIPTION,
} from '~/ide/components/switch_editors/switch_editors_view.vue';
import eventHub from '~/ide/eventhub';
import { createStore } from '~/ide/stores';

jest.mock('~/flash');
jest.mock('~/lib/logger');
jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal');

const TEST_USER_PREFERENCES_PATH = '/test/user-pref/path';
const TEST_SWITCH_EDITOR_SVG_PATH = '/test/switch/editor/path.svg';
const TEST_HREF = '/test/new/web/ide/href';

describe('~/ide/components/switch_editors/switch_editors_view.vue', () => {
  useMockLocationHelper();

  let store;
  let wrapper;
  let confirmResolve;
  let requestSpy;
  let skipBeforeunloadSpy;
  let axiosMock;

  // region: finders ------------------
  const findButton = () => wrapper.findComponent(GlButton);
  const findEmptyState = () => wrapper.findComponent(GlEmptyState);

  // region: actions ------------------
  const triggerSwitchPreference = () => findButton().vm.$emit('click');
  const submitConfirm = async (val) => {
    confirmResolve(val);

    // why: We need to wait for promises for the immediate next lines to be executed
    await waitForPromises();
  };

  const createComponent = () => {
    wrapper = shallowMount(SwitchEditorsView, {
      store,
      stubs: {
        GlEmptyState,
      },
    });
  };

  // region: test setup ------------------
  beforeEach(() => {
    // Setup skip-beforeunload side-effect
    skipBeforeunloadSpy = jest.fn();
    eventHub.$on('skip-beforeunload', skipBeforeunloadSpy);

    // Setup request side-effect
    requestSpy = jest.fn().mockImplementation(() => new Promise(() => {}));
    axiosMock = new MockAdapter(axios);
    axiosMock.onPut(TEST_USER_PREFERENCES_PATH).reply(({ data }) => requestSpy(data));

    // Setup store
    store = createStore();
    store.state.userPreferencesPath = TEST_USER_PREFERENCES_PATH;
    store.state.switchEditorSvgPath = TEST_SWITCH_EDITOR_SVG_PATH;
    store.state.links = {
      newWebIDEHelpPagePath: TEST_HREF,
    };

    // Setup user confirm side-effect
    confirmAction.mockImplementation(
      () =>
        new Promise((resolve) => {
          confirmResolve = resolve;
        }),
    );
  });

  afterEach(() => {
    eventHub.$off('skip-beforeunload', skipBeforeunloadSpy);

    axiosMock.restore();
  });

  // region: tests ------------------
  describe('default', () => {
    beforeEach(() => {
      createComponent();
    });

    it('render empty state', () => {
      expect(findEmptyState().props()).toMatchObject({
        svgPath: TEST_SWITCH_EDITOR_SVG_PATH,
        svgHeight: 150,
        title: MSG_TITLE,
      });
    });

    it('render link', () => {
      expect(wrapper.findComponent(GlLink).attributes('href')).toBe(TEST_HREF);
      expect(wrapper.findComponent(GlLink).text()).toBe(MSG_LEARN_MORE);
    });

    it('renders description', () => {
      expect(findEmptyState().text()).toContain(MSG_DESCRIPTION);
    });

    it('is not loading', () => {
      expect(findButton().props('loading')).toBe(false);
    });
  });

  describe('when user triggers switch preference', () => {
    beforeEach(() => {
      createComponent();

      triggerSwitchPreference();
    });

    it('creates a single confirm', () => {
      // Call again to ensure that we only show 1 confirm action
      triggerSwitchPreference();

      expect(confirmAction).toHaveBeenCalledTimes(1);
      expect(confirmAction).toHaveBeenCalledWith(MSG_CONFIRM, {
        primaryBtnText: __('Switch editors'),
        cancelBtnText: __('Cancel'),
      });
    });

    it('starts loading', () => {
      expect(findButton().props('loading')).toBe(true);
    });

    describe('when user cancels confirm', () => {
      beforeEach(async () => {
        await submitConfirm(false);
      });

      it('does not make request', () => {
        expect(requestSpy).not.toHaveBeenCalled();
      });

      it('can be triggered again', () => {
        triggerSwitchPreference();

        expect(confirmAction).toHaveBeenCalledTimes(2);
      });
    });

    describe('when user accepts confirm and response success', () => {
      beforeEach(async () => {
        requestSpy.mockReturnValue([200, {}]);
        await submitConfirm(true);
      });

      it('does not handle error', () => {
        expect(logError).not.toHaveBeenCalled();
        expect(createAlert).not.toHaveBeenCalled();
      });

      it('emits "skip-beforeunload" and reloads', () => {
        expect(skipBeforeunloadSpy).toHaveBeenCalledTimes(1);
        expect(window.location.reload).toHaveBeenCalledTimes(1);
      });

      it('calls request', () => {
        expect(requestSpy).toHaveBeenCalledTimes(1);
        expect(requestSpy).toHaveBeenCalledWith(
          JSON.stringify({ user: { use_legacy_web_ide: false } }),
        );
      });

      it('is not loading', () => {
        expect(findButton().props('loading')).toBe(false);
      });
    });

    describe('when user accepts confirm and response fails', () => {
      beforeEach(async () => {
        requestSpy.mockReturnValue([400, {}]);
        await submitConfirm(true);
      });

      it('handles error', () => {
        expect(logError).toHaveBeenCalledTimes(1);
        expect(logError).toHaveBeenCalledWith(
          'Error while updating user preferences',
          expect.any(Error),
        );

        expect(createAlert).toHaveBeenCalledTimes(1);
        expect(createAlert).toHaveBeenCalledWith({
          message: MSG_ERROR_ALERT,
        });
      });

      it('does not reload', () => {
        expect(skipBeforeunloadSpy).not.toHaveBeenCalled();
        expect(window.location.reload).not.toHaveBeenCalled();
      });
    });
  });
});