Jest.js 如何模拟类星体成分

r3i60tvu  于 2022-12-08  发布在  Jest
关注(0)|答案(2)|浏览(232)

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?

rsaldnfx

rsaldnfx1#

我也试着模仿useDialogPluginComponent,但是我意识到installQuasarPlugin()会处理所有的Quasar插件,所以我不需要自己模仿任何插件。
但是它必须通过挂载来创建示例,不要使用shallowMount,也就是确保dialogRef = vue.ref(null)会捕获q-dialog。
对于我的例子,我想检查'onDialogOK'是否被调用,但是我不能通过spyOn跟踪'onDialogOK',所以我检查发出的wrapper.vm

expect(vueWrapper.emitted()).toHaveProperty('ok');

这很管用。

6tqwzwtp

6tqwzwtp2#

我不确定为什么useDialogPluginComponent既是函数示例又是对象示例。作为一种解决方法,我定义了一个函数,并将emits对象分配给它。

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

相关问题