概述

最近玩 Jest,测试 Vue 组件上的事件,有一些心得,记录下来供以后开发时参考,相信对其他人也有用。

事件测试

对于 Vue 组件上的事件,分为 2 种,一种是子组件 Emit 的事件,另一种是插件的事件回调

子组件 emit 的事件

对于子组件 Emit 的事件,我们使用 Jest mock 这个子组件,然后使用 Vue-Test-Util 提供的方法,模拟 emit 事件即可,示例如下:

// ChildComponent
export default {
name: 'ChildComponent',
}; // ParentComponent
<template>
<div>
<child-component @custom="onCustom" />
<p v-if="emitted">Emitted!</p>
</div>
</template> <script>
import ChildComponent from './ChildComponent'; export default {
name: 'ParentComponent',
components: { ChildComponent },
data() {
return {
emitted: false,
};
},
methods: {
onCustom() {
this.emitted = true;
},
},
};
</script> // test.js
import { shallowMount } from '@vue/test-utils';
import ChildComponent from './helper/ChildComponent';
import ParentComponent from './helper/ParentComponent.vue'; describe('emit custom event', () => {
beforeEach(() => {
jest.resetAllMocks();
}); it("displays 'Emitted!' when custom event is emitted", () => {
const wrapper = shallowMount(ParentComponent);
wrapper.find(ChildComponent).vm.$emit('custom');
expect(wrapper.vm.emitted).toBeTruthy();
expect(wrapper.html()).toContain('Emitted!');
});
});

插件的事件回调

有些插件会提供事件回调,比如 sortable.js 就提供这样的事件回调:

var sortable = new Sortable(el, {
group: "name",
sort: true,
onSort: function (/**Event*/evt) {
// 事件回调
},
});

这个时候我们只需要用 Jest mock 这个插件,然后直接手动执行这个事件绑定的回调函数即可。因为 Jest 代理了这个插件,所以实际上这个插件并没有真正被引入,所以所有这个插件执行的操作都需要我们手动调用或者模拟执行。实例如下:

// module.js
function ChildModule(options) {}
export default ChildModule; // ParentComponent
<template>
<div>
<child-component @custom="onCustom" />
<p v-if="called">Called!</p>
</div>
</template> <script>
import ChildModule from './ChildModule'; export default {
name: 'ParentComponent',
data() {
return {
called: false,
};
},
created() {
this.childModule = new ChildModule({
callback: () => {
this.called = true;
},
});
},
};
</script> // test.js
import { shallowMount } from '@vue/test-utils';
import ChildModule from './helper/ChildModule';
import ParentComponent from './helper/ParentComponent.vue'; jest.mock('./helper/ChildModule'); describe('emit custom event', () => {
beforeEach(() => {
jest.resetAllMocks();
}); it("displays 'Called!' when callback is called", async () => {
const wrapper = shallowMount(ParentComponent);
ChildModule.mock.calls[0][0].callback();
expect(wrapper.vm.called).toBeTruthy;
expect(wrapper.html()).toContain('Called!');
});
});

需要说明的是:我们是通过这段代码执行回调函数的:

ChildModule.mock.calls[0][0].callback();

