Unit Test a Quasar Dialog



  • Hey guys,

    I would like to write a unit test to check that upon confirmation, a Quasar dialog will emit an event in it’s .then() function.

    Given that this would normally involve clicking the OK button, is there any way to easily accomplish this in a unit test?

    I am using Jest and Vue Test Utils.

    My test so far

    it('will emit signUp when the dialog is confirmed', () => {
        const wrapper = createWrapper()
    
        wrapper.vm.$q.dialog.call()
    
        // What's the best way to trigger the 'ok' event from here?
    
        expect(wrapper.emitted().signUp).toHaveLength(1)
      })
    

    Thanks a lot.



  • Just in case this helps anyone, it’s easy enough to simply replace the $q.dialog function with a mock function that returns a Promise.resolve().

    Be sure to handle the promise asynchronously

    import flushPromises from 'flush-promises'
    
    it('will emit signUp when the dialog is confirmed', async () => {
        const wrapper = createWrapper()
    
        wrapper.vm.$q.dialog = jest.fn(() => Promise.resolve())
    
    await flushPromises()
    
        expect(wrapper.emitted()).toHaveProperty('signUp)
      })