-
Notifications
You must be signed in to change notification settings - Fork 668
Support wrapping an already mounted Vue instance #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm against exporting these classes. Most users won't be aware of how to create WrapperArrays, and I don't think we should make them aware. We only expose a few methods as methods to use: mount |
@eddyerburgh Why? Is there a risk I'm not seeing? We're already using instances of these and the documentation is already available. |
It's just that most utilities provided by |
The risk in my opinion is bloating the API and making the library more confusing for users. The docs make a distinction between Wrapper and WrapperArray. But they don't make a distinction between VueWrapper, or Wrapper, and we may add or remove extra Wrappers in the future. I think that exposing methods like this that deal with the internals encourage users to use vue-test-utils rely on internals that could change in the future. Making sure mount and shallow are the only entry points give us more control to change how it works under the hood. |
Is there a way to use Or just a |
No, not at the moment. When would you have an already mounted instance in a test? Can you post an example in code? |
For example, I'm testing our widget API (vanilla JS) which mounts a Vue instance on a optionally passed element: render() {
const doc = this.options.document;
let el = query(this.options.el);
if (!el) {
el = doc.createElement('div');
doc.body.appendChild(el);
}
this.vue = new Vue({
el,
render: h => h(App),
});
} And I want to make sure that the passed element is replaced by our Vue instance's element. // Create a test element
const testClass = 'test-element';
const el = document.createElement('section');
el.classList.add(testClass);
body.append(el);
// Use our element with the widget
const instance = new Widget({ el });
const vue = instance.vue;
it('should replace our element to a Vue instance', () => {
expect(vue.$el).not.toBe(el);
expect(vue.$el.classList.contains(testClass)).toBeFalsy();
expect(body.contains(el)).toBe(false);
}); There's probably a better way to test this, but initially, I wanted to use the Not that it's absolutely necessary, but I wanted to be consistent with the other tests where I'm mounting the components with |
@eddyerburgh |
@38elements what specification do you mean? |
How about exposing the I see that there's a need for this for some users. |
|
Ah, so we don't currently support already mounted elements. We could look at adding support. |
Vue instance and Vue component are different. If Vue instance is supported and mounted vue instance is necessary, the following el option should considered.
|
Since I could not find the description about supporting not mounted Vue instance in document, |
You can create a VueWrapper with a mounted Vue instance, so exposing a createWrapper lets us support a mounted Vue instance. |
I think the title of this issue is not appropriate and "Support Vue instance" is appropriate. I think that Most people will make a method for each function. |
I don't think we should encourage calling
There's already an issue to support an |
The object having el property, not Vue instance will be passed |
Like Vue component, I think that Vue instance requires stub and mock. |
I don't think we should encourage mounting Vue instances, apart from exposing the createWrapper method. To add a mock you add to the Vue class prototype: Vue.prototype.mock = 'value' To add a stub you pass components inside the component options. Maybe after we've exposed createWrapper we can revisit this, but initially I don't think we should add an interface for stubbing or mocking. |
IMHO, I assume a Vue instance which is on page of Laravel or Ruby on Rails as a target for testing. |
Agreed, but we have an open PR that would allow the user to mount an instance |
I think providing Wrapper class and WrapperArray class and supporting Vue instance are different issue. |
IMHO, vue-instance.js import Vue from 'vue'
export const vueInstance = new Vue({
methods: {
foo () {
return 'a'
},
bar () {
return 'b'
}
}
}) import { vueInstance } from './vue-instance'
const options = {
methods: {
bar () {
return 'B'
},
baz () {
return 'C'
}
}
}
let c = vueInstance.$options
c.template = '<div>{{ foo() }}{{ bar() }}{{ baz() }}</div>'
const wrapper = mount(c, options)
expect(wrapper.text()).to.equal('aBC') |
The other day while testing a class that manually mounts a Vue instance to an element in the page, I wanted to use the
Wrapper
utilities but found out that I can't just callnew Wrapper(myVueInstance);
as it seems thatWrapper
andWrapperArray
are not exposed within the public API.I didn't dive deep into the code, so I might have missed a way to achieve what I want.
A quick look at the source and the documentation shows that only the following is exposed:
I found /pull/328 which is close to what I'd want.
The text was updated successfully, but these errors were encountered: