summaryrefslogtreecommitdiff
path: root/spec/frontend_integration/ide/ide_integration_spec.js
blob: 7e8fb3a32eef0b1f01d4fc9caa505bf104b50263 (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
/**
 * WARNING: WIP
 *
 * Please do not copy from this spec or use it as an example for anything.
 *
 * This is in place to iteratively set up the frontend integration testing environment
 * and will be improved upon in a later iteration.
 *
 * See https://gitlab.com/gitlab-org/gitlab/-/issues/208800 for more information.
 */
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { initIde } from '~/ide';

jest.mock('~/api', () => {
  return {
    project: jest.fn().mockImplementation(() => new Promise(() => {})),
  };
});

jest.mock('~/ide/services/gql', () => {
  return {
    query: jest.fn().mockImplementation(() => new Promise(() => {})),
  };
});

describe('WebIDE', () => {
  let vm;
  let root;
  let mock;
  let initData;
  let location;

  beforeEach(() => {
    root = document.createElement('div');
    initData = {
      emptyStateSvgPath: '/test/empty_state.svg',
      noChangesStateSvgPath: '/test/no_changes_state.svg',
      committedStateSvgPath: '/test/committed_state.svg',
      pipelinesEmptyStateSvgPath: '/test/pipelines_empty_state.svg',
      promotionSvgPath: '/test/promotion.svg',
      ciHelpPagePath: '/test/ci_help_page',
      webIDEHelpPagePath: '/test/web_ide_help_page',
      clientsidePreviewEnabled: 'true',
      renderWhitespaceInCode: 'false',
      codesandboxBundlerUrl: 'test/codesandbox_bundler',
    };

    mock = new MockAdapter(axios);
    mock.onAny('*').reply(() => new Promise(() => {}));

    location = { pathname: '/-/ide/project/gitlab-test/test', search: '', hash: '' };
    Object.defineProperty(window, 'location', {
      get() {
        return location;
      },
    });
  });

  afterEach(() => {
    vm.$destroy();
    vm = null;

    mock.restore();
  });

  const createComponent = () => {
    const el = document.createElement('div');
    Object.assign(el.dataset, initData);
    root.appendChild(el);
    vm = initIde(el);
  };

  expect.addSnapshotSerializer({
    test(value) {
      return value instanceof HTMLElement && !value.$_hit;
    },
    print(element, serialize) {
      element.$_hit = true;
      element.querySelectorAll('[style]').forEach(el => {
        el.$_hit = true;
        if (el.style.display === 'none') {
          el.textContent = '(jest: contents hidden)';
        }
      });

      return serialize(element)
        .replace(/^\s*<!---->$/gm, '')
        .replace(/\n\s*\n/gm, '\n');
    },
  });

  it('runs', () => {
    createComponent();

    return vm.$nextTick().then(() => {
      expect(root).toMatchSnapshot();
    });
  });
});