-
Notifications
You must be signed in to change notification settings - Fork 668
[help-wanted] How to change a select element #260
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
If you call I am trying to recreate this now, can you post the entire component and test? The issue says "how to change a select element" but you also mentioned a watcher. Since watchers are async, you might need to use |
Edit, had a shot at this. Used the element ui template on their website. I think you should be using the The way I made it "work" in vue-test-utils is by adding I think setting I thought about this a little more though, and I don't think we should unit testing external components anyway. They should have their own tests. vue-test-utils is for testing your components, the code you write. You can be confident The test concerning "when the user selects something from a dropdown, an a watcher causes such and such to happen" sounds more like an integration test - much more high level. Of course I am also new to unit testing and programming in general, so I could be wrong. Here is what I tried regarding your initial question. Comments in the test (second snippet).
I hope that helps, please let me know if you have more questions or anything else. I think for the 1.0 release we should provide some good documentation on what to test, not just how. |
@gsccheng, @lmiller1990: Have you figured it out? @lmiller1990
So in this case, instead changing internal data we should rather simulate the change event. Otherwise handleChange method won't be covered. But frankly I've also found triggering change not working for radio inputs in vue-test-utils (I write specs in Jest). Anyone figured out how to simulate selecting a radio input? |
@folmert to check a radiobutton: wrapper.find('#radioButton2').element.checked = true @gsccheng Thanks for the issue. Are you able to make a minimal reproduction (a test that fails with the least amount of code as possible), and include all the code. Then I can have a look and debug. I can let you know how it's done. |
@eddyerburgh it doesn't work, see my minimal reproduced example, running
Spec: import {mount} from 'vue-test-utils';
import RadioGroup from '@/components/RadioGroup';
describe('RadioGroup', () => {
let wrapperDeep;
beforeEach(() => {
wrapperDeep = mount(RadioGroup, {});
});
// works
it('changes data according to the changed folder data', () => {
wrapperDeep.setData({folder: 'existing'});
expect(wrapperDeep.find('.selected').text()).toBe('Selected: existing');
});
// doesn't work
it('changes data according to the selected folder option', () => {
wrapperDeep.find('input[type="radio"][value="existing"]').element.selected = true;
expect(wrapperDeep.find('.selected').text()).toBe('Selected: existing');
});
}); |
HI @folmert, you're right this is indeed an issue with model not picking up the change. Can you create a new issue for this please? |
To change the value of inputs and update data bound with const input = wrapper.find('input[type="text"]')
input.element.value = 'some value' // input element value is changed, v-model is not
input.trigger('input') // v-model updated const radioInput = wrapper.find('input[type="radio"]')
radioInput.element.checked = true // input element value is changed, v-model is not
radioInput.trigger('input') // v-model not updated
radioInput.trigger('change') // v-model updated const radioInput = wrapper.find('input[type="radio"]')
radioInput.element.checked = true // input element value is changed, v-model is not
radioInput.trigger('change') // v-model updated wrapper.findAll('option').at(1).element.selected = true
wrapper.find('select').trigger('change') There is an open issue to add helper methods to make this easier: #530 I'm closing this issue in favor of that feature request. |
It would be nice if all of these examples were in the documentation |
There's a PR that will add methods to make this simpler—#557 |
Okay. I also have one question. Is there a way to test that user can't change value in a disabled input? For example const wrapper = mount({
data: () => ({ val: '' }),
template: '<input v-model="val" disabled />'
});
wrapper.find('input').element.value = 'testing';
wrapper.find('input').trigger('input');
expect(wrapper.vm.val).toEqual('');
expect(wrapper.vm.val).not.toEqual('testing'); Or it's a browser's work - prevent input on a disabled input? |
I believe JSDom supported |
Oh, I made a mistake. Snippet above does work. But if I change |
Readonly does not prevent events from triggering, or JavaScript from filling the value. You can research |
I want to be able to test my Single File Vue Component by selecting an option in a Vue Child component. I need to trigger the Vue watcher that watches
selectedFooItem
. I am usingmocha-webpack
andjs-global
according to thevue-test-utils
docs.This is related to this issue: #128 , and my SO ticket. I also tried going the chat route but Discord tells me I don't have permission to send messages in that channel in vue-land.js.org.
I'm testing a Vue 2 component that has an
el-select
child component from ElementIO . The main difference in my example is that there is a child Vue component as opposed to HTML elements (i.e. can't rely on.find("select")
. It looks like this:What doesn't work:
1.
I see
onClick
being called twice (one fromel-option
). I think this refers to the event propagation behavior the issue I referenced was talking about. But, my watcher does not detect changes.Still, watcher does not detect changes.
My work around
Right now I'm resorting to
or
The text was updated successfully, but these errors were encountered: