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 mutated
  • applyPatch(model, patch) applies a patch (or array of patches) to the provided model
  • revertPatch(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

Link

最新文章

  1. vuex
  2. [ZZ]From QA to Engineering Productivity
  3. Mysql子查询
  4. GPS模块启动模式说明
  5. String.Format格式说明
  6. Mac OS X 好用的软件包管理工具 Homebrew
  7. unix ourhdr.h myerr.h
  8. @Transactional问题记录下
  9. Dom+2016/4/20
  10. 在虚拟机VMware上安装Linux系统教程
  11. python学习之Numpy.genfromtxt
  12. NC 6.X笔记(编辑中)
  13. Redis在python中的使用
  14. springBootJpa的复杂查询
  15. react学习笔记2之正确使用状态
  16. word中替换【换行符】与【回车符】
  17. Word或者WPS里证件照的背景底色和像素调整
  18. 【SpringCloud微服务实战学习系列】创建应用及解析
  19. A - Subsequence (算法 二分 )
  20. 如果不用jQuery,Ajax你还能写出多少?

热门文章

  1. jvm 堆、栈 、方法区概念和联系
  2. 2015 Multi-University Training Contest 7 hdu 5379 Mahjong tree
  3. hadoop系列:zookeeper(2)——zookeeper核心原理(选举)
  4. [Python]threading local 线程局部变量小測试
  5. js中callback执行
  6. doT.js变量和数组混合读取方式
  7. Google Maps API 将开始收费
  8. sublime配置python运行环境
  9. IEEE的论文需要注意的一些
  10. Android setImageResource与setImageBitmap的区别