Skip to content
Myr

base.js

This file

Path

__tests__/base.js

243 lines · 7.4 KiB · JavaScript

Last changed by

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

History of this file

Take it

More repo setupMichel Weststratebefore adoption1637393

lines 1–2why, one step back
1import immer from ".."
2

Make debugging workMichel Weststratebefore adoptiond0ef4ed

lines 3–4why, one step back
3describe("base", () => {
4 let baseState

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 5why, one step back
5 let origBaseState

Make debugging workMichel Weststratebefore adoptiond0ef4ed

lines 6–7why, one step back
6
7 beforeEach(() => {

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 8why, one step back
8 origBaseState = baseState = createBaseState()

Make debugging workMichel Weststratebefore adoptiond0ef4ed

lines 9–15why, one step back
9 })
1011 it("should return the original without modifications", () => {12 const nextState = immer(baseState, () => {})13 expect(nextState).toBe(baseState)14 })15

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 16–18why, one step back
16 it("should return the original without modifications when reading stuff", () => {
17 const nextState = immer(baseState, s => {18 expect(s.aProp).toBe("hi")

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 19why, one step back
19 expect(s.anObject.nested).toEqual({yummie: true})

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 20–23why, one step back
20 })
21 expect(nextState).toBe(baseState)22 })23

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 24–33why, one step back
24 it("should not return any value: thunk", () => {
25 const warning = jest.spyOn(console, "warn")26 immer(baseState, () => ({bad: "don't do this"}))27 immer(baseState, () => [1, 2, 3])28 immer(baseState, () => false)29 immer(baseState, () => "")3031 expect(warning).toHaveBeenCalledTimes(4)32 })33

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 34–57why, one step back
34 it("should return a copy when modifying stuff", () => {
35 const nextState = immer(baseState, s => {36 s.aProp = "hello world"37 })38 expect(nextState).not.toBe(baseState)39 expect(baseState.aProp).toBe("hi")40 expect(nextState.aProp).toBe("hello world")41 // structural sharing?42 expect(nextState.nested).toBe(baseState.nested)43 })4445 it("deep change bubbles up", () => {46 const nextState = immer(baseState, s => {47 s.anObject.nested.yummie = false48 })49 expect(nextState).not.toBe(baseState)50 expect(nextState.anObject).not.toBe(baseState.anObject)51 expect(baseState.anObject.nested.yummie).toBe(true)52 expect(nextState.anObject.nested.yummie).toBe(false)53 expect(nextState.anArray).toBe(baseState.anArray)54 })5556 it("can add props", () => {57 const nextState = immer(baseState, s => {

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 58why, one step back
58 s.anObject.cookie = {tasty: true}

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 59–62why, one step back
59 })
60 expect(nextState).not.toBe(baseState)61 expect(nextState.anObject).not.toBe(baseState.anObject)62 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 63why, one step back
63 expect(nextState.anObject.cookie).toEqual({tasty: true})

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 64–90why, one step back
64 })
6566 it("can delete props", () => {67 const nextState = immer(baseState, s => {68 delete s.anObject.nested69 })70 expect(nextState).not.toBe(baseState)71 expect(nextState.anObject).not.toBe(baseState.anObject)72 expect(nextState.anObject.nested).toBe(undefined)73 })7475 it("ignores single non-modification", () => {76 const nextState = immer(baseState, s => {77 s.aProp = "hi"78 })79 expect(nextState).toBe(baseState)80 })8182 it("processes single modification", () => {83 const nextState = immer(baseState, s => {84 s.aProp = "hello"85 s.aProp = "hi"86 })87 expect(nextState).not.toBe(baseState)88 expect(nextState).toEqual(baseState)89 })90

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 91–114why, one step back
91 it("should support reading arrays", () => {
92 const nextState = immer(baseState, s => {93 s.anArray.slice()94 })95 expect(nextState.anArray).toBe(baseState.anArray)96 expect(nextState).toBe(baseState)97 })9899 it("should support changing arrays", () => {100 const nextState = immer(baseState, s => {101 s.anArray[3] = true102 })103 expect(nextState).not.toBe(baseState)104 expect(nextState.anArray).not.toBe(baseState.anArray)105 expect(nextState.anArray[3]).toEqual(true)106 })107108 it("should support changing arrays - 2", () => {109 const nextState = immer(baseState, s => {110 s.anArray.splice(1, 1, "a", "b")111 })112 expect(nextState).not.toBe(baseState)113 expect(nextState.anArray).not.toBe(baseState.anArray)114

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 115why, one step back
115 expect(nextState.anArray).toEqual([3, "a", "b", {c: 3}, 1])

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 116–125why, one step back
116 })
117118 it("should support sorting arrays", () => {119 const nextState = immer(baseState, s => {120 s.anArray[2].c = 4121 s.anArray.sort()122 s.anArray[3].c = 5123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 126why, one step back
126 expect(nextState.anArray).toEqual([1, 2, 3, {c: 5}])

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 127–128why, one step back
127 })
128

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 129–134why, one step back
129 it("should updating inside arrays", () => {
130 const nextState = immer(baseState, s => {131 s.anArray[2].test = true132 })133 expect(nextState).not.toBe(baseState)134 expect(nextState.anArray).not.toBe(baseState.anArray)

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 135why, one step back
135 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 136–137why, one step back
136 })
137

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 138–147why, one step back
138 it("reusing object should work", () => {
139 const nextState = immer(baseState, s => {140 debugger141 const obj = s.anObject142 delete s.anObject143 s.messy = obj144 })145 expect(nextState).not.toBe(baseState)146 expect(nextState.anArray).toBe(baseState.anArray)147 expect(nextState).toEqual({

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 148why, one step back
148 anArray: [3, 2, {c: 3}, 1],

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 149–151why, one step back
149 aProp: "hi",
150 messy: {151 nested: {

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 152why, one step back
152 yummie: true,

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 153why, one step back
153 },

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 154–155why, one step back
154 coffee: false,
155 },

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 156–170why, one step back
156 })
157 expect(nextState.messy.nested).toBe(baseState.anObject.nested)158 })159160 it("refs should be transparent", () => {161 const nextState = immer(baseState, s => {162 const obj = s.anObject163 s.aProp = "hello"164 delete s.anObject165 obj.coffee = true166 s.messy = obj167 })168 expect(nextState).not.toBe(baseState)169 expect(nextState.anArray).toBe(baseState.anArray)170 expect(nextState).toEqual({

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 171why, one step back
171 anArray: [3, 2, {c: 3}, 1],

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 172–174why, one step back
172 aProp: "hello",
173 messy: {174 nested: {

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 175why, one step back
175 yummie: true,

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 176why, one step back
176 },

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 177–178why, one step back
177 coffee: true,
178 },

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 179–182why, one step back
179 })
180 expect(nextState.messy.nested).toBe(baseState.anObject.nested)181 })182

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

lines 183–187why, one step back
183 it("should allow setting to undefined a defined draft property", () => {
184 const nextState = immer(baseState, s => {185 s.aProp = undefined186 })187 expect(nextState).not.toBe(baseState)

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 188why, one step back
188 expect(baseState.aProp).toBe("hi")

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

lines 189–191why, one step back
189 expect(nextState.aProp).toBe(undefined)
190 })191

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 192why, one step back
192 afterEach(() => {

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 193why, one step back
193 expect(baseState).toBe(origBaseState)

Make debugging workMichel Weststratebefore adoptiond0ef4ed

lines 194–196why, one step back
194 expect(baseState).toEqual(createBaseState())
195 })196

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 197–198why, one step back
197 function createBaseState() {
198 return {

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 199why, one step back
199 anArray: [3, 2, {c: 3}, 1],

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 200–202why, one step back
200 aProp: "hi",
201 anObject: {202 nested: {

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 203why, one step back
203 yummie: true,

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 204why, one step back
204 },

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 205–206why, one step back
205 coffee: false,
206 },

Make debugging workMichel Weststratebefore adoptiond0ef4ed

lines 207–208why, one step back
207 }
208 }

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 209–215why, one step back
209})
210211describe("readme example", () => {212 it("works", () => {213 const baseState = [214 {215 todo: "Learn typescript",

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 216why, one step back
216 done: true,

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 217–219why, one step back
217 },
218 {219 todo: "Try immer",

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 220–221why, one step back
220 done: false,
221 },

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 222–224why, one step back
222 ]
223224 const nextState = immer(baseState, state => {

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 225why, one step back
225 state.push({todo: "Tweet about it"})

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 226–243why, one step back
226 state[1].done = true
227 })228229 // the new item is only added to the next state,230 // base state is unmodified231 expect(baseState.length).toBe(2)232 expect(nextState.length).toBe(3)233234 // same for the changed 'done' prop235 expect(baseState[1].done).toBe(false)236 expect(nextState[1].done).toBe(true)237238 // unchanged data is structurally shared239 expect(nextState[0]).toBe(baseState[0])240 // changed data not (dûh)241 expect(nextState[1]).not.toBe(baseState[1])242 })243})