Skip to content
Myr

base.js

This file

Path

__tests__/base.js

256 lines · 8.9 KiB · JavaScript

Last changed by

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

History of this file

Take it

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 1why, one step back
1"use strict"

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 2–3why, one step back
2import immerProxy from ".."
3import immerEs5 from "../es5"

More repo setupMichel Weststratebefore adoption1637393

line 4why, one step back
4

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 5–6why, one step back
5runBaseTest("proxy", immerProxy)
6runBaseTest("es5", immerEs5)

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 7why, one step back
7

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 8–11why, one step back
8function runBaseTest(name, immer) {
9 describe(`base functionality - ${name}`, () => {10 let baseState11 let origBaseState

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 12why, one step back
12

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 13–15why, one step back
13 beforeEach(() => {
14 origBaseState = baseState = createBaseState()15 })

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 16why, one step back
16

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 17–19why, one step back
17 it("should return the original without modifications", () => {
18 const nextState = immer(baseState, () => {})19 expect(nextState).toBe(baseState)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 20–21why, one step back
20 })
21

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 22–28why, one step back
22 it("should return the original without modifications when reading stuff", () => {
23 const nextState = immer(baseState, s => {24 expect(s.aProp).toBe("hi")25 expect(s.anObject.nested).toMatchObject({yummie: true})26 })27 expect(nextState).toBe(baseState)28 })

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 29why, one step back
29

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 30–35why, one step back
30 it("should not return any value: thunk", () => {
31 const warning = jest.spyOn(console, "warn")32 immer(baseState, () => ({bad: "don't do this"}))33 immer(baseState, () => [1, 2, 3])34 immer(baseState, () => false)35 immer(baseState, () => "")

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 36why, one step back
36

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 37–38why, one step back
37 expect(warning).toHaveBeenCalledTimes(4)
38 warning.mockClear()

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 39–40why, one step back
39 })
40

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 41–49why, one step back
41 it("should return a copy when modifying stuff", () => {
42 const nextState = immer(baseState, s => {43 s.aProp = "hello world"44 })45 expect(nextState).not.toBe(baseState)46 expect(baseState.aProp).toBe("hi")47 expect(nextState.aProp).toBe("hello world")48 // structural sharing?49 expect(nextState.nested).toBe(baseState.nested)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 50–51why, one step back
50 })
51

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 52–60why, one step back
52 it("deep change bubbles up", () => {
53 const nextState = immer(baseState, s => {54 s.anObject.nested.yummie = false55 })56 expect(nextState).not.toBe(baseState)57 expect(nextState.anObject).not.toBe(baseState.anObject)58 expect(baseState.anObject.nested.yummie).toBe(true)59 expect(nextState.anObject.nested.yummie).toBe(false)60 expect(nextState.anArray).toBe(baseState.anArray)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 61–62why, one step back
61 })
62

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 63–70why, one step back
63 it("can add props", () => {
64 const nextState = immer(baseState, s => {65 s.anObject.cookie = {tasty: true}66 })67 expect(nextState).not.toBe(baseState)68 expect(nextState.anObject).not.toBe(baseState.anObject)69 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)70 expect(nextState.anObject.cookie).toEqual({tasty: true})

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 71–72why, one step back
71 })
72

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 73–79why, one step back
73 it("can delete props", () => {
74 const nextState = immer(baseState, s => {75 delete s.anObject.nested76 })77 expect(nextState).not.toBe(baseState)78 expect(nextState.anObject).not.toBe(baseState.anObject)79 expect(nextState.anObject.nested).toBe(undefined)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 80–81why, one step back
80 })
81

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 82–86why, one step back
82 it("ignores single non-modification", () => {
83 const nextState = immer(baseState, s => {84 s.aProp = "hi"85 })86 expect(nextState).toBe(baseState)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 87–88why, one step back
87 })
88

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 89–95why, one step back
89 it("processes single modification", () => {
90 const nextState = immer(baseState, s => {91 s.aProp = "hello"92 s.aProp = "hi"93 })94 expect(nextState).not.toBe(baseState)95 expect(nextState).toEqual(baseState)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 96–97why, one step back
96 })
97

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 98–103why, one step back
98 it("should support reading arrays", () => {
99 const nextState = immer(baseState, s => {100 s.anArray.slice()101 })102 expect(nextState.anArray).toBe(baseState.anArray)103 expect(nextState).toBe(baseState)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 104–105why, one step back
104 })
105

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 106–112why, one step back
106 it("should support changing arrays", () => {
107 const nextState = immer(baseState, s => {108 s.anArray[3] = true109 })110 expect(nextState).not.toBe(baseState)111 expect(nextState.anArray).not.toBe(baseState.anArray)112 expect(nextState.anArray[3]).toEqual(true)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 113–114why, one step back
113 })
114

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 115–120why, one step back
115 it("should support changing arrays - 2", () => {
116 const nextState = immer(baseState, s => {117 s.anArray.splice(1, 1, "a", "b")118 })119 expect(nextState).not.toBe(baseState)120 expect(nextState.anArray).not.toBe(baseState.anArray)

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 121why, one step back
121

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 123–124why, one step back
123 })
124

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 125–133why, one step back
125 it("should support sorting arrays", () => {
126 const nextState = immer(baseState, s => {127 s.anArray[2].c = 4128 s.anArray.sort()129 s.anArray[3].c = 5130 })131 expect(nextState).not.toBe(baseState)132 expect(nextState.anArray).not.toBe(baseState.anArray)133 expect(nextState.anArray).toEqual([1, 2, 3, {c: 5}])

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 134–135why, one step back
134 })
135

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 136–142why, one step back
136 it("should updating inside arrays", () => {
137 const nextState = immer(baseState, s => {138 s.anArray[2].test = true139 })140 expect(nextState).not.toBe(baseState)141 expect(nextState.anArray).not.toBe(baseState.anArray)142 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 143–144why, one step back
143 })
144

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 145–163why, one step back
145 it("reusing object should work", () => {
146 const nextState = immer(baseState, s => {147 const obj = s.anObject148 delete s.anObject149 s.messy = obj150 })151 expect(nextState).not.toBe(baseState)152 expect(nextState.anArray).toBe(baseState.anArray)153 expect(nextState).toEqual({154 anArray: [3, 2, {c: 3}, 1],155 aProp: "hi",156 messy: {157 nested: {158 yummie: true159 },160 coffee: false161 }162 })163 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 164–165why, one step back
164 })
165

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 166–186why, one step back
166 it("refs should be transparent", () => {
167 const nextState = immer(baseState, s => {168 const obj = s.anObject169 s.aProp = "hello"170 delete s.anObject171 obj.coffee = true172 s.messy = obj173 })174 expect(nextState).not.toBe(baseState)175 expect(nextState.anArray).toBe(baseState.anArray)176 expect(nextState).toEqual({177 anArray: [3, 2, {c: 3}, 1],178 aProp: "hello",179 messy: {180 nested: {181 yummie: true182 },183 coffee: true184 }185 })186 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

lines 187–188why, one step back
187 })
188

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 189–195why, one step back
189 it("should allow setting to undefined a defined draft property", () => {
190 const nextState = immer(baseState, s => {191 s.aProp = undefined192 })193 expect(nextState).not.toBe(baseState)194 expect(baseState.aProp).toBe("hi")195 expect(nextState.aProp).toBe(undefined)

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 196–197why, one step back
196 })
197

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 198–208why, one step back
198 // ES implementation does't protect against all outside modifications, just some..
199 if (name === "proxy") {200 it("should revoke the proxy of the baseState after immer function is executed", () => {201 let proxy202 const nextState = immer(baseState, s => {203 proxy = s204 s.aProp = "hello"205 })206 expect(nextState).not.toBe(baseState)207 expect(baseState.aProp).toBe("hi")208 expect(nextState.aProp).toBe("hello")

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 209why, one step back
209

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 210–215why, one step back
210 expect(() => {
211 proxy.aProp = "Hallo"212 }).toThrowError(/revoked/)213 expect(() => {214 const aProp = proxy.aProp215 }).toThrowError(/revoked/)

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 216why, one step back
216

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 217–235why, one step back
217 expect(nextState).not.toBe(baseState)
218 expect(baseState.aProp).toBe("hi")219 expect(nextState.aProp).toBe("hello")220 })221 }222223 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {224 let proxy225 const nextState = immer(baseState, s => {226 proxy = s.anObject227 })228 expect(nextState).toBe(baseState)229 expect(() => {230 // In ES5 implemenation only protects existing props, but alas..231 proxy.coffee = "Hallo"232 }).toThrowError(/revoked/)233 expect(() => {234 const test = proxy.coffee235 }).toThrowError(/revoked/)

Updated changelogMichel Weststratebefore adoption8f44348

lines 236–237why, one step back
236 })
237

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 238–241why, one step back
238 afterEach(() => {
239 expect(baseState).toBe(origBaseState)240 expect(baseState).toEqual(createBaseState())241 })

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 242why, one step back
242

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 243–252why, one step back
243 function createBaseState() {
244 return {245 anArray: [3, 2, {c: 3}, 1],246 aProp: "hi",247 anObject: {248 nested: {249 yummie: true250 },251 coffee: false252 }

No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31

line 253why, one step back
253 }

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 254why, one step back
254 }

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 255–256why, one step back
255 })
256}