[MST] Test mobx-state-tree Models by Recording Snapshots or Patches
Testing models is straightforward. Especially because MST provides powerful tools to track exactly how your state changes over time. Track snapshots, action invocations or even patches to verify the correctness of your actions!
In this lesson you will learn:
- To obtain immutable snapshots of the state using
getSnapshot
- To record snapshots using
onSnapshot
- To store and test modifications over time using
onPatch
- Using Jest's snapshot test feature to verify snapshots and patches
- That MST can infer the type of a snapshot for you
Instead of doing test like:
it("can add new items", () => {
const list = WishList.create()
list.add(
WishListItem.create({
name: "Chesterton",
price:
})
) expect(list.items.length).toBe()
expect(list.items[].name).toBe("Chesterton")
list.items[].changeName("Book of G.K. Chesterton")
expect(list.items[].name).toBe("Book of G.K. Chesterton")
})
We can use snapshot, a more powerful way to test state:
Using 'getSnaphost' from 'mobx-state-tree' and 'toMatchSnapshot()' from the Jest:
it("can add new items", () => {
const list = WishList.create()
list.add({
name: "Chesterton",
price:
}) expect(getSnapshot(list)).toMatchSnapshot() expect(states).toMatchSnapshot()
})
We can also use a listener to listen the state changes:
it("can add new items", () => {
const list = WishList.create()
const states = []
onSnapshot(list, snapshot => {
states = [...state, snapshot]
}) list.add({
name: "Chesterton",
price:
}) expect(getSnapshot(list)).toMatchSnapshot() expect(states).toMatchSnapshot()
})
Sometimes we might don't need to check the whole state tree, we only change part of state, such as an object's name, then what we can use is 'onPatch' from 'mobx-state-tree':
it("can add new items - 2", () => {
const list = WishList.create()
const patches = []
onPatch(list, patch => {
patches.push(patch)
}) list.add({
name: "Chesterton",
price:
}) list.items[].changeName("Google") expect(patches).toMatchSnapshot()
})
In the code, we only change the name:
list.items[].changeName("Book of G.K. Chesterton")
Patch snapshot looks like:
exports[`can create a wishlist - onPatch toMatchSnapshot 1`] = `
Array [
Object {
"op": "replace",
"path": "/items/0/name",
"value": "Google",
},
]
`;
The 'path' prop tells which prop on the object has been changed to what.
Therefore it is also easy to do undo redo.
onPatch(model, listener)
attaches a patch listener to the provided model, which will be invoked whenever the model or any of its descendants is mutatedapplyPatch(model, patch)
applies a patch (or array of patches) to the provided modelrevertPatch(model, patch)
reverse applies a patch (or array of patches) to the provided model. This replays the inverse of a set of patches to a model, which can be used to bring it back to its original state
[MST] Test mobx-state-tree Models by Recording Snapshots or Patches的更多相关文章
- [MST] Create an Entry Form to Add Models to the State Tree
It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...
- [MST] Use Volatile State and Lifecycle Methods to Manage Private State
MST has a pretty unique feature: It allows you to capture private state on models, and manage this s ...
- [MST] Restore the Model Tree State using Hot Module Reloading when Model Definitions Change
n this lesson, we will set up Hot Module Reloading(HMR), making it possible to load new definitions ...
- [MST] Attach Behavior to mobx-state-tree Models Using Actions
Models are not just a nifty feature for type checking. They enable you to attach behavior to your ac ...
- 最小生成树(MST,minimum spanning tree)
生成树:由图生成的树,由图转化为树,进一步可用对树的相关操作来对图进行操作.最小指的是权值最小: 生成树是边的集合,如下图所示的最小生成树:MST={{a,b},{a,f},{f,c}} 本文主要探讨 ...
- 如何用 React 构建前端架构
早期的前端是由后端开发的,最开始的时候仅仅做展示,点一下链接跳转到另外一个页面去,渲染表单,再用Ajax的方式请求网络和后端交互,数据返回来还需要把数据渲染到DOM上.写这样的代码的确是很简单.在We ...
- react第三方库
作者:慕课网链接:https://www.zhihu.com/question/59073695/answer/1071631250来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- [MST] Derive Information from Models Using Views
Redundant data or caching data is a constant source of bugs. MST adheres to the philosophy that no d ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
随机推荐
- Python 绘图与可视化 seaborn
Seaborn是一个基于matplotlib的Python数据可视化库.它提供了一个高级界面,用于绘制有吸引力且信息丰富的统计图形. 主页:http://seaborn.pydata.org/ 官方教 ...
- 【BZOJ 1211】 [HNOI2004]树的计数
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] prufer数列的应用 http://www.cnblogs.com/AWCXV/p/7626625.html 这一题没有节点的度数 ...
- JavaFX学习之道:JavaFX之TableView
TableView表 TableColumn列 构建一个表主要有TableView,TableColumn,ObservableList,Bean. 加入列table.getColumns ...
- cloudstack给已有zone加入物理网络
默认情况下,假设zone建立完后.cloudstack是不提供加入物理网络接口的. 基础架构- 域 - 物理网络 以下仅仅有我们创建zone的时候加入的物理网络 假设想在这个基础上加入一个物理网络是没 ...
- 轻松学习JavaScript十七:JavaScript的BOM学习(二)
JavaScript计时事件 通过使用JavaScript中的BOM对象中的window对象的两个方法就是setTimeout()方法和claerTimeout()方法,我们 有能力作到在一个设定的时 ...
- ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)
题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...
- shadowOffset 具体解释
x向右为正,y向下为正 1.y<0 UILabel *label=[[UILabelalloc] initWithFrame:CGRectMake(40,40, 250,50)]; label. ...
- 如何编译dotnet core
1.git clone源码 2.init-tools.cmd 3. Error: DIA SDK is missing at "C:\Program Files (x86)\Microsof ...
- php中echo什么时候用到\"这个符号
php中echo什么时候用到\"这个符号 当在引号中用到引号时,为避免混乱用\" \称为转义符,表示后面的字符和原来程序语言里的语法符号含义不同 常见的转义有 \" \' ...
- ORA-01950: 表空间'USERS'中无权限的2种解决办法
在创建了一个新的表空间和一个新的用户,当用这个新用户创建表时, 却出现:ORA-01950: 表空 间'USERS'中无权限. 我已经把创建表的权限赋给了此用户,怎么还会缺少权限呢?解决办法 ...