【问题标题】:AnimationEvent is not defined in JestJest 中未定义 AnimationEvent
【发布时间】:2020-08-04 13:35:28
【问题描述】:

我正在尝试为我在 Angular 应用程序中的一项测试模拟 animationEvent,但出现错误:

ReferenceError: AnimationEvent 未定义。

我假设这是一个实验性功能,它不会在 Jest 中的 jsdom 中可用。有谁知道模拟这个的方法吗?

这是失败的代码

dispatchEvent(new AnimationEvent("animationend", { animationName: "slide-down" }));

有谁知道如何解决这个问题?

此事件将触发我的组件中的一些代码,并会自己发出一个输出事件,但我想确保当这种带有animationName 'slide-down' 的特定类型的事件发生时会发生这种情况。

【问题讨论】:

    标签: javascript jestjs css-animations


    【解决方案1】:

    好的,所以我设法做到了。 我正在使用一个普通事件,然后使用我需要的动画名称进行更新。

    const animationEventMixin = (animationName: string) => ({ animationName });
    
    ...
    
    const event = new Event("animationend");
    Object.assign(event, animationEventMixin("slide-down"));
    dispatchEvent(event);
    

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 2017-06-10
      • 2019-08-26
      • 2021-03-02
      • 2022-11-17
      • 2022-12-31
      • 1970-01-01
      • 2018-06-01
      • 2021-02-01
      相关资源
      最近更新 更多