fixed most tests
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 08f4682 · Browse files · 13 files·+150 added−183 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
13 files+150 added−183 removed
- tests:+22 added−21 removed
- other:+128 added−162 removed
Provenance
- Commit
385c0a4- Tree
1a8c95f- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
08f4682
13 files changed+150 added−183 removed
modified:__tests__/base.js+8 added-7 removed
317 unmodified lines318 })319 expect(nextState).toBe(base)320 })321 322- it.skip("supports iteration", () => {322+ it("supports iteration", () => {323 const base = new Map([324 ["first", {id: 1, a: 1}],325 ["second", {id: 2, a: 1}]326 ])13 unmodified lines340 expect(result.get("first").a).toEqual(2)341 expect(result.get("second").a).toEqual(2)342 })343 344- it.skip("supports 'entries'", () => {344+ it("supports 'entries'", () => {345 const base = new Map([346 ["first", {id: 1, a: 1}],347 ["second", {id: 2, a: 1}]348 ])13 unmodified lines362 expect(result.get("first").a).toEqual(2)363 expect(result.get("second").a).toEqual(2)364 })365 366- it.skip("supports 'values'", () => {366+ it("supports 'values'", () => {367 const base = new Map([368 ["first", {id: 1, a: 1}],369 ["second", {id: 2, a: 1}]370 ])173 unmodified lines544 expect(next).toBe(base)545 })546 })547 548- describe.skip("set drafts", () => {548+ describe("set drafts", () => {549 it("supports iteration", () => {550 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])551 const findById = (set, id) => {552 for (const item of set) {123 unmodified lines676 677 it("can use 'delete' to remove items", () => {678 const nextState = produce(baseState, s => {679 expect(s.aSet.has("Luke")).toBe(true)680- s.aSet.delete("Luke")680+ expect(s.aSet.delete("Luke")).toBe(true)681+ expect(s.aSet.delete("Luke")).toBe(false)682 expect(s.aSet.has("Luke")).toBe(false)683 })684 expect(nextState.aSet).not.toBe(baseState.aSet)684- expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)685 expect(baseState.aSet.has("Luke")).toBe(true)686 expect(nextState.aSet.has("Luke")).toBe(false)687+ expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)688 })689 690 it("can use 'clear' to remove items", () => {691 const nextState = produce(baseState, s => {908 unmodified lines1600 expect(isDraft(array)).toBeFalsy()1601 })1602 })1603 1603- describe.skip(`complex nesting map / set / object`, () => {1604+ describe(`complex nesting map / set / object`, () => {1605 const a = {a: 1}1606 const b = {b: 2}1607 const c = {c: 3}1608 const set1 = new Set([a, b])172 unmodified linesmodified:__tests__/frozen.js+1 added-1 removed
154 unmodified lines155 // In draft, still editable156 expect(produce(res, d => void d.set("a", 2))).not.toBe(res)157 })158 159- it.skip("will freeze sets", () => {159+ it("will freeze sets", () => {160 const base = new Set()161 const res = produce(base, draft => {162 base.add(1)163 })38 unmodified linesmodified:__tests__/hooks.js+5 added-5 removed
118 unmodified lines119 expect(calls).toEqual(["b", "d", "c", "x"])120 })121 })122 123- describe.skip("when draft is a Map", () => {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 => {112 unmodified lines240 expectCalls(onDelete)241 })242 })243 244- describe.skip("when draft is a Map -", () => {244+ describe("when draft is a Map -", () => {245 test("delete", () => {246 const key1 = {prop: "val1"}247 const key2 = {prop: "val2"}248 produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {18 unmodified lines267 expectCalls(onDelete)268 })269 })270 271- describe.skip("when draft is a Set -", () => {271+ describe("when draft is a Set -", () => {272 test("delete", () => {273 produce({a: new Set([1])}, s => {274 s.a.delete(1)275 })19 unmodified lines295 })296 expect(calls).toShallowEqual([base.a.b, base.a, base])297 })298 299- it.skip("is called in the right order for Maps", () => {299+ it("is called in the right order for Maps", () => {300 const base = new Map([["a", new Map([["b", 0]])]])301 produce(base, s => {302 s.get("a").delete("b")303 })304 expect(calls).toShallowEqual([base.get("a"), base])305 })306 307- it.skip("is called in the right order for Sets", () => {307+ it("is called in the right order for Sets", () => {308 const item1 = {a: 0}309 const base = new Set([item1])310 produce(base, s => {311 s.forEach(item => item.a++)50 unmodified linesmodified:__tests__/map-set.js+2 added-2 removed
55 unmodified lines565758596060616263646 unmodified lines7172737475757677787997 unmodified lines55 unmodified lines bar2.blocked = true // TypeError: "blocked" is read-only }) })
test.skip("#466 - setNoPatches", () => { test("#466 - setNoPatches", () => { const obj = { set: new Set() }
6 unmodified lines [{op: "remove", path: ["set", 0], value: "abc"}] ]) })
test.skip("#466 - mapChangeBug ", () => { test("#466 - mapChangeBug ", () => { const obj = { map: new Map([ ["a", new Map([["b", true], ["c", true], ["d", true]])], ["b", new Map([["a", true]])],97 unmodified linesmodified:__tests__/patch.js+5 added-5 removed
245 unmodified lines246 ]247 )248 })249 250-describe.skip("delete 6", () => {250+describe("delete 6", () => {251 runPatchTest(252 new Set(["x", 1]),253 d => {254 d.delete("x")2 unmodified lines257 [{op: "add", path: [0], value: "x"}]258 )259 })260 261-describe.skip("delete 7", () => {261+describe("delete 7", () => {262 runPatchTest(263 {x: new Set(["y", 1])},264 d => {265 d.x.delete("y")314 unmodified lines580 ]581 )582 })583 584-describe.skip("sets - add - 1", () => {584+describe("sets - add - 1", () => {585 runPatchTest(586 new Set([1]),587 d => {588 d.add(2)2 unmodified lines591 [{op: "remove", path: [1], value: 2}]592 )593 })594 595-describe.skip("sets - add, delete, add - 1", () => {595+describe("sets - add, delete, add - 1", () => {596 runPatchTest(597 new Set([1]),598 d => {599 d.add(2)4 unmodified lines604 [{op: "remove", path: [1], value: 2}]605 )606 })607 608-describe.skip("sets - add, delete, add - 2", () => {608+describe("sets - add, delete, add - 2", () => {609 runPatchTest(610 new Set([2, 1]),611 d => {612 d.add(2)251 unmodified linesmodified:__tests__/readme.js+1 added-1 removed
84 unmodified lines85 age: 3286 })87 })88 89- it.skip("can update set", () => {89+ it("can update set", () => {90 const state = {91 title: "hello",92 tokenSet: new Set()93 }139 unmodified linesmodified:src/common.ts+21 added-52 removed
192 unmodified lines193 export function isSet(target): target is Set<any> {194 return hasSet && target instanceof Set195 }196 197+198+export function makeIterable2(baseIterator: Iterator<any>): IterableIterator<any> {199+ // TODO: correct?200+ // TODO: don't use symbol201+ baseIterator[Symbol.iterator] = () => baseIterator202+ return baseIterator as any203+}204+205 export function makeIterable(next: () => {done: boolean; value: any}) {206 let self207 return (self = {208 [Symbol.iterator]: () => self,209 next210 })211 }212 205-/** Map.prototype.values _-or-_ Map.prototype.entries */206-export function iterateMapValues(state, prop, receiver) {207- const isEntries = prop !== "values"208- return () => {209- const iterator = latest(state)[Symbol.iterator]()210- return makeIterable(() => {211- const result = iterator.next()212- if (!result.done) {213- const [key] = result.value214- const value = receiver.get(key)215- result.value = isEntries ? [key, value] : value216- }217- return result218- })219- }220-}221-222-export function makeIterateSetValues() {223- function iterateSetValues(state, prop?) {224- const isEntries = prop === "entries"225- return () => {226- const iterator = latest(state)[Symbol.iterator]()227- return makeIterable(() => {228- const result = iterator.next()229- if (!result.done) {230- const value = wrapSetValue(state, result.value)231- result.value = isEntries ? [value, value] : value232- }233- return result234- })235- }236- }237-238- function wrapSetValue(state, value) {239- const key = original(value) || value240- let draft = state.drafts.get(key)241- if (!draft) {242- if (state.finalized || !isDraftable(value) || state.finalizing) {243- return value244- }245- draft = state.scope.immer.createProxy(value, state)246- state.drafts.set(key, draft)247- if (state.modified) {248- state.copy.add(draft)249- }250- }251- return draft252- }253-254- return iterateSetValues255-}256-213 export function latest(state: any): any {214 return state.copy || state.base215 }216 217+// export function iterateCollection(collection: Map<any, any> | Set<any>, transformer: (key, value) => any) {218+// // TODO: create own iterator symbols for fallback219+// const iterator = collection.entries()220+// return makeIterable(() => {221+// const result = iterator.next()222+// if (!result.done) {223+// const value = wrapSetValue(state, result.value)224+// result.value = isEntries ? [value, value] : value225+// }226+// return result227+// } as any)228+// }229+230 // TODO: duplicate of shallow clone231 export function clone<T extends Objectish>(obj: T): T232 export function clone(obj) {233 if (!isDraftable(obj)) return obj25 unmodified linesmodified:src/es5.ts+4 added-1 removed
11 unmodified lines12 shallowCopy,13 DRAFT_STATE,14 iterateMapValues,15 makeIterable,16- makeIterateSetValues,16 latest17 } from "./common"18 import {proxyMap, hasMapChanges} from "./map"19 import {proxySet, hasSetChanges} from "./set"42 unmodified lines62 }63 }64 65 export function createProxy<T>(base: T, parent: ES5State): ES5Draft {66+ if (!base || typeof base !== "object" || !isDraftable(base)) {67+ // TODO: || isDraft ?68+ return base as any // TODO: fix69+ }70 const scope = parent ? parent.scope : ImmerScope.current!71 if (isMap(base)) {72 const draft = proxyMap(base, parent) as any // TODO: typefix73 scope.drafts.push(draft)283 unmodified linesmodified:src/immer.ts+2 added-1 removed
345 unmodified lines346 if (!this.useProxies && !isMap(root) && !isSet(root)) {347 // Create the final copy, with added keys and without deleted keys.348 state.copy = shallowCopy(state.draft, true) // TODO: optimization, can we get rid of this and just use state.copy?349 }350+351 root = state.copy352 }353 354 const needPatches = !!rootPath && !!scope.patches24 unmodified lines379 scope.canAutoFreeze = false380 }381 382 // Unchanged drafts are never passed to the `onAssign` hook.382- if (isDraftProp && value === get(state.base, prop)) return383+ if (isDraftProp && !isSetMember && value === get(state.base, prop)) return384 }385 // Unchanged draft properties are ignored.386 else if (isDraftProp && is(value, get(state.base, prop))) {387 return40 unmodified linesmodified:src/map.ts+51 added-15 removed
8 unmodified lines9 isSet,10 hasSymbol,11 shallowCopy,12 DRAFT_STATE,13- iterateMapValues,13 makeIterable,15- makeIterateSetValues,14 latest15 } from "./common"16 17 // TODO: kill:111 unmodified lines129 }130 if (value !== state.base.get(key)) {131 return value // either already drafted or reassigned132 }133+ // despite what it looks, this creates a draft only once, see above condition134 const draft = state.scope.immer.createProxy(value, state)135 prepareCopy(state)136 state.copy!.set(key, draft)137 return draft2 unmodified lines140 keys() {141 return latest(this[DRAFT_STATE]).keys();142 }143 145- // TODO: make these normal functions146- // TODO: values and entries iterators147- // @ts-ignore TODO:148- values = iterateMapValues149- // @ts-ignore TODO:150- entries = iterateMapValues144+ values() {145+ const iterator = this.keys()146+ return {147+ [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly148+ next: () => {149+ const r = iterator.next()150+ if (r.done) return r;151+ const value = this.get(r.value);152+ return {153+ done: false, value154+ }155+ }156+ } as any157+ }158+159+ entries() {160+ const iterator = this.keys()161+ return {162+ [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly163+ next: () => {164+ const r = iterator.next()165+ if (r.done) return r;166+ const value = this.get(r.value);167+ return {168+ done: false, value: [r.value, value]169+ }170+ }171+ } as any172+ }173+174+ [Symbol.iterator]() { // TODO: don't use symbol directly175+ return this.entries()176+ }177 }178 179 154-if (hasSymbol) {155- Object.defineProperty(156- DraftMap.prototype,157- Symbol.iterator,158- // @ts-ignore TODO fix159- proxyMethod(iterateMapValues)160- )180+/** Map.prototype.values _-or-_ Map.prototype.entries */181+export function iterateMapValues(state, prop, receiver) {182+ const isEntries = prop !== "values"183+ return () => {184+ const iterator = latest(state)[Symbol.iterator]()185+ return makeIterable(() => {186+ const result = iterator.next()187+ if (!result.done) {188+ const [key] = result.value189+ const value = receiver.get(key)190+ result.value = isEntries ? [key, value] : value191+ }192+ return result193+ })194+ }195 }196 197+198+199 export function proxyMap(target, parent) {200 if (target instanceof DraftMap) {201 return target; // TODO: or copy?202 }34 unmodified linesmodified:src/patches.ts+1 added
107 unmodified lines108 basePath: (number | string)[],109 patches: Patch[],110 inversePatches: Patch[]111 ) {112+ // TODO: if this doesn't use assigned, drop assigned from SetState stuff113 let {base, copy} = state114 115 let i = 0116 base.forEach(value => {92 unmodified linesmodified:src/proxy.ts+13 added-55 removed
14 unmodified lines15 assignMap,16 assignSet,17 original,18 iterateMapValues,19- makeIterateSetValues,19 latest20 } from "./common"21 import {ImmerScope} from "./scope"22 import { proxyMap } from "./map"23+import { proxySet } from "./set"24 25 // Do nothing before being finalized.26 export function willFinalize() {}27 28 unmodified lines56 export function createProxy<T extends object>(57 base: T,58 parent: ES6State59 ): ES6Draft {60+ // TODO: dedupe61+ if (!base || typeof base !== "object" || !isDraftable(base)) {62+ // TODO: || isDraft ?63+ return base as any // TODO: fix64+ }65+66 const scope = parent ? parent.scope : ImmerScope.current!67 68+ // TODO: dedupe this, it is the same for ES569 if (isMap(base)) {70 const draft = proxyMap(base, parent) as any // TODO: typefix71 scope.drafts.push(draft);72 return draft;73 }74+ if (isSet(base)) {75+ const draft = proxySet(base, parent) as any // TODO: typefix76+ scope.drafts.push(draft);77+ return draft;78+ }79 80 const state: ES6State<T> = {81 // Track which produce call this is associated with.82 scope,29 unmodified lines112 target = [state] as any113 traps = arrayTraps114 }115 104- // Set drafts use a Map object to track which of its values are drafted.105- // And we don't need the "assigned" property, because Set objects have no keys.106- else if (isSet(base)) {107- traps = setTraps108- state.drafts = new Map()109- }110-116 const {revoke, proxy} = Proxy.revocable(target, traps)117 118 state.draft = proxy119 state.revoke = revoke200 unmodified lines320 entries: iterateMapValues,321 [hasSymbol ? Symbol.iterator : "@@iterator"]: iterateMapValues322 })323 319-const iterateSetValues = makeIterateSetValues()320-/**321- * Set drafts322- */323-324-const setTraps = makeTrapsForGetters<Set<any>>({325- //@ts-ignore326- [DRAFT_STATE]: state => state,327- size: state => latest(state).size,328- has: state => key => latest(state).has(key),329- add: state => value => {330- if (!latest(state).has(value)) {331- markChanged(state)332- //@ts-ignore333- state.copy.add(value)334- }335- return state.draft336- },337- delete: state => value => {338- markChanged(state)339- //@ts-ignore340- return state.copy.delete(value)341- },342- clear: state => () => {343- markChanged(state)344- //@ts-ignore345- return state.copy.clear()346- },347- forEach: state => (cb, thisArg) => {348- const iterator = iterateSetValues(state)()349- let result = iterator.next()350- while (!result.done) {351- cb.call(thisArg, result.value, result.value, state.draft)352- result = iterator.next()353- }354- },355- keys: iterateSetValues,356- values: iterateSetValues,357- entries: iterateSetValues,358- [hasSymbol ? Symbol.iterator : "@@iterator"]: iterateSetValues359-})360-361-/**362- * Helpers363- */364-365-324 // Access a property without creating an Immer draft.325 function peek(draft, prop) {326 const state = draft[DRAFT_STATE]327 const desc = Reflect.getOwnPropertyDescriptor(63 unmodified linesmodified:src/set.ts+36 added-38 removed
8 unmodified lines9 isSet,10 hasSymbol,11 shallowCopy,12 DRAFT_STATE,13- iterateMapValues,13 makeIterable,15- makeIterateSetValues,16- latest14+ latest,15+ original,16+ makeIterable217 } from "./common"18 19 // TODO: kill:20 import {20 unmodified lines41 }42 43 function prepareCopy(state: SetState) {44 if (!state.copy) {45+ // create drafts for all entries to preserve insertion order45- state.copy = new Set(state.base);46+ state.copy = new Set()47+ state.base.forEach(value => {48+ state.copy!.add(state.scope.immer.createProxy(value, state))49+ })50 }51 }52 53 // Make sure DraftSet declarion doesn't die if Map is not avialable...22 unmodified lines76 get size(): number {77 return latest(this[DRAFT_STATE]).size78 }79 80+ has(value: V): boolean {81+ return latest(this[DRAFT_STATE]).has(value)82+ }83+84 add(value: V): this {85 const state = this[DRAFT_STATE];78- if (latest(state).has(value) !== value) {86+ if (state.copy) {87+ state.copy.add(value)88+ } else if (!state.base.has(value)) {89 prepareCopy(state)90 state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly91 state.copy!.add(value)92 }7 unmodified lines100 101 const state = this[DRAFT_STATE];102 prepareCopy(state)103 state.scope.immer.markChanged(state)94- state.copy!.delete(value)95- return true104+ return state.copy!.delete(value)105 }106 107 clear() {108 const state = this[DRAFT_STATE];1 unmodified line110 state.scope.immer.markChanged(state)111 return state.copy!.clear()112 }113 105- keys() {106- return latest(this[DRAFT_STATE]).keys();114+ values(): IterableIterator<V> {115+ const state = this[DRAFT_STATE]116+ prepareCopy(state)117+ return state.copy!.values()118 }119 109- // TODO: values and entries iterators110- // @ts-ignore TODO:111- entries = iterateSetValues112- // values: iterateMapValues,113- // @ts-ignore TODO:114- values= iterateSetValues115- // entries: iterateMapValues120+ entries(): IterableIterator<[V, V]> {121+ const state = this[DRAFT_STATE]122+ prepareCopy(state)123+ return state.copy!.entries()124+ }125 126+ keys(): IterableIterator<V> {127+ return this.values()128+ }129+130+ // TODO: factor out symbol131+ [Symbol.iterator]() {132+ return this.values()133+ }134+135 forEach(cb, thisArg) {136 const state = this[DRAFT_STATE]119- const iterator = iterateSetValues(state)()137+ const iterator = this.values()138 let result = iterator.next()139 while (!result.done) {140 cb.call(thisArg, result.value, result.value, state.draft)141 result = iterator.next()8 unmodified lines150 }151 return new DraftSet(target, parent)152 }153 136-if (hasSymbol) {137- Object.defineProperty(138- DraftSet.prototype,139- Symbol.iterator,140- // @ts-ignore TODO fix141- proxyMethod(iterateMapValues)142- )143-}154+// const iterateSetValues = makeIterateSetValues()155 145-const iterateSetValues = makeIterateSetValues()156 147-// TODO: kill?148-function proxyMethod(trap, key) {149- return {150- get() {151- return function(this: ES5Draft, ...args) {152- const state = this[DRAFT_STATE]153- assertUnrevoked(state)154- return trap(state, key, state.draft)(...args)155- }156- }157- }158-}157 158 export function hasSetChanges(state) {159 const {base, draft} = state160 12 unmodified lines