feat: `onAssign` for `Maps`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by keenondrums · committed by Alec Larson · parent 00fb26a · Browse files · 3 files·+96 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
3 files+96 added−2 removed
- tests:+93 added−0 removed
- other:+3 added−2 removed
Provenance
- Commit
eaea1b0- Tree
705e6a7- Author
keenondrums <andrey.goncharov+it@protonmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
00fb26a
3 files changed+96 added−2 removed
modified:__tests__/__snapshots__/hooks.js.snap+44 added
149 unmodified lines150 ],151 ]152 `;153 154+exports[`hooks (proxy) - onAssign() when draft is a Map assign 1`] = `155+Array [156+ Array [157+ "a",158+ 10,159+ ],160+ Array [161+ Object {162+ "prop": "val1",163+ },164+ 11,165+ ],166+]167+`;168+169+exports[`hooks (proxy) - onAssign() when draft is a Map nested assignments 1`] = `170+Array [171+ Array [172+ "b",173+ 2,174+ ],175+ Array [176+ Object {177+ "prop": "val1",178+ },179+ Map {180+ "b" => 2,181+ "d" => 1,182+ },183+ ],184+ Array [185+ "a",186+ Map {187+ Object {188+ "prop": "val1",189+ } => Map {190+ "b" => 2,191+ "d" => 1,192+ },193+ },194+ ],195+]196+`;197+198 exports[`hooks (proxy) - onAssign() when draft is an array assign 1`] = `199 Array [200 Array [201 0,140 unmodified linesmodified:__tests__/hooks.js+49 added
116 unmodified lines117 // properties always come first in their ancestor chain.118 expect(calls).toEqual(["b", "d", "c", "x"])119 })120 })121+122+ if (useProxies) {123+ describe("when draft is a Map", () => {124+ test("assign", () => {125+ const key1 = {prop: "val1"}126+ const key2 = {prop: "val2"}127+ produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {128+ s.set("a", 10)129+ s.set(key1, 11)130+ })131+ expectCalls(onAssign)132+ })133+ test("assign (no change)", () => {134+ produce(new Map([["a", 0]]), s => {135+ s.set("a", 0)136+ })137+ expect(onAssign).not.toBeCalled()138+ })139+ test("delete", () => {140+ produce(new Map([["a", 0]]), s => {141+ s.delete("a")142+ })143+ expect(onAssign).not.toBeCalled()144+ })145+ test("nested assignments", () => {146+ const key1 = {prop: "val1"}147+ produce(148+ new Map([149+ [150+ "a",151+ new Map([152+ [153+ key1,154+ new Map([["b", 1], ["c", 1], ["d", 1]])155+ ]156+ ])157+ ]158+ ]),159+ s => {160+ const nested = s.get("a").get(key1)161+ nested.set("b", 2)162+ nested.delete("c")163+ nested.set("d", 1) // no-op164+ }165+ )166+ expectCalls(onAssign)167+ })168+ })169+ }170 })171 172 describe("onDelete()", () => {173 useSharedTests(() => onDelete)103 unmodified linesmodified:src/immer.js+3 added-2 removed
2 unmodified lines3 import {applyPatches, generatePatches} from "./patches"4 import {5 assign,6 each,7+ get,8 has,9 is,10 isDraft,11 isDraftable,257 unmodified lines269 setProperty(parent, prop, value)270 271 // Unchanged drafts are never passed to the `onAssign` hook.272 // TODO: Add tests and support for Maps and Sets272- if (isDraftProp && value === state.base[prop]) return273+ if (isDraftProp && value === get(state.base, prop)) return274 }275 // Unchanged draft properties are ignored.275- else if (isDraftProp && is(value, state.base[prop])) {276+ else if (isDraftProp && is(value, get(state.base, prop))) {277 return278 }279 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.280 else if (isDraftable(value) && !Object.isFrozen(value)) {31 unmodified lines