jest 事件测试的更多相关文章

  1. socket.io稳定性及事件测试

    socket.io测试报告 1.socekt.io能坚持多久 将服务器上的socekt.io代码从早上9:30分开始运行到晚上18点,每100毫秒发送一条数据,数据大概15个字符,同时开启5个连接 结 ...

  2. App测试从入门到精通之交叉事件测试

    交叉事件测试又叫事件或者叫冲突测试.对于正在运行的应用,若进入短信,电话等其他软件响应的情况,不会影响所测试应用,且会保证应用都能正确运行.下面我来看一下关于交叉测试中,我们测试人员需要考虑的一些测试 ...

  3. 使用Jest快照测试api

    你知道什么很烦人吗?API不匹配. 有一天,后台开发人员在没有通知前端开发人员的情况下更改了其中一个api."我们认为dateCreated这个名字比created_at更好,"他 ...

  4. DataGridView 些许事件测试

    原始设计需求:当单元格内容是空白时,鼠标进入之后,显示一些数据 直观的第一感觉必然是用CellClick,细想,如果用户不用鼠标,直接按Tab键切换单元格呢?又或者,用户直接双击涅~ 主要测试的是:  ...

  5. 搭建 Jest+ Enzyme 测试环境

    1.为什么要使用单元测试工具? 因为代码之间的相互调用关系,又希望测试过程单元相互独立,又能正常运行,这就需要我们对被测函数的依赖函数和环境进行mock,在测试数据输入.测试执行和测试结果检查方面存在 ...

  6. jest js 测试框架-简单方便人性化

    1. 安装 yarn global add jest-cli or npm install -g jest-cli 备注:可以安装为依赖不用全局安装 2. 项目代码 a. 项目初始化 yarn ini ...

  7. Unity3D碰撞器事件测试(Rigidbody/Kinematic/Trigger/Collider)

    1.Kinematic和刚体之间的碰撞事件 Unity官方有一个详细的碰撞关系表:http://docs.unity3d.com/Manual/CollidersOverview.html 但其实可以 ...

  8. AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务

    测试应用 1.测试路由 我们需要检测路由是否在运作,是否找到了,或者是404了.我们要确认路由事件触发了,预期的模板是否真的加载了.既然路由会改变页面的地址(URL)和页面内容,我们需要检测路由是否被 ...

  9. 前端测试框架Jest系列教程 -- Asynchronous(测试异步代码)

    写在前面: 在JavaScript代码中,异步运行是很常见的.当你有异步运行的代码时,Jest需要知道它测试的代码何时完成,然后才能继续进行另一个测试.Jest提供了几种方法来处理这个问题. 测试异步 ...

随机推荐

  1. css实现div水平垂直居中

    中秋快到了,祝大家中秋快乐. 平时大家写bug过程中肯定会遇到让div框水平或者垂直居中,然而有时候能居中,有时候不能居中.我把平时遇到的一些方法写出来,如果对你有用,那便是晴天. 1.text-al ...

  2. openlayers之地图测距侧面

    项目背景vue-cli3.0 public下html需要引入文件 <link rel="stylesheet" href="<%= BASE_URL %> ...

  3. iOS 支付 [支付宝、银联、微信]

    这是开头语 前不久做了一个项目,涉及到支付宝和银联支付,支付宝和银联都是业界的老大哥,文档.SDK都是很屌,屌的找不到,屌的看不懂,屌到没朋友(吐槽而已),本文将涉及到的最新可用SDK.文档,以及本人 ...

  4. python程序超时处理 timeout_decorator

    如下两个例子,实现对某个函数的超时处理(其实就是加了一个装饰器timeout): 成功例子: 代码: import time import timeout_decorator @timeout_dec ...

  5. 修改或删除windows上的git用户凭证信息

    参考博客:https://blog.csdn.net/qq_42249896/article/details/85207865

  6. 如何配置SQL Server数据库远程连接

    本地数据库(SQL Server 2012或以上) 连接外网服务器的数据库,外网的服务器端需要做如下配置: 1.首先是要打开 数据的配置管理工具 2.配置相关的客户端协议,开启TCP/IP 3.数据库 ...

  7. Jsoup抓取网页数据完成一个简易的Android新闻APP

    前言:作为一个篮球迷,每天必刷NBA新闻.用了那么多新闻APP,就想自己能不能也做个简易的新闻APP.于是便使用Jsoup抓取了虎扑NBA新闻的数据,完成了一个简易的新闻APP.虽然没什么技术含量,但 ...

  8. python _str_方法

    _str_方法: 使用:如: class Car: def __init__(self,newWheelNum,newColor): self.wheelNum=newWheelNum self.co ...

  9. DataWorks参数配置

    https://help.aliyun.com/document_detail/30281.html?spm=a2c6h.13066369.0.0.7bef69daI5ajKt

  10. Metafile::EmfToWmfBits的使用

    其中涉及到string转换LPCWSTR以及模块绝对路径的调用 #include <iostream> #include <stdio.h> #include <WIND ...