[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 ...
随机推荐
- Could not find result map java.util.HashMap
Could not find result map java.util.HashMap 找不到结果图java.util.HashMap MyBatis 找不到返回的 'resultMap'!把resu ...
- Unity 代码集锦之图片处理
1.将Texture2D保存为jpg void TestSaveImageToJPG(Texture2D buffer) { File.WriteAllBytes("F:/output.jp ...
- 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] D】Single-use Stones
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设长度为L的所有区间里面,石头的个数的最小值为k 设取到k的区间为l,r 那么k就为最多能通过的青蛙个数. 假设k再大一点.比如为k ...
- Java二维码打印
http://blog.csdn.net/OnePersonTZ/article/details/66560513
- Windows下面使用curl
Windows下面使用curl 学习了:https://www.cnblogs.com/xing901022/p/4652624.html 下载地址:https://curl.haxx.se/down ...
- Floodlight 中创建消息对象的方法
在 floodlight 中创建各种openflow message 和 action 等採用的是简单工厂方式.BasicFactory类(实现OFMessageFactory接口.) ...
- HDU 5410(2015多校10)-CRB and His Birthday(全然背包)
题目地址:HDU 5410 题意:有M元钱,N种礼物,若第i种礼物买x件的话.会有Ai*x+Bi颗糖果,现给出每种礼物的单位价格.Ai值与Bi值.问最多能拿到多少颗糖果. 思路:全然背包问题. dp[ ...
- Android实战简易教程-第十三枪(五大布局研究)
我们知道Android系统应用程序通常是由多个Activity组成,而这些Activity以视图的形式展如今我们面前, 视图都是由一个一个的组件构成的. 组件就是我们常见的Button.TextEdi ...
- python实战之编码问题:中文!永远的痛
编码的思维图谱: 也就是说文件没有编码之说,事实上都是按二进制格式保存在硬盘中的.不过在写入读取时须使用相应的编码进行处理,以便操作系统配合相关软件/字体,绘制到屏幕中给人看.所以关键问题是得知道原先 ...
- 怎样在Web项目中的service业务层获取项目根路劲
这里我们有两个前提 1.没有使用struts2框架.没有使用servlet,无法给service层传递request对象. 2.使用了Spring框架. 那你可能问.会有这样的情况吗?答案是有的,比方 ...