如何使用开玩笑/酶测试resizeObserver回调函数

发布于 2025-02-06 10:11:46 字数 760 浏览 1 评论 0原文

我有一个使用resizeObserver显示/隐藏某些样式的组件。

样式的切换正在函数中发生,将由resizeObserver回调触发。

component.tsx

  private toggleStyles() {
        const applyStyles = some condition here...
        this.setState({ applyStyles });
  }

  private observeResize = () => {
        this.resizeObserver = new ResizeObserver(() => this.toggleStyles());
        this.resizeObserver.observe(element to observe here..);
    }

    componentDidMount() {
        this.observeResize();
    }

    componentWillUnmount() {
        if (this.resizeObserver) {
            this.resizeObserver.disconnect();
        }
    }

我想测试resizeObserver回调,该回调会触发togglestyles函数,但是我不确定如何在元素本身上触发调整大小。

I have a component that uses the ResizeObserver to show/hide some styles.

the toggling of the styles is happening in a function the will be triggered by the ResizeObserver callback.

component.tsx

  private toggleStyles() {
        const applyStyles = some condition here...
        this.setState({ applyStyles });
  }

  private observeResize = () => {
        this.resizeObserver = new ResizeObserver(() => this.toggleStyles());
        this.resizeObserver.observe(element to observe here..);
    }

    componentDidMount() {
        this.observeResize();
    }

    componentWillUnmount() {
        if (this.resizeObserver) {
            this.resizeObserver.disconnect();
        }
    }

I want to test the ResizeObserver callback that will trigger the toggleStyles function, but I'm not sure how can I trigger the resize on the element itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文