-
Notifications
You must be signed in to change notification settings - Fork 669
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
feat: Add setValue method #557
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great so far :)
Things we need:
- setValue added to flow/wrapper.flow.js
- A setValue type added to spackages/test-utils/types/index.d.ts and a test in packages/test-utils/types/wrapper.ts
I also think we should throw an error if setValue is called on an element that it won't affect. I think it affects most of the text content input types, like text
, email
. You can see a full list here—https://html.spec.whatwg.org/multipage/input.html#attr-input-type-keywords.
Could you add tests for each of the text control elements and confirm that setValue
does set the v-model value for each type. Then can you write a test and throw an error if setValue
is called on an element that isn't a text control.
Thanks 😀
I checked that v-model works on every input and confirmed it looking in v-model source code (https://github.com/vuejs/vue/blob/550c3c0d14af5485bb7e507c504664a7136e9bf9/src/platforms/web/compiler/directives/model.js) I used the same rules that v-model uses to determine the input type, in order to check if the method was valid. Errors and suggestions are thrown if invalid. Im having difficulties understanding what to test in packages/test-utils/types/wrapper.ts Will continue with setSelected. Thanks for reviewing |
packages/test-utils/src/wrapper.js
Outdated
const type = this.attributes().type | ||
const event = 'input' | ||
|
||
if (this.isVueComponent) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this check here? I don't think the function needs to behave differently if the wrapper contains a vue instance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its unnecesary, will remove
packages/test-utils/src/wrapper.js
Outdated
const event = 'change' | ||
|
||
if (this.isVueComponent) { | ||
throwError('wrapper.setChecked() cannot be called on component. Use wrapper.setValue() instead') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A component instance still has a root element, which can have its value set, so I think we should let setChecked work for vue instance wrappers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern is related to how v-model behaves on components, but Im not sure. Let me know your final decision
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The v-model would be set on the root element, so if you have a component like this:
<template>
<input type="text" />
</template>
And you called setValue
, you would set the value of the input:
const wrapper = mount(TestComponent)
wrapper.setValue('some val')
All component instance wrappers have a root element (this.element) so you can keep the current code and it would work correctly on instance wrappers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this check for isVueComponent
The tests are to check the typings are correct. You can just add two lines that call You can see an example for This file is compiled by typescript, so if the typings are incorrect the file won't compile and the type tests will fail. |
I believe its done, except for that mysterious failing compatibility test on checkbox and radio v-model @2.1.10. Any idea on that? Thanks |
@eddyerburgh any idea? |
Sorry for the delay, I must have missed the notifications from this PR. I'm not sure why the tests fail on 2.1.10 without investigating. Have you debugged locally? There might be a different implementation of v-model in 2.1.x |
If you change vue and vue-template-compiler version to 2.1.10 in |
Its solved! Thanks for the help!
That was in fact the problem, on that versions v-model on checkboxes and radios is listening for click events. With some changes now it should work on all versions Anything missing? |
packages/test-utils/src/wrapper.js
Outdated
const event = 'input' | ||
|
||
if (tag === 'SELECT') { | ||
throwError('wrapper.setValue() cannot be called on select') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can tell the user to call setSelected(). Also, can you change the errors to this format:
wrapper.setValue() cannot be called on a <select> element. Use wrapper.setSelected() instead
* Checks radio button or checkbox element | ||
*/ | ||
setChecked (checked: boolean) { | ||
if (typeof checked !== 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should default to true, so you can call setChecked() and it will set the element to checked. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is set to true if no argument is passed. It isn't set on the parameters as a default but it behaves the way you suggest, there is a test for this specific case.
@@ -72,6 +72,10 @@ interface BaseWrapper { | |||
setData (data: object): void | |||
setMethods (data: object): void | |||
setProps (props: object): void | |||
|
|||
setValue (value: any): void | |||
setChecked (checked: boolean): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setSelected is missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ups, sorry. Done
test/specs/wrapper/setValue.spec.js
Outdated
expect(input.element.value).to.equal('foo') | ||
}) | ||
|
||
it('calls trigger with input event', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this test, as long as the dom updates with the v-model, it doesn't matter whether we call trigger or dispatch an event directly.
Good job finding that 2.1.x listens to the click event 👏 I've just realized there aren't any docs pages. Can you add them in this PR? |
Yeah of course, will do! |
Hey @beyersito do you have time to add docs this week? It would be great to get this merged 😄 |
Since there is no reply, I think it is better to close this pull request and someone send new one, or merge this one and someone add document. |
Sorry for the delay, will add docs this week or if someone prefers to add docs great |
Let's get this merged, I can add docs tomorrow unless you were about to do so @beyersito ? |
Great, I was thinking about this PR earlier 👍 |
Please do @lmiller1990 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @beyersito and @lmiller1990
The setSelected() method was added in vuejs/vue-test-utils#557, but apparently never documented. It seems stable and intended for end-user use nonetheless, so it is probably just an oversight documentation wise.
The setSelected() method was added in vuejs/vue-test-utils#557, but apparently never documented. It seems stable and intended for end-user use nonetheless, so it is probably just an oversight documentation wise.
#530
First contribution, wanted to know if I am following the correct path.
Its not finished yet.
Thanks