Vue test utils - setChecked() not updating v-model











up vote
0
down vote

favorite












I am writing unit tests for some components I made at my job. We are using Mocha (TDD) and the Chai assertion library. I have a component with some checkboxes, and using the setChecked() method on them from vue-test-utils is not behaving as expected. I have made a small example that reproduces the error:



TestComponent.vue:



<template>
<div>
<input class="checkboxTest" type="checkbox" v-model="cbVal">
<input class="inputTest" type="text" v-model="textVal">
</div>
</template>

<script>
define(, function() {
return {
data: function() {
return {
cbVal: false,
textVal: ""
}
}
}
})
</script>


test.js:



suite("Random test", function() {
var VueTest;
var TestComponent;

//Import the vue test utils library and TestComponent
suiteSetup(function(done) {
requirejs(
["vue-test-utils", "vuec!components/TestComponent"],
function(VT, TC) {
VueTest = VT;
TestComponent = TC;
done();
}
);
});


//This test passes
test("fill in the input", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".inputTest").setValue("Hello, world!");

assert.equal(wrapper.vm.textVal, "Hello, world!");
});

//This one does not
test("programatically check the box", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".checkboxTest").setChecked(true);

//Prints out AssertionError: expected false to equal true
assert.equal(wrapper.vm.cbVal, true);
});
});


The textVal data member in TestComponent is getting changed, but cbVal is not. Can anyone please explain why setValue() works just fine, but setChecked() does not? Thank you in advance.










share|improve this question


















  • 1




    This looks like a bug, setChecked is intended to update the element and the Vue model. Can you please create a minimal reproduction and create an issue for Vue Test Utils?
    – Edd
    Nov 23 at 17:36















up vote
0
down vote

favorite












I am writing unit tests for some components I made at my job. We are using Mocha (TDD) and the Chai assertion library. I have a component with some checkboxes, and using the setChecked() method on them from vue-test-utils is not behaving as expected. I have made a small example that reproduces the error:



TestComponent.vue:



<template>
<div>
<input class="checkboxTest" type="checkbox" v-model="cbVal">
<input class="inputTest" type="text" v-model="textVal">
</div>
</template>

<script>
define(, function() {
return {
data: function() {
return {
cbVal: false,
textVal: ""
}
}
}
})
</script>


test.js:



suite("Random test", function() {
var VueTest;
var TestComponent;

//Import the vue test utils library and TestComponent
suiteSetup(function(done) {
requirejs(
["vue-test-utils", "vuec!components/TestComponent"],
function(VT, TC) {
VueTest = VT;
TestComponent = TC;
done();
}
);
});


//This test passes
test("fill in the input", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".inputTest").setValue("Hello, world!");

assert.equal(wrapper.vm.textVal, "Hello, world!");
});

//This one does not
test("programatically check the box", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".checkboxTest").setChecked(true);

//Prints out AssertionError: expected false to equal true
assert.equal(wrapper.vm.cbVal, true);
});
});


The textVal data member in TestComponent is getting changed, but cbVal is not. Can anyone please explain why setValue() works just fine, but setChecked() does not? Thank you in advance.










share|improve this question


















  • 1




    This looks like a bug, setChecked is intended to update the element and the Vue model. Can you please create a minimal reproduction and create an issue for Vue Test Utils?
    – Edd
    Nov 23 at 17:36













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am writing unit tests for some components I made at my job. We are using Mocha (TDD) and the Chai assertion library. I have a component with some checkboxes, and using the setChecked() method on them from vue-test-utils is not behaving as expected. I have made a small example that reproduces the error:



TestComponent.vue:



<template>
<div>
<input class="checkboxTest" type="checkbox" v-model="cbVal">
<input class="inputTest" type="text" v-model="textVal">
</div>
</template>

<script>
define(, function() {
return {
data: function() {
return {
cbVal: false,
textVal: ""
}
}
}
})
</script>


test.js:



suite("Random test", function() {
var VueTest;
var TestComponent;

//Import the vue test utils library and TestComponent
suiteSetup(function(done) {
requirejs(
["vue-test-utils", "vuec!components/TestComponent"],
function(VT, TC) {
VueTest = VT;
TestComponent = TC;
done();
}
);
});


//This test passes
test("fill in the input", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".inputTest").setValue("Hello, world!");

assert.equal(wrapper.vm.textVal, "Hello, world!");
});

//This one does not
test("programatically check the box", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".checkboxTest").setChecked(true);

//Prints out AssertionError: expected false to equal true
assert.equal(wrapper.vm.cbVal, true);
});
});


The textVal data member in TestComponent is getting changed, but cbVal is not. Can anyone please explain why setValue() works just fine, but setChecked() does not? Thank you in advance.










share|improve this question













I am writing unit tests for some components I made at my job. We are using Mocha (TDD) and the Chai assertion library. I have a component with some checkboxes, and using the setChecked() method on them from vue-test-utils is not behaving as expected. I have made a small example that reproduces the error:



TestComponent.vue:



<template>
<div>
<input class="checkboxTest" type="checkbox" v-model="cbVal">
<input class="inputTest" type="text" v-model="textVal">
</div>
</template>

<script>
define(, function() {
return {
data: function() {
return {
cbVal: false,
textVal: ""
}
}
}
})
</script>


test.js:



suite("Random test", function() {
var VueTest;
var TestComponent;

//Import the vue test utils library and TestComponent
suiteSetup(function(done) {
requirejs(
["vue-test-utils", "vuec!components/TestComponent"],
function(VT, TC) {
VueTest = VT;
TestComponent = TC;
done();
}
);
});


//This test passes
test("fill in the input", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".inputTest").setValue("Hello, world!");

assert.equal(wrapper.vm.textVal, "Hello, world!");
});

