No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Trigger events on QCheckbox using Jest & Vue Test Utils?

    Help
    1
    2
    1530
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Jezzta667
      Jezzta667 last edited by

      Hi guys,

      I recently asked this same question regarding the QDatetime but I thought I would re-ask in the context of the QCheckbox because it SHOULD be more straight forward.

      When using Vue Test Utils and Jest, I am not able to use the setChecked() method to set the value of the checkbox, even when I can clearly select the input checkbox in the DOM

      <QCheckbox
          v-model="accept"
          @input="updateAccept"
          data-test="checkbox"
      />
      
      it('will check the checkbox and change the value', () => {
      
          expect(wrapper.vm.accept).toBe(false)
          
          const checkbox = wrapper.find('[data-test="checkbox"]')
          // CONSOLE OUTPUT:
          // wrapper.setChecked() cannot be called on this element
      
          const checkbox = wrapper.find(QCheckbox)
          // CONSOLE OUTPUT:
          // wrapper.find() must be passed a valid CSS selector, 
          // Vue constructor, or valid find option object
      
           const checkbox = wrapper.find('input[type="checkbox"]')
          // This finds the checkbox
          
          checkbox.setChecked(true)
          // This has no effect, even on checkbox.element.checked
      
          checkbox.element.checked = true
          checkbox.trigger('click')
          checkbox.trigger('change')
          // setChecked is supposedly the equivalent of these
          // commands, however these do at least change the value
          // of checkbox.element.checked
      
          expect(wrapper.vm.accept).toBe(true)
          // FAILS 
        })
      

      Anyway, I really just need someone to explain how to emit the events from Quasar components, if possible.

      Cheers

      1 Reply Last reply Reply Quote 0
      • Jezzta667
        Jezzta667 last edited by Jezzta667

        Figured it out by attaching a ‘ref’ attribute which returns the Vue instance of that DOM element (otherwise returns the DOM element if it’s a regular element).

        I then emitted the correct event from the vm of the QCheckbox through that.

        <QCheckbox
            v-model="accept"
            @input="updateAcceptTerms"
            ref="checkbox"
        />
        
        it('will change and update the value', () => {
            expect(wrapper.vm.accept).toBe(false)
        
            const checkbox = wrapper.vm.$refs.checkbox
            checkbox.$emit('input', true)
        
            expect(wrapper.vm.accept).toBe(true) // Passes
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post