chore: improve some tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- cover a missing edge case - removed an extraneous test - improved test names - array-specific tests are grouped together
authored by aleclarson · committed by Alec Larson · parent 21846ab · Browse files · 1 file·+113 added−134 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+113 added−134 removed
- tests:+113 added−134 removed
Provenance
- Commit
efd47f6- Tree
89bfb04- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
21846ab
1 file changed+113 added−134 removed
modified:__tests__/base.js+113 added-134 removed
41 unmodified lines42 beforeEach(() => {43 origBaseState = baseState = createBaseState()44 })45 46- it("should return the original without modifications", () => {47- const nextState = produce(baseState, () => {})48- expect(nextState).toBe(baseState)49- })50-51- it("should return the original without modifications when reading stuff", () => {46+ it("returns the original state when no changes are made", () => {47 const nextState = produce(baseState, s => {48 expect(s.aProp).toBe("hi")49 expect(s.anObject.nested).toMatchObject({yummie: true})50 })51 expect(nextState).toBe(baseState)52 })53 59- it("should return a copy when modifying stuff", () => {54+ it("does structural sharing", () => {55+ const random = Math.random()56 const nextState = produce(baseState, s => {61- s.aProp = "hello world"57+ s.aProp = random58 })59 expect(nextState).not.toBe(baseState)64- expect(baseState.aProp).toBe("hi")65- expect(nextState.aProp).toBe("hello world")66- // structural sharing?60+ expect(nextState.aProp).toBe(random)61 expect(nextState.nested).toBe(baseState.nested)62 })63 70- it("should preserve type", () => {64+ it("preserves the prototype of drafts", () => {65+ const getProto = Object.getPrototypeOf66 const nextState = produce(baseState, s => {72- expect(Object.getPrototypeOf(s)).toBe(Object.prototype)73- expect(Array.isArray(s.anArray)).toBe(true)74- s.anArray.push(3)75- s.aProp = "hello world"76- expect(Object.getPrototypeOf(s)).toBe(Object.prototype)77- expect(Array.isArray(s.anArray)).toBe(true)67+ expect(getProto(s)).toBe(Object.prototype)68+ expect(getProto(s.anArray)).toBe(Array.prototype)69+ s.aProp = Math.random()70+ s.anArray.push(1)71 })79- expect(Object.getPrototypeOf(nextState)).toBe(Object.prototype)80- expect(Array.isArray(nextState.anArray)).toBe(true)72+ expect(getProto(nextState)).toBe(Object.prototype)73+ expect(getProto(nextState.anArray)).toBe(Array.prototype)74 })75 76 it("deep change bubbles up", () => {77 const nextState = produce(baseState, s => {46 unmodified lines124 })125 expect(nextState).toBe(baseState)126 })127 135- it("processes single modification", () => {128+ // Once a draft is marked as modified, it stays that way.129+ it("never removes modified properties from its internal state", () => {130 const nextState = produce(baseState, s => {131+ const original = s.aProp132+ // This assignment marks the draft as modified.133 s.aProp = "hello"138- s.aProp = "hi"134+ // This assignment reverts the property to its original value,135+ // but Immer cannot detect this, so the base state is copied anyway.136+ s.aProp = original137 })138 expect(nextState).not.toBe(baseState)139 expect(nextState).toEqual(baseState)140 })141 144- it("processes with for loop", () => {145- const base = [{id: 1, a: 1}, {id: 2, a: 1}]146- const findById = (collection, id) => {147- for (const item of collection) {148- if (item.id === id) return item149- }150- return null151- }152- const result = produce(base, draft => {153- const obj1 = findById(draft, 1)154- const obj2 = findById(draft, 2)155- obj1.a = 2156- obj2.a = 2142+ describe("array drafts", () => {143+ it("supports Array.isArray()", () => {144+ const nextState = produce(baseState, s => {145+ expect(Array.isArray(s.anArray)).toBeTruthy()146+ s.anArray.push(1)147+ })148+ expect(Array.isArray(nextState.anArray)).toBeTruthy()149 })158- expect(result[0].a).toEqual(2)159- expect(result[1].a).toEqual(2)160- })150 162- it("works with objects without proto", () => {163- const base = Object.create(null)164- base.x = 1165- base.y = Object.create(null)166- base.y.y = 2167- expect(base.__proto__).toBe(undefined)168- const next = produce(base, draft => {169- draft.y.z = 3170- draft.y.y++171- draft.x++151+ it("supports index access", () => {152+ const value = baseState.anArray[0]153+ const nextState = produce(baseState, s => {154+ expect(s.anArray[0]).toBe(value)155+ })156+ expect(nextState).toBe(baseState)157 })173- expect(next).toEqual({174- x: 2,175- y: {y: 3, z: 3}158+159+ it("supports iteration", () => {160+ const base = [{id: 1, a: 1}, {id: 2, a: 1}]161+ const findById = (collection, id) => {162+ for (const item of collection) {163+ if (item.id === id) return item164+ }165+ return null166+ }167+ const result = produce(base, draft => {168+ const obj1 = findById(draft, 1)169+ const obj2 = findById(draft, 2)170+ obj1.a = 2171+ obj2.a = 2172+ })173+ expect(result[0].a).toEqual(2)174+ expect(result[1].a).toEqual(2)175 })177- expect(next.__proto__).toBe(undefined)178- })176 180- it("should support reading arrays", () => {181- const nextState = produce(baseState, s => {182- s.anArray.slice()177+ it("can assign an index via bracket notation", () => {178+ const nextState = produce(baseState, s => {179+ s.anArray[3] = true180+ })181+ expect(nextState).not.toBe(baseState)182+ expect(nextState.anArray).not.toBe(baseState.anArray)183+ expect(nextState.anArray[3]).toEqual(true)184 })184- expect(nextState.anArray).toBe(baseState.anArray)185- expect(nextState).toBe(baseState)186- })185 188- it("should support changing arrays", () => {189- const nextState = produce(baseState, s => {190- s.anArray[3] = true186+ it("can use splice() to both add and remove items", () => {187+ const nextState = produce(baseState, s => {188+ s.anArray.splice(1, 1, "a", "b")189+ })190+ expect(nextState.anArray).not.toBe(baseState.anArray)191+ expect(nextState.anArray[1]).toBe("a")192+ expect(nextState.anArray[2]).toBe("b")193 })192- expect(nextState).not.toBe(baseState)193- expect(nextState.anArray).not.toBe(baseState.anArray)194- expect(nextState.anArray[3]).toEqual(true)195- })194 197- it("should support changing arrays - 2", () => {198- const nextState = produce(baseState, s => {199- s.anArray.splice(1, 1, "a", "b")195+ it("can truncate via the length property", () => {196+ const baseLength = baseState.anArray.length197+ const nextState = produce(baseState, s => {198+ s.anArray.length = baseLength - 1199+ })200+ expect(nextState.anArray).not.toBe(baseState.anArray)201+ expect(nextState.anArray.length).toBe(baseLength - 1)202 })201- expect(nextState).not.toBe(baseState)202- expect(nextState.anArray).not.toBe(baseState.anArray)203 204- expect(enumerableOnly(nextState.anArray)).toEqual([205- 3,206- "a",207- "b",208- {c: 3},209- 1210- ])211- })204+ it("can extend via the length property", () => {205+ const baseLength = baseState.anArray.length206+ const nextState = produce(baseState, s => {207+ s.anArray.length = baseLength + 1208+ })209+ expect(nextState.anArray).not.toBe(baseState.anArray)210+ expect(nextState.anArray.length).toBe(baseLength + 1)211+ })212 213- // Reported here: https://github.com/mweststrate/immer/issues/116214- it("should support changing arrays - 3", () => {215- const nextState = produce([1, 2, 3], s => {216- s.pop()217- s.push(100)213+ // Reported here: https://github.com/mweststrate/immer/issues/116214+ it("can pop then push", () => {215+ const nextState = produce([1, 2, 3], s => {216+ s.pop()217+ s.push(100)218+ })219+ expect(nextState).toEqual([1, 2, 100])220 })219- expect(nextState).toEqual([1, 2, 100])220- })221 222- it("can delete array items", () => {223- const nextState = produce(baseState, s => {224- s.anArray.length = 3222+ it("can be sorted", () => {223+ const baseState = [3, 1, 2]224+ const nextState = produce(baseState, s => {225+ s.sort()226+ })227+ expect(nextState).not.toBe(baseState)228+ expect(nextState).toEqual([1, 2, 3])229 })226- expect(nextState).not.toBe(baseState)227- expect(nextState.anObject).toBe(baseState.anObject)228- expect(nextState.anArray).not.toBe(baseState.anArray)229- expect(nextState.anArray).toEqual([3, 2, {c: 3}])230- })230 232- it("should support sorting arrays", () => {233- const nextState = produce(baseState, s => {234- s.anArray[2].c = 4235- s.anArray.sort()236- s.anArray[3].c = 5231+ it("supports modifying nested objects", () => {232+ const baseState = [{a: 1}, {}]233+ const nextState = produce(baseState, s => {234+ s[0].a++235+ s[1].a = 0236+ })237+ expect(nextState).not.toBe(baseState)238+ expect(nextState[0].a).toBe(2)239+ expect(nextState[1].a).toBe(0)240 })238- expect(nextState).not.toBe(baseState)239- expect(nextState.anArray).not.toBe(baseState.anArray)240- expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])241 })242 243- it("should expose property descriptors", () => {243+ it("supports access of property descriptors", () => {244 const nextState = produce([], s => {245 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)246 s.unshift("x")247 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({13 unmodified lines261 writable: !autoFreeze262 })263 })264 265- it("should support sorting arrays - 2", () => {266- const nextState = produce(baseState, s => {267- s.anArray.unshift("x")268- s.anArray[3].c = 4269- s.anArray.sort()270- s.anArray[3].c = 5271- s.anArray.unshift("y")272- })273- expect(nextState).not.toBe(baseState)274- expect(nextState.anArray).not.toBe(baseState.anArray)275- expect(enumerableOnly(nextState.anArray)).toEqual([276- "y",277- 1,278- 2,279- 3,280- {c: 5},281- "x"282- ])283- })284-285- it("should updating inside arrays", () => {286- const nextState = produce(baseState, s => {287- s.anArray[2].test = true288- })289- expect(nextState).not.toBe(baseState)290- expect(nextState.anArray).not.toBe(baseState.anArray)291- expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])292- })293-265 it("can rename nested objects (no changes)", () => {266 const nextState = produce(baseState, s => {267 const obj = s.anObject268 delete s.anObject71 unmodified lines340 }341 })342 })343 373- it("should allow setting to undefined a defined draft property", () => {344+ it("supports assigning undefined to an existing property", () => {345 const nextState = produce(baseState, s => {346 s.aProp = undefined347 })348 expect(nextState).not.toBe(baseState)378- expect(baseState.aProp).toBe("hi")349 expect(nextState.aProp).toBe(undefined)350 })351 352+ it("supports assigning undefined to a new property", () => {353+ const baseState = {}354+ const nextState = produce(baseState, s => {355+ s.aProp = undefined356+ })357+ expect(nextState).not.toBe(baseState)358+ expect(nextState.aProp).toBe(undefined)359+ })360+361 // ES implementation does't protect against all outside modifications, just some..362 if (useProxies) {363 it("should revoke the proxy of the baseState after immer function is executed", () => {364 let proxy944 unmodified lines