zoukankan      html  css  js  c++  java
  • [MST] Describe Your Application Domain Using mobx-state-tree(MST) Models

    In this lesson, we introduce the running example of this course, a wishlist app. We will take a look at the core of mobx-state-tree (MST), models. Models describe the shape of your state and perform type validation.

    You will learn:

    • Defining models using types.Model
    • Instantiating models from JSON using Model.create
    • Primitive types: types.string & types.number
    • Type inference for primitive types
    • types.array
    • types.optional
    • Composing models into a model tree
    • Testing models using jest

    To create a model:

    import { types } from "mobx-state-tree"
    
    export const WishListItem = types.model({
        name: types.string,
        price: types.number,
        image: ""
    })
    
    export const WishList = types.model({
        items: types.optional(types.array(WishListItem), [])
    })

    'types' is similar to React PropTypes.

    Once model is created, then we can write tests to verify:

    import { WishList, WishListItem } from "./WishList"
    
    it("can create a instance of a model", () => {
        const item = WishListItem.create({
            name: "Chronicles of Narnia Box Set - C.S. Lewis",
            price: 28.73
        })
    
        expect(item.price).toBe(28.73)
        expect(item.image).toBe("")
    })
    
    it("can create a wishlist", () => {
        const list = WishList.create({
            items: [
                {
                    name: "Chronicles of Narnia Box Set - C.S. Lewis",
                    price: 28.73
                }
            ]
        })
    
        expect(list.items.length).toBe(1)
        expect(list.items[0].price).toBe(28.73)
    })
  • 相关阅读:
    h264 流、帧结构
    H264 帧结构分析、帧判断
    sigaction
    sigaction 用法实例
    sigaction函数的使用
    linux c 之signal 和sigaction区别
    linux 信号signal和sigaction理解
    Hamcrest使用
    Junit4中的新断言assertThat的使用方法
    Hamcrest Tutorial
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8337570.html
Copyright © 2011-2022 走看看