4

Trying to work out something that I thought would be pretty simple!

I have a PrimeVue checkbox component on my page (and I'm using Vue 3, PrimeVue, vitest, @testing-library/vue and @testing-library/user-event)

<div class="field-checkbox">
    <Checkbox role = "checkbox" :aria-label="`Select all students in class ${props.className}`" @change="selectAllChecked()" v-model="selectAll" :binary="true" />
    <label>Select All</label>
</div>

I just want to write a test to check the checkbox and then test that it is correctly checked - not a particularly meaningful test but I'm new to these libraries (and unit testing) so want to work out how to do this stuff.

In my test I have:

it("checkbox test", async() => {
    const ariaLabel = `Select all students in class ${mockProps.className}`;
    const checkbox = screen.getByRole("checkbox", { name: ariaLabel });
    await userEvent.click(checkbox);
    expect(checkbox.getAttribute('checked')).toBe(true)
});

if I remove the 3rd and 4th lines, the test passes so it seems to be checking that the checkbox is there ok.

Lines 3 and 4 (I hoped) would allow me to confirm that the checkbox is now checked but my test fails with

AssertionError: expected null to be true // Object.is equality
  - Expected   "true"
  + Received   "null"

on the 4th line

4
  • Can you share the frontend code? Commented Aug 31, 2022 at 19:25
  • I've edited the post with the checkbox markup. Like I said the test passes ok in terms of finding the checkbox, it's just the clicking it and checking it's value I'm having trouble with. The Checkbox is a PrimeVue Checkbox component - I'm wondering now if that's the issue? Commented Sep 1, 2022 at 7:24
  • One issue I've had in general with checkboxes and other inputs using Vuetify and Cypress, is that if I add a testid to it and try to click it directly like you do, then it doesn't work. I'm not entirely familiar with @testing-library/vue, but is there anyway you could try clicking the parent of that? Short explanation on the problem in Cypress + Vuetify can be found here, not sure if it helps Commented Sep 1, 2022 at 7:43
  • 1
    Thanks, I think I understand what I'm doing wrong, I'm not injecting the PrimeVue plugin to the test and also specifying the CheckBox component in the components: I will do some more tests and see how I get on Commented Sep 1, 2022 at 9:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.