'How to mock a quasar component

This is my code for a quasar component that I want to mock

emits: [
"buyer-protection",
...useDialogPluginComponent.emits
]

But I get the following error:

TypeError: _quasar.useDialogPluginComponent.emits is not iterable

I'd like to mock useQuasar and usePluginDialogComponent from the quasar module. I tried to mock them this way:

jest.mock('quasar', () => ({
  useDialogPluginComponent: () =>  ({
    emits: []
  }),
  useQuasar: () => ({
    platform: {
      is: {
        desktop: true
      }
    }
  })
}))

How can I mock these quasar components?



Solution 1:[1]

I am not sure why useDialogPluginComponent is both a function instance and object instance. As a workaround, I have defined a function and assigned emits object to it.

jest.mock("quasar", () => {
  let t1 = () => {
    return {
      dialogRef: {},
      onDialogHide: () => {},
      onDialogOK: () => {},
      onDialogCancel: () => {}
    }      
  };
  t1.emits = ['ok', 'hide'];
  return {
    useQuasar: () => ({
      platform: {
        is: {
          Mobile: true,
        },
      },
    }),
    useDialogPluginComponent: t1
  }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nirmal patel