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

vue_compat_test_setup.js « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad1230f2ca9fb5f6e5419bb15141368ef8ee76a1 (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
/* eslint-disable import/no-commonjs */
const Vue = require('vue');
const VTU = require('@vue/test-utils');
const { installCompat: installVTUCompat, fullCompatConfig } = require('vue-test-utils-compat');

function getComponentName(component) {
  if (!component) {
    return undefined;
  }

  return (
    component.name ||
    getComponentName(component.extends) ||
    component.mixins?.find((mixin) => getComponentName(mixin))
  );
}

function isLegacyExtendedComponent(component) {
  return Reflect.has(component, 'super') && component.super.extend({}).super === component.super;
}
function unwrapLegacyVueExtendComponent(selector) {
  return isLegacyExtendedComponent(selector) ? selector.options : selector;
}
function getStubProps(component) {
  const stubProps = { ...component.props };
  component.mixins?.forEach((mixin) => {
    Object.assign(stubProps, unwrapLegacyVueExtendComponent(mixin).props);
  });
  return stubProps;
}

if (global.document) {
  const compatConfig = {
    MODE: 2,

    GLOBAL_MOUNT: 'suppress-warning',
    GLOBAL_EXTEND: 'suppress-warning',
    GLOBAL_PROTOTYPE: 'suppress-warning',
    RENDER_FUNCTION: 'suppress-warning',

    INSTANCE_DESTROY: 'suppress-warning',
    INSTANCE_DELETE: 'suppress-warning',

    INSTANCE_ATTRS_CLASS_STYLE: 'suppress-warning',
    INSTANCE_CHILDREN: 'suppress-warning',
    INSTANCE_SCOPED_SLOTS: 'suppress-warning',
    INSTANCE_LISTENERS: 'suppress-warning',
    INSTANCE_EVENT_EMITTER: 'suppress-warning',
    INSTANCE_EVENT_HOOKS: 'suppress-warning',
    INSTANCE_SET: 'suppress-warning',
    GLOBAL_OBSERVABLE: 'suppress-warning',
    GLOBAL_SET: 'suppress-warning',
    COMPONENT_FUNCTIONAL: 'suppress-warning',
    COMPONENT_V_MODEL: 'suppress-warning',
    COMPONENT_ASYNC: 'suppress-warning',
    CUSTOM_DIR: 'suppress-warning',
    OPTIONS_BEFORE_DESTROY: 'suppress-warning',
    OPTIONS_DATA_MERGE: 'suppress-warning',
    OPTIONS_DATA_FN: 'suppress-warning',
    OPTIONS_DESTROYED: 'suppress-warning',
    ATTR_FALSE_VALUE: 'suppress-warning',

    COMPILER_V_ON_NATIVE: 'suppress-warning',
    COMPILER_V_BIND_OBJECT_ORDER: 'suppress-warning',

    CONFIG_WHITESPACE: 'suppress-warning',
    CONFIG_OPTION_MERGE_STRATS: 'suppress-warning',
    PRIVATE_APIS: 'suppress-warning',
    WATCH_ARRAY: 'suppress-warning',
  };

  let compatH;
  Vue.config.compilerOptions.whitespace = 'preserve';
  Vue.createApp({
    compatConfig: {
      MODE: 3,
      RENDER_FUNCTION: 'suppress-warning',
    },
    render(h) {
      compatH = h;
    },
  }).mount(document.createElement('div'));

  Vue.configureCompat(compatConfig);
  installVTUCompat(VTU, fullCompatConfig, compatH);

  jest.mock('vue', () => {
    const actualVue = jest.requireActual('vue');
    actualVue.configureCompat(compatConfig);
    return actualVue;
  });

  jest.mock('@vue/test-utils', () => {
    const actualVTU = jest.requireActual('@vue/test-utils');

    return {
      ...actualVTU,
      RouterLinkStub: {
        ...actualVTU.RouterLinkStub,
        render() {
          const { default: defaultSlot } = this.$slots ?? {};
          const defaultSlotFn =
            defaultSlot && typeof defaultSlot !== 'function' ? () => defaultSlot : defaultSlot;
          return actualVTU.RouterLinkStub.render.call({
            $slots: defaultSlot ? { default: defaultSlotFn } : undefined,
            custom: this.custom,
          });
        },
      },
    };
  });

  jest.mock('portal-vue', () => ({
    __esModule: true,
    default: {
      install: jest.fn(),
    },
    Portal: {},
    PortalTarget: {},
    MountingPortal: {
      template: '<h1>MOUNTING-PORTAL</h1>',
    },
    Wormhole: {},
  }));

  VTU.config.global.renderStubDefaultSlot = true;

  const noop = () => {};
  const invalidProperties = new Set();

  const getDescriptor = (root, prop) => {
    let obj = root;
    while (obj != null) {
      const desc = Object.getOwnPropertyDescriptor(obj, prop);
      if (desc) {
        return desc;
      }
      obj = Object.getPrototypeOf(obj);
    }
    return null;
  };

  const isPropertyValidOnDomNode = (prop) => {
    if (invalidProperties.has(prop)) {
      return false;
    }

    const domNode = document.createElement('anonymous-stub');
    const descriptor = getDescriptor(domNode, prop);
    if (descriptor && descriptor.get && !descriptor.set) {
      invalidProperties.add(prop);
      return false;
    }

    return true;
  };

  VTU.config.plugins.createStubs = ({ name, component: rawComponent, registerStub, stubs }) => {
    const component = unwrapLegacyVueExtendComponent(rawComponent);
    const hyphenatedName = name.replace(/\B([A-Z])/g, '-$1').toLowerCase();
    const stubTag = stubs?.[name] ? name : hyphenatedName;

    const stub = Vue.defineComponent({
      name: getComponentName(component),
      props: getStubProps(component),
      model: component.model ?? component.mixins?.find((m) => m.model),
      methods: Object.fromEntries(
        Object.entries(component.methods ?? {}).map(([key]) => [key, noop]),
      ),
      render() {
        const { $scopedSlots: scopedSlots = {} } = this;

        // eslint-disable-next-line no-underscore-dangle
        const hasDefaultSlot = 'default' in scopedSlots && scopedSlots.default._ns;
        const shouldRenderAllSlots = !component.functional && !hasDefaultSlot;

        const renderSlotByName = (slotName) => {
          const slot = scopedSlots[slotName];
          let result;
          if (typeof slot === 'function') {
            try {
              result = slot({});
            } catch {
              // intentionally blank
            }
          } else {
            result = slot;
          }
          return result;
        };

        const slotContents = shouldRenderAllSlots
          ? Object.keys(scopedSlots).map(renderSlotByName).filter(Boolean)
          : renderSlotByName('default');

        const props = Object.fromEntries(
          Object.entries(this.$props)
            .filter(([prop]) => isPropertyValidOnDomNode(prop))
            .map(([key, value]) => [key, typeof value === 'function' ? '[Function]' : value]),
        );

        return Vue.h(`${stubTag || 'anonymous'}-stub`, props, slotContents);
      },
    });

    if (typeof component === 'function') {
      component()?.then?.((resolvedComponent) => {
        registerStub({ source: resolvedComponent.default, stub });
      });
    }

    return stub;
  };
}