首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@ testing -library/vue ::testing使用去反跳在VueJS组件上发出事件

@ testing -library/vue ::testing使用去反跳在VueJS组件上发出事件
EN

Stack Overflow用户
提问于 2021-10-06 14:20:15
回答 1查看 187关注 0票数 0

我正在尝试通过一个输入字段测试一个发出的事件,它有一个在更新方法上去跳的方法。

没有去抖动,测试通过了,没有问题。

这是一段代码。

代码语言:javascript
复制
import { render } from '@testing-library/vue';
import userEvent from '@testing-library/user-event';
import debounce from 'lodash.debounce';

const template = {
  template: '<input v-model="searchText" @input="update" type="search" />',
  data() {
    return {
      searchText: '',
    };
  },
  methods: {
    update: debounce(function (e) {
      this.searchText = e.target.value;
      if (this.searchText.length >= 3) this.$emit('update-items', this.searchText);
    }, 300),
  },
};

it('should emit [update-items] event if the query length typed on search input field is equal os greater than three', async () => {
  // * Arrange

  // * Act
  const { container, emitted } = await render(template);

  const el = container.querySelector('input');
  await userEvent.type(el, 'abcd');

  // * Assert
  expect(emitted()).toHaveProperty('update-items'); // ! FAIL
});

原件在这里:https://gist.github.com/vicainelli/cf614ef7d7967684ebab6cae8290033e

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-11 17:27:04

我想出来了,我不得不嘲笑lodash.debounce的依赖

代码语言:javascript
复制
jest.mock('lodash.debounce', () => jest.fn((fn) => fn));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69467506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档