Wrote tests, fixed a bug
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent d0ef4ed · Browse files · 2 files·+69 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+69 added−2 removed
- tests:+68 added−1 removed
- other:+1 added−1 removed
Provenance
- Commit
46e1d5d- Tree
7f9c2aa- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
d0ef4ed
2 files changed+69 added−2 removed
modified:__tests__/base.js+68 added-1 removed
1234567789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858612 unmodified linesimport immer from ".."
describe("base", () => { let baseState let origBaseState
beforeEach(() => { baseState = createBaseState() origBaseState = baseState = createBaseState() })
it("should return the original without modifications", () => { const nextState = immer(baseState, () => {}) expect(nextState).toBe(baseState) })
it("should return the original without modifications when reading stuff", () => { const nextState = immer(baseState, s => { expect(s.aProp).toBe("hi") expect(s.anObject.nested).toEqual({ yummie: true }) }) expect(nextState).toBe(baseState) })
it("should return a copy when modifying stuff", () => { const nextState = immer(baseState, s => { s.aProp = "hello world" }) expect(nextState).not.toBe(baseState) expect(baseState.aProp).toBe("hi") expect(nextState.aProp).toBe("hello world") // structural sharing? expect(nextState.nested).toBe(baseState.nested) })
it("deep change bubbles up", () => { const nextState = immer(baseState, s => { s.anObject.nested.yummie = false }) expect(nextState).not.toBe(baseState) expect(nextState.anObject).not.toBe(baseState.anObject) expect(baseState.anObject.nested.yummie).toBe(true) expect(nextState.anObject.nested.yummie).toBe(false) expect(nextState.anArray).toBe(baseState.anArray) })
it("can add props", () => { const nextState = immer(baseState, s => { s.anObject.cookie = { tasty: true } }) expect(nextState).not.toBe(baseState) expect(nextState.anObject).not.toBe(baseState.anObject) expect(nextState.anObject.nested).toBe(baseState.anObject.nested) expect(nextState.anObject.cookie).toEqual({ tasty: true }) })
it("can delete props", () => { const nextState = immer(baseState, s => { delete s.anObject.nested }) expect(nextState).not.toBe(baseState) expect(nextState.anObject).not.toBe(baseState.anObject) expect(nextState.anObject.nested).toBe(undefined) })
it("ignores single non-modification", () => { const nextState = immer(baseState, s => { s.aProp = "hi" }) expect(nextState).toBe(baseState) })
it("processes single modification", () => { const nextState = immer(baseState, s => { s.aProp = "hello" s.aProp = "hi" }) expect(nextState).not.toBe(baseState) expect(nextState).toEqual(baseState) })
afterEach(() => { expect(baseState).toBe(origBaseState) expect(baseState).toEqual(createBaseState()) })})
12 unmodified linesmodified:immer.js+1 added-1 removed
26 unmodified lines2728293031313233343577 unmodified lines26 unmodified lines ownKeys(target) { return Reflect.ownKeys(getCurrentSource(target)) }, set(target, prop, value) { const current = proxyThing(target[prop]) const current = proxyThing(getCurrentSource(target)[prop]) const newValue = proxyThing(value) if (current !== newValue) { const copy = getOrCreateObjectCopy(target) copy[prop] = newValue77 unmodified lines