-
Notifications
You must be signed in to change notification settings - Fork 258
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
docs: click event without change event does not update checkbox value without attachTo: document.body
#1470
Comments
Hey @cefn I think this is expected. If I remember correctly, if you want the same behavior as in VTU v1, you need to add |
Thanks, @cexbrayat that's exactly what I needed to know. With the extra mount option passed, the test passes and a change event is issued. The commit that fixes it is cefn/vite-reactive-repro@20c1791 |
Does anyone know of any documentation indicating this change? Seems like a surprising and major issue not to describe if it means core eventing fails for all DOM elements. The issue is not mentioned in the migration guide at the time of writing (linked from https://github.com/vuejs/test-utils/ ). Having read the note in the migration guide I am reopening since probably this should be documented...
Also does test state (DOM state) have to be managed more carefully on the new model, to prevent unexpected interactions and dependencies? The literal |
Sure, if you're willing to, a PR would be most welcome to update the migration guide and mention this edge case 👍 To answer your other question, I think the proper way is to use |
attachTo: document.body
Thanks @cexbrayat I wouldn't go down the According to that philosophy, (in my view), manipulating the internal state of the checkbox is too close a coupling - it doesn't actually prove that clicking works. What if someone fumbled preventDefault in click handling somewhere? For purity we should probably be issuing an event that clicks on the input in the document having label 'X' as a user would do, and not even making assumptions about which input reference it is. I'm not well-informed enough to update the documentation without knowing what the previous behaviour was for mount (without I'm assuming it's too late to reconsider the default for |
VTU is a low-level library. So it's expected to have to trigger some updates manually. I don't think we should change the default of attachTo. But we can add a mention in the migration guide to help users that stumble on the same use-case as yours. Something pretty simple summarizing our conversation should be enough. Let's add @afontcu and @lmiller1990 who know VTU v1 better than me, and may have a better insight. |
Agreed. However, the work ahead is migration from Vue2 to Vue3 using existing VTU1 tests ported to VTU2 to prove that the Vue3 migration worked. In the suite we have ~700 uses of |
I think testing-library/vue defaults to I think the default here is the same as Test Utils v1; we create an element (but do not actually attach it, unless you specifically say so). When making VTU v2 we tried to stick as close to the previous behaviour as possible. It should be the same. The reasoning was exactly what you said - making migration from Vue 2 to Vue 3 (and VTU v1 to v2) as seamless as possible. It seems like |
Thanks, @lmiller1990 we can go through adding It sounds like you're saying VTU1 had the same behaviour and that's not what we observe. Rather, we observe hundreds of tests using VTU1 with invocations like I am particularly confused because the use of click without dom attachment is right there in the reference documentation https://v1.test-utils.vuejs.org/api/wrapper/trigger.html There is a comment there about |
hey!
Yep, it attaches to Beyond that, issues with inputs and
if that's the case (and I strongly agree with those statements!) then you should definitely give Vue Testing Library a go, which acts as a wrapper of VTU and provides methods to test at user level |
I should clarify - you don't need |
So for the action to close this, I'd say a small note on this page: https://test-utils.vuejs.org/guide/essentials/event-handling.html would suffice! |
* chore(deps): update all non-major dependencies * chore: remove the lockfile and reinstall to update trnasitive deps The outdated `@types/babel__traverse` package is causing type errors * fix: fix htmlTags type error * fix: pin @types/node to 18.8.0 to work around vuejs/language-tools#1985 * fix: pin @vue/test-utils temporarily to avoid snapshot differences * test: update snapshot As far as I see, all the snapshot differences are merely the newlines after `import` statements * test: add `attachTo: document.body` to make click event take effect See vuejs/test-utils#1470 (comment) * fix: fix mjs processing for webpack 4 Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Haoqun Jiang <[email protected]>
Fixes vuejs#1470 Some events, like clicking on a checkbox to change its `v-model`, will only work if the test uses `attachTo: document.body`. Otherwise, the `change` event will not be triggered, and the `v-model` value does not change.
Fixes #1470 Some events, like clicking on a checkbox to change its `v-model`, will only work if the test uses `attachTo: document.body`. Otherwise, the `change` event will not be triggered, and the `v-model` value does not change.
Describe the bug
A click() on an
input[type="checkbox"]
in@vue/test-utils
doesn't seem conformant with Chrome. It doesn't cause a change to the vm instance's boolean ref, which is bound byv-model
. A watcher of that ref gets no update notification when the checkbox gets a click.The expected behaviour (that 'click' changes the ref, hence triggers a watcher) CAN be proven interactively in Chrome with this component mounted in a Vite app. It can be proven by either clicking the input or calling
.click()
on the element from the console. In the Vite-hosted App.vue theselectionHandler
watcher of the bound boolean is simplyconsole.log
and behaves as expected - reporting the boolean value on every click.To Reproduce
See the repro at cefn/vite-reactive-repro#3 based off the automated Vue3+Typescript scaffolding created by an out-of-the-box Vite project. You can observe the correct behaviour in Chrome by running
pnpm install
thenpnpm run dev
and experiment interactively in the browser. You can observe the broken@vue/test-utils
behaviour by runningpnpm run test
.Expected behavior
I expected the
@vue/test-utils
test at bad0970 to pass. I expected to be able to call justawait containerCheckbox.trigger("click")
in order for a v-model bound boolean variable change to propagate to the watcher. I didn't expect to need to explicitly callawait containerCheckbox.trigger("change")
to double-announce the change.The missing
change
event is likely a regression as I discovered this when migrating components and their tests from Vue2, so it could be an obstacle for those upgrading and who assumeclick
will causechange
.We need to port lots of components and having
@vue/test-utils
align with the behaviour of the previous version will help us with migration.Related information:
@vue/test-utils
version: 2.0.0-rc.21Vue
version: 3.2.31node
version: v16.14.2npm
(oryarn
) version:pnpm --version is 6.32.10
The text was updated successfully, but these errors were encountered: