-
-
Notifications
You must be signed in to change notification settings - Fork 856
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from mweststrate/feature/freeze
Added support for frozen trees, and automatically freeze generated trees
- Loading branch information
Showing
6 changed files
with
188 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"use strict" | ||
import immer from ".." | ||
|
||
describe("auto freeze", () => { | ||
const baseState = { | ||
object: {a: 1}, | ||
array: [1, 2], | ||
} | ||
|
||
it("should freeze objects after modifications", () => { | ||
expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen | ||
const next = immer(baseState, draft => { | ||
draft.object.c = 2 | ||
}) | ||
expect(Object.isFrozen(next.object)).toBe(true) | ||
expect(Object.isFrozen(next)).toBe(true) | ||
expect(Object.isFrozen(next.array)).toBe(false) | ||
|
||
expect(() => { | ||
next.object.a = 2 | ||
}).toThrow(/Cannot assign to read only property/) | ||
}) | ||
|
||
it("should freeze arrays after modifications", () => { | ||
expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen | ||
const next = immer(baseState, draft => { | ||
draft.array.push(3) | ||
}) | ||
expect(Object.isFrozen(next.object)).toBe(false) // not touched | ||
expect(Object.isFrozen(next)).toBe(true) | ||
expect(Object.isFrozen(next.array)).toBe(true) | ||
|
||
expect(() => { | ||
next.array.shift() | ||
}).toThrow(/Cannot add\/remove sealed array elements/) | ||
}) | ||
|
||
it("can handle already frozen trees", () => { | ||
const a = [] | ||
const b = {a: a} | ||
Object.freeze(a) | ||
Object.freeze(b) | ||
const n = immer(b, draft => { | ||
draft.c = true | ||
draft.a.push(3) | ||
}) | ||
expect(n).toEqual({c: true, a: [3]}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict" | ||
import immer from ".." | ||
|
||
describe("readme example", () => { | ||
it("works", () => { | ||
const baseState = [ | ||
{ | ||
todo: "Learn typescript", | ||
done: true, | ||
}, | ||
{ | ||
todo: "Try immer", | ||
done: false, | ||
}, | ||
] | ||
|
||
const nextState = immer(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]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters