jest 事件测试
概述
最近玩 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 事件测试的更多相关文章
- socket.io稳定性及事件测试
socket.io测试报告 1.socekt.io能坚持多久 将服务器上的socekt.io代码从早上9:30分开始运行到晚上18点,每100毫秒发送一条数据,数据大概15个字符,同时开启5个连接 结 ...
- App测试从入门到精通之交叉事件测试
交叉事件测试又叫事件或者叫冲突测试.对于正在运行的应用,若进入短信,电话等其他软件响应的情况,不会影响所测试应用,且会保证应用都能正确运行.下面我来看一下关于交叉测试中,我们测试人员需要考虑的一些测试 ...
- 使用Jest快照测试api
你知道什么很烦人吗?API不匹配. 有一天,后台开发人员在没有通知前端开发人员的情况下更改了其中一个api."我们认为dateCreated这个名字比created_at更好,"他 ...
- DataGridView 些许事件测试
原始设计需求:当单元格内容是空白时,鼠标进入之后,显示一些数据 直观的第一感觉必然是用CellClick,细想,如果用户不用鼠标,直接按Tab键切换单元格呢?又或者,用户直接双击涅~ 主要测试的是: ...
- 搭建 Jest+ Enzyme 测试环境
1.为什么要使用单元测试工具? 因为代码之间的相互调用关系,又希望测试过程单元相互独立,又能正常运行,这就需要我们对被测函数的依赖函数和环境进行mock,在测试数据输入.测试执行和测试结果检查方面存在 ...
- jest js 测试框架-简单方便人性化
1. 安装 yarn global add jest-cli or npm install -g jest-cli 备注:可以安装为依赖不用全局安装 2. 项目代码 a. 项目初始化 yarn ini ...
- Unity3D碰撞器事件测试(Rigidbody/Kinematic/Trigger/Collider)
1.Kinematic和刚体之间的碰撞事件 Unity官方有一个详细的碰撞关系表:http://docs.unity3d.com/Manual/CollidersOverview.html 但其实可以 ...
- AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务
测试应用 1.测试路由 我们需要检测路由是否在运作,是否找到了,或者是404了.我们要确认路由事件触发了,预期的模板是否真的加载了.既然路由会改变页面的地址(URL)和页面内容,我们需要检测路由是否被 ...
- 前端测试框架Jest系列教程 -- Asynchronous(测试异步代码)
写在前面: 在JavaScript代码中,异步运行是很常见的.当你有异步运行的代码时,Jest需要知道它测试的代码何时完成,然后才能继续进行另一个测试.Jest提供了几种方法来处理这个问题. 测试异步 ...
随机推荐
- jquery-mobile pop
一.弹框 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- 重置Brocade光纤交换机的管理IP地址
1.使用串口登录光纤交换机 使用RS/232 (9针)串口连接线将笔记本连至交换机的串口. 输入以下参数: Bits per second (每秒位数): 9600 Data Bits (数据位): ...
- PAT Basic 1038 统计同成绩学生 (20 分)
本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第 1 行给出不超过 1 的正整数 N,即学生总人数.随后一行给出 N 名学生的百分制整数成绩,中间以空格分隔.最 ...
- PAT Basic 1029 旧键盘 (20 分)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在 2 行中分别给出应该输入的文字.以及 ...
- FastDFS 集群
FastDFS 集群 克隆虚拟机 VMware修改mac 修改 ip地址 rm -f /etc/udev/rules.d/70-persistent-net.rules reboot Tracker集 ...
- WPF 样式Style
一:样式基础 如果我们的程序有三个这样的按键,一般我们会这样写 <StackPanel> <!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为 ...
- python实例31[列出目录下所有的文件到txt]
代码: (使用os.listdir) import os def ListFilesToTxt(dir,file,wildcard,recursion): exts = wildcard.sp ...
- day_04 基本数据类型的结构和使用方法
1. 简述Python的五大数据类型的作用.定义方式.使用方法: 1). 数字类型: 整型(int): 表示年龄.号码.级别:变量名=值,变量名=int(值):加减乘除.逻辑判断: 浮点型(float ...
- JAVA笔记19-容器之三 Set接口、List接口、Collections类、Comparable接口(重要)
一.Set接口 //HashSet综合举例 import java.util.*; public class Test{ public static void main(String[] args){ ...
- xgboost调参过程
from http://blog.csdn.net/han_xiaoyang/article/details/52665396