debian-mirror-gitlab/spec/frontend/__helpers__/stub_component.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
/**
* 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]: () => {},
}),
{},
);
};
2022-08-27 11:52:29 +05:30
export const RENDER_ALL_SLOTS_TEMPLATE = `<div>
<template v-for="(_, name) in $scopedSlots">
<div :data-testid="'slot-' + name">
<slot :name="name" />
</div>
</template>
</div>`;
2021-02-22 17:27:13 +05:30
export function stubComponent(Component, options = {}) {
return {
props: Component.props,
model: Component.model,
2021-03-11 19:13:27 +05:30
methods: createStubbedMethods(Component.methods),
2021-02-22 17:27:13 +05:30
// Do not render any slots/scoped slots except default
// This differs from VTU behavior which renders all slots
template: '<div><slot></slot></div>',
2022-11-25 23:54:43 +05:30
// allows wrapper.findComponent(Component) to work for stub
2021-02-22 17:27:13 +05:30
$_vueTestUtils_original: Component,
...options,
};
}