summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/stub_component.js
blob: 4f9d1ee6f5dc7c3e51a8ec003721189a580477aa (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
/**
 * Returns a new object with keys pointing to stubbed methods
 *
 * This is helpful for stubbing components like GlModal where it's supported
 * in the API to call `.show()` and `.hide()` ([Bootstrap Vue docs][1]).
 *
 * [1]: https://bootstrap-vue.org/docs/components/modal#using-show-hide-and-toggle-component-methods
 *
 * @param {Object} methods - Object whose keys will be in the returned object.
 */
const createStubbedMethods = (methods = {}) => {
  if (!methods) {
    return {};
  }

  return Object.keys(methods).reduce(
    (acc, key) =>
      Object.assign(acc, {
        [key]: () => {},
      }),
    {},
  );
};

export const RENDER_ALL_SLOTS_TEMPLATE = `<div>
  <template v-for="(_, name) in $scopedSlots">
    <div :data-testid="'slot-' + name">            
      <slot :name="name" />
    </div>
  </template>
</div>`;

export function stubComponent(Component, options = {}) {
  return {
    props: Component.props,
    model: Component.model,
    methods: createStubbedMethods(Component.methods),
    // Do not render any slots/scoped slots except default
    // This differs from VTU behavior which renders all slots
    template: '<div><slot></slot></div>',
    // allows wrapper.find(Component) to work for stub
    $_vueTestUtils_original: Component,
    ...options,
  };
}