在React模块中测试SweetAlert2的preConfirm挂钩

亚历克斯

具有这样的React模块名称Modal

import withReactContent from 'sweetalert2-react-content'

export const Modal = withReactContent(Swal)
const showModal = props => {
  return Modal.fire({
    ...props,
    showCloseButton: true
  })
}

export default showModal

在另一个组件中将其用作用户操作的确认框

export const renderDeployModal = (deploymentId) => {
  console.log(' - renderDeployModal - ')
  Modal.fire({
    type: 'question',
    text: `Are you sure you wish to re-deploy this (${deploymentId})?`,
    showCancelButton: true,
    confirmButtonText: 'Deploy',
    preConfirm: () => {
      console.log(' - preConfirm - ')
      return apiRequest(`/deployments/${deploymentId}/trigger`, {}, 'POST')
        .then(response => {
          return response.body
        })
        .catch(response => {
          Modal.showValidationMessage(response.message)
        })
    },
    allowOutsideClick: () => !Modal.isLoading()
  }).then(result => {
    if (result.value) {
      notify('success', 'Your deployment has triggered.')
    }
  })
}

实现是可行的,但是我受困的是测试在preConfirm钩子中执行的逻辑,因为我无法找到任何Modal.clickConfirm()在测试中手动触发并实际工作的方法

import * as mockModal from '../../modal'
jest.mock('../../modal')

describe('renderDeployModal', () => {

  it('fails to run a deploy without deploymentId argument', async () => {
    const Modal = mockModal.Modal.mockImplementationOnce()

    Modal.fire.mockImplementationOnce(() => Promise.resolve({ value: false }))
    Modal.clickConfirm = jest.fn()

    // Modal.clickConfirm.mockImplementation(() => Promise.resolve())
    // const spy = jest.spyOn(mockModal.Modal, 'clickConfirm')

    apiRequest.default = jest.fn().mockReturnValue(Promise.reject(new Error('foo')))
    await renderDeployModal(null)
    await Promise.resolve()

    Modal.clickConfirm()
    await Promise.resolve()

    expect(Modal.fire).toHaveBeenCalled()
    expect(Modal.clickConfirm).toHaveBeenCalled()
    expect(apiRequest.default).toHaveBeenCalledWith(`/deployments/null/trigger`, {}, 'POST')
  })

上述测试在预期的最后一次失败apiRequest

  console.log src/actions.js:111
     - renderDeployModal -
 FAIL  src/actions.test.js
...
    ✕ fails to run a deploy without deploymentId argument (56ms)

  ● renderDeployModal › fails to run a deploy without deploymentId argument

    expect(jest.fn()).toHaveBeenCalledWith(expected)

    Expected mock function to have been called with:
      ["/deployments/null/trigger", {}, "POST"]
    But it was not called.

      148 |     expect(Modal.fire).toHaveBeenCalled()
      149 |     expect(Modal.clickConfirm).toHaveBeenCalled()
    > 150 |     expect(apiRequest.default).toHaveBeenCalledWith(`/deployments/null/trigger`, {}, 'POST')
          |                                ^

console.log(' - renderDeployModal - ')显示,但console.log(' - preConfirm - ')显示,表示Modal.clickConfirm()未正确触发。

我在这里想念什么?我没有办法尝试(好的或坏的)。

呼吸

每当您模拟模块时

jest.mock('../../modal')

它为模块对象对象的每个属性创建模拟函数,而无需执行。

因此,您要将一个对象传递给Modal.fire()具有回调的preConfirm,但没有任何东西可以调用它。因此,您应该将模拟实现更改为:

Modal.fire.mockImplementationOnce(({ preConfirm }) => {
  preConfirm(); // <- execute the given callback
  return Promise.resolve({ value: false })
})

然后期望它被调用


旁注:顺便说一句

test('if I call a function it`s actually being called', () => {
    Modal.clickConfirm() // <- execute a function within the test
    expect(Modal.clickConfirm).toHaveBeenCalled() // and make sure it have been called few lines below
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

未捕获(承诺中)使用SweetAlert2取消

来自分类Dev

如何在SweetAlert2中使用v-for

来自分类Dev

如何在sweetalert2中显示变量

来自分类Dev

如何在sweetalert2中隐藏按钮的边框

来自分类Dev

链接sweetalert2模态

来自分类Dev

如何让水豚镀铬无头打开sweetalert2模态进行Rspec测试

来自分类Dev

Sweetalert2不返回true / false

来自分类Dev

无法让 sweetalert2 ajax 工作

来自分类Dev

我如何使整个确认按钮成为Laravel控制器中sweetalert2中的链接

来自分类Dev

如何从按钮中删除类并在sweetalert2中添加自定义类?

来自分类Dev

sweetalert2无法调用Laravel中的Confirm POST Submit按钮

来自分类Dev

将文件对象保存到文件中 - sweetalert2 - javascript - php

来自分类Dev

SweetAlert2 不工作并在 IE11 中停止

来自分类Dev

使用 SweetAlert2,我如何使用 Toast 警报在单独的行中获取标题和文本?

来自分类Dev

sweetalert2具有相同功能的多个swal

来自分类Dev

使用angular和sweetalert2的剑道网格删除方法

来自分类Dev

Sweetalert2:如何将textarea放入ajax请求

来自分类Dev

Sweetalert2:完全禁用版本> = 9.0.0的动画的正确方法

来自分类Dev

Vue-无法在方法内部调用sweetalert2

来自分类Dev

Sweetalert2 textarea输入显示在电子邮件上

来自分类Dev

使用SweetAlert2作为页面加载时的弹出窗口

来自分类Dev

如何结合 aurelia-materialize-bridge 和 sweetalert2

来自分类Dev

如何将 Datepicker 带到 SweetAlert2 的前面?

来自分类Dev

在 Vue js 中使用 Sweetalert2 删除确认

来自分类Dev

如何检索发送回 SweetAlert2 Ajax 调用的数据?

来自分类Dev

使用 sweetalert2 进行软删除的正确方法

来自分类Dev

在Angular 2中测试ngOnChanges生命周期挂钩

来自分类Dev

在Angle 2测试中伪造模块

来自分类Dev

react-query如何在Jest测试中从自定义useMutation挂钩调用mutate

Related 相关文章

  1. 1

    未捕获(承诺中)使用SweetAlert2取消

  2. 2

    如何在SweetAlert2中使用v-for

  3. 3

    如何在sweetalert2中显示变量

  4. 4

    如何在sweetalert2中隐藏按钮的边框

  5. 5

    链接sweetalert2模态

  6. 6

    如何让水豚镀铬无头打开sweetalert2模态进行Rspec测试

  7. 7

    Sweetalert2不返回true / false

  8. 8

    无法让 sweetalert2 ajax 工作

  9. 9

    我如何使整个确认按钮成为Laravel控制器中sweetalert2中的链接

  10. 10

    如何从按钮中删除类并在sweetalert2中添加自定义类?

  11. 11

    sweetalert2无法调用Laravel中的Confirm POST Submit按钮

  12. 12

    将文件对象保存到文件中 - sweetalert2 - javascript - php

  13. 13

    SweetAlert2 不工作并在 IE11 中停止

  14. 14

    使用 SweetAlert2,我如何使用 Toast 警报在单独的行中获取标题和文本?

  15. 15

    sweetalert2具有相同功能的多个swal

  16. 16

    使用angular和sweetalert2的剑道网格删除方法

  17. 17

    Sweetalert2:如何将textarea放入ajax请求

  18. 18

    Sweetalert2:完全禁用版本> = 9.0.0的动画的正确方法

  19. 19

    Vue-无法在方法内部调用sweetalert2

  20. 20

    Sweetalert2 textarea输入显示在电子邮件上

  21. 21

    使用SweetAlert2作为页面加载时的弹出窗口

  22. 22

    如何结合 aurelia-materialize-bridge 和 sweetalert2

  23. 23

    如何将 Datepicker 带到 SweetAlert2 的前面?

  24. 24

    在 Vue js 中使用 Sweetalert2 删除确认

  25. 25

    如何检索发送回 SweetAlert2 Ajax 调用的数据?

  26. 26

    使用 sweetalert2 进行软删除的正确方法

  27. 27

    在Angular 2中测试ngOnChanges生命周期挂钩

  28. 28

    在Angle 2测试中伪造模块

  29. 29

    react-query如何在Jest测试中从自定义useMutation挂钩调用mutate

热门标签

归档