@inkline/inkline
Version:
Inkline is the Vue.js UI/UX Library built for creating your next design system
55 lines • 1.82 kB
JavaScript
/**
* Simulates a Vue instance with props, methods, data and computed properties
* for scenarios that cannot be tested using testing-library.
*
* @param component
* @param options
*/
export const createMockInstance = (component, { inject = {}, data = {}, props = {}, computed = {}, methods = {}, mocks = {}, $el = document.createElement('div'), $refs = {} } = {}) => {
const instance = {
$emit: jest.fn(),
$el,
$refs
};
if (component.props) {
Object.entries(component.props).forEach(([key, prop]) => {
instance[key] = props[key] ??
(typeof prop.default === 'function'
? prop.default()
: prop.default);
});
}
if (component.methods) {
Object.entries(component.methods).forEach(([key, fn]) => {
instance[key] = jest.fn().mockImplementation(methods[key] || fn);
});
}
if (component.data) {
Object.entries(component.data.call(instance)).forEach(([key, value]) => {
instance[key] = data[key] || value;
});
}
if (component.inject) {
Object.entries(component.inject).forEach(([key, value]) => {
instance[key] = inject[key] || value?.default();
});
}
if (component.computed) {
Object.entries(component.computed).forEach(([key, fn]) => {
Object.defineProperty(instance, key, {
get: computed[key] || fn
});
});
}
Object.entries(mocks).forEach(([key, mock]) => {
instance[key] = mock;
});
if (component.created) {
component.created.call(instance);
}
if (component.mounted) {
component.mounted.call(instance);
}
return instance;
};
//# sourceMappingURL=createMockInstance.mjs.map