//This one does not
test("programatically check the box", function() {
var wrapper = VueTest.mount(TestComponent);
wrapper.find(".checkboxTest").setChecked(true);

//Prints out AssertionError: expected false to equal true
assert.equal(wrapper.vm.cbVal, true);
});
});


The textVal data member in TestComponent is getting changed, but cbVal is not. Can anyone please explain why setValue() works just fine, but setChecked() does not? Thank you in advance.







vue.js vue-test-utils






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 15:05









Brett Fisher

42115




42115








  • 1




    This looks like a bug, setChecked is intended to update the element and the Vue model. Can you please create a minimal reproduction and create an issue for Vue Test Utils?
    – Edd
    Nov 23 at 17:36














  • 1




    This looks like a bug, setChecked is intended to update the element and the Vue model. Can you please create a minimal reproduction and create an issue for Vue Test Utils?
    – Edd
    Nov 23 at 17:36








1




1




This looks like a bug, setChecked is intended to update the element and the Vue model. Can you please create a minimal reproduction and create an issue for Vue Test Utils?
– Edd
Nov 23 at 17:36




This looks like a bug, setChecked is intended to update the element and the Vue model. Can you please create a minimal reproduction and create an issue for Vue Test Utils?
– Edd
Nov 23 at 17:36












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I can't answer why it doesn't work, but I can tell you your approach is incorrect in the first place.



You shouldn't be interacting with the html elements directly to set their values. When you set vue-model to cbVal you should instead be interacting with cbVal.



In other words, change your code from setChecked() to cbVal = true in order for it to comply with how Vue wants you to develop your project. There's no guarantee Vue can remain dynamic and reactive if you don't interact with your code the way Vue wants you to.






share|improve this answer





















  • Yeah, I realized that what I had to do was something like var cb=wrapper.find(".checkboxTest"), then cb.trigger("click"). This changed the variable in v-model. Still not sure what setChecked() is doing internally.
    – Brett Fisher
    Nov 14 at 21:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283893%2fvue-test-utils-setchecked-not-updating-v-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










I can't answer why it doesn't work, but I can tell you your approach is incorrect in the first place.



You shouldn't be interacting with the html elements directly to set their values. When you set vue-model to cbVal you should instead be interacting with cbVal.



In other words, change your code from setChecked() to cbVal = true in order for it to comply with how Vue wants you to develop your project. There's no guarantee Vue can remain dynamic and reactive if you don't interact with your code the way Vue wants you to.






share|improve this answer





















  • Yeah, I realized that what I had to do was something like var cb=wrapper.find(".checkboxTest"), then cb.trigger("click"). This changed the variable in v-model. Still not sure what setChecked() is doing internally.
    – Brett Fisher
    Nov 14 at 21:20















up vote
1
down vote



accepted










I can't answer why it doesn't work, but I can tell you your approach is incorrect in the first place.



You shouldn't be interacting with the html elements directly to set their values. When you set vue-model to cbVal you should instead be interacting with cbVal.



In other words, change your code from setChecked() to cbVal = true in order for it to comply with how Vue wants you to develop your project. There's no guarantee Vue can remain dynamic and reactive if you don't interact with your code the way Vue wants you to.






share|improve this answer





















  • Yeah, I realized that what I had to do was something like var cb=wrapper.find(".checkboxTest"), then cb.trigger("click"). This changed the variable in v-model. Still not sure what setChecked() is doing internally.
    – Brett Fisher
    Nov 14 at 21:20













up vote
1
down vote



accepted







up vote
1
down vote



accepted






I can't answer why it doesn't work, but I can tell you your approach is incorrect in the first place.



You shouldn't be interacting with the html elements directly to set their values. When you set vue-model to cbVal you should instead be interacting with cbVal.



In other words, change your code from setChecked() to cbVal = true in order for it to comply with how Vue wants you to develop your project. There's no guarantee Vue can remain dynamic and reactive if you don't interact with your code the way Vue wants you to.






share|improve this answer












I can't answer why it doesn't work, but I can tell you your approach is incorrect in the first place.



You shouldn't be interacting with the html elements directly to set their values. When you set vue-model to cbVal you should instead be interacting with cbVal.



In other words, change your code from setChecked() to cbVal = true in order for it to comply with how Vue wants you to develop your project. There's no guarantee Vue can remain dynamic and reactive if you don't interact with your code the way Vue wants you to.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 at 6:05









Simon Hyll

9481922




9481922












  • Yeah, I realized that what I had to do was something like var cb=wrapper.find(".checkboxTest"), then cb.trigger("click"). This changed the variable in v-model. Still not sure what setChecked() is doing internally.
    – Brett Fisher
    Nov 14 at 21:20


















  • Yeah, I realized that what I had to do was something like var cb=wrapper.find(".checkboxTest"), then cb.trigger("click"). This changed the variable in v-model. Still not sure what setChecked() is doing internally.
    – Brett Fisher
    Nov 14 at 21:20
















Yeah, I realized that what I had to do was something like var cb=wrapper.find(".checkboxTest"), then cb.trigger("click"). This changed the variable in v-model. Still not sure what setChecked() is doing internally.
– Brett Fisher
Nov 14 at 21:20




Yeah, I realized that what I had to do was something like var cb=wrapper.find(".checkboxTest"), then cb.trigger("click"). This changed the variable in v-model. Still not sure what setChecked() is doing internally.
– Brett Fisher
Nov 14 at 21:20


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283893%2fvue-test-utils-setchecked-not-updating-v-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?