readme.js
This file
Path
__tests__/readme.js
51 lines · 1.3 KiB · JavaScript
Last changed by
Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152"use strict"import produce from "../src/immer"
describe("readme example", () => { it("works", () => { const baseState = [ { todo: "Learn typescript", done: true }, { todo: "Try immer", done: false } ]
const nextState = produce(baseState, draft => { draft.push({todo: "Tweet about it"}) draft[1].done = true })
// the new item is only added to the next state, // base state is unmodified expect(baseState.length).toBe(2) expect(nextState.length).toBe(3)
// same for the changed 'done' prop expect(baseState[1].done).toBe(false) expect(nextState[1].done).toBe(true)
// unchanged data is structurally shared expect(nextState[0]).toBe(baseState[0]) // changed data not (dûh) expect(nextState[1]).not.toBe(baseState[1]) })
it("it can use this", () => { const base = {counter: 0}
const next = produce(base, function() { this.counter++ }) expect(next.counter).toBe(1)
// OR const increment = produce(function() { this.counter++ }) expect(increment(base).counter).toBe(1) })})