Skip to content
Myr

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

13 files+150 added−183 removed

modified:__tests__/base.js+8 added-7 removed
317 unmodified lines
318 })319 expect(nextState).toBe(base)320 })321322 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 lines
340 expect(result.get("first").a).toEqual(2)341 expect(result.get("second").a).toEqual(2)342 })343344 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 lines
362 expect(result.get("first").a).toEqual(2)363 expect(result.get("second").a).toEqual(2)364 })365366 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 lines
544 expect(next).toBe(base)545 })546 })547548 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 lines
676677 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 })689690 it("can use 'clear' to remove items", () => {691 const nextState = produce(baseState, s => {
908 unmodified lines
1600 expect(isDraft(array)).toBeFalsy()1601 })1602 })16031603 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 lines
modified:__tests__/frozen.js+1 added-1 removed
154 unmodified lines
155 // In draft, still editable156 expect(produce(res, d => void d.set("a", 2))).not.toBe(res)157 })158159 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 lines
modified:__tests__/hooks.js+5 added-5 removed
118 unmodified lines
119 expect(calls).toEqual(["b", "d", "c", "x"])120 })121 })122123 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 lines
240 expectCalls(onDelete)241 })242 })243244 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 lines
267 expectCalls(onDelete)268 })269 })270271 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 lines
295 })296 expect(calls).toShallowEqual([base.a.b, base.a, base])297 })298299 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 })306307 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 lines
modified:__tests__/map-set.js+2 added-2 removed
modified:__tests__/patch.js+5 added-5 removed
245 unmodified lines
246 ]247 )248})249250describe.skip("delete 6", () => {250describe("delete 6", () => {251 runPatchTest(252 new Set(["x", 1]),253 d => {254 d.delete("x")
2 unmodified lines
257 [{op: "add", path: [0], value: "x"}]258 )259})260261describe.skip("delete 7", () => {261describe("delete 7", () => {262 runPatchTest(263 {x: new Set(["y", 1])},264 d => {265 d.x.delete("y")
314 unmodified lines
580 ]581 )582})583584describe.skip("sets - add - 1", () => {584describe("sets - add - 1", () => {585 runPatchTest(586 new Set([1]),587 d => {588 d.add(2)
2 unmodified lines
591 [{op: "remove", path: [1], value: 2}]592 )593})594595describe.skip("sets - add, delete, add - 1", () => {595describe("sets - add, delete, add - 1", () => {596 runPatchTest(597 new Set([1]),598 d => {599 d.add(2)
4 unmodified lines
604 [{op: "remove", path: [1], value: 2}]605 )606})607608describe.skip("sets - add, delete, add - 2", () => {608describe("sets - add, delete, add - 2", () => {609 runPatchTest(610 new Set([2, 1]),611 d => {612 d.add(2)
251 unmodified lines
modified:__tests__/readme.js+1 added-1 removed
84 unmodified lines
85 age: 3286 })87 })8889 it.skip("can update set", () => {89 it("can update set", () => {90 const state = {91 title: "hello",92 tokenSet: new Set()93 }
139 unmodified lines
modified:src/common.ts+21 added-52 removed
192 unmodified lines
193export function isSet(target): target is Set<any> {194 return hasSet && target instanceof Set195}196197198export function makeIterable2(baseIterator: Iterator<any>): IterableIterator<any> {199 // TODO: correct?200 // TODO: don't use symbol201 baseIterator[Symbol.iterator] = () => baseIterator202 return baseIterator as any203}204205export function makeIterable(next: () => {done: boolean; value: any}) {206 let self207 return (self = {208 [Symbol.iterator]: () => self,209 next210 })211}212205/** Map.prototype.values _-or-_ Map.prototype.entries */206export 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}221222export 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 }237238 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 }253254 return iterateSetValues255}256213export function latest(state: any): any {214 return state.copy || state.base215}216217// 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// }229230// TODO: duplicate of shallow clone231export function clone<T extends Objectish>(obj: T): T232export function clone(obj) {233 if (!isDraftable(obj)) return obj
25 unmodified lines
modified:src/es5.ts+4 added-1 removed
11 unmodified lines
12 shallowCopy,13 DRAFT_STATE,14 iterateMapValues,15 makeIterable,16 makeIterateSetValues,16 latest17} from "./common"18import {proxyMap, hasMapChanges} from "./map"19import {proxySet, hasSetChanges} from "./set"
42 unmodified lines
62 }63}6465export 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 lines
modified:src/immer.ts+2 added-1 removed
345 unmodified lines
346 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 }350351 root = state.copy352 }353354 const needPatches = !!rootPath && !!scope.patches
24 unmodified lines
379 scope.canAutoFreeze = false380 }381382 // 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 return
40 unmodified lines
modified:src/map.ts+51 added-15 removed
8 unmodified lines
9 isSet,10 hasSymbol,11 shallowCopy,12 DRAFT_STATE,13 iterateMapValues,13 makeIterable,15 makeIterateSetValues,14 latest15} from "./common"1617// TODO: kill:
111 unmodified lines
129 }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 draft
2 unmodified lines
140 keys() {141 return latest(this[DRAFT_STATE]).keys();142 }143145 // 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 }158159 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 }173174 [Symbol.iterator]() { // TODO: don't use symbol directly175 return this.entries()176 }177}178179154if (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 */181export 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}196197198199export function proxyMap(target, parent) {200 if (target instanceof DraftMap) {201 return target; // TODO: or copy?202 }
34 unmodified lines
modified:src/patches.ts+1 added
107 unmodified lines
108 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} = state114115 let i = 0116 base.forEach(value => {
92 unmodified lines
modified:src/proxy.ts+13 added-55 removed
14 unmodified lines
15 assignMap,16 assignSet,17 original,18 iterateMapValues,19 makeIterateSetValues,19 latest20} from "./common"21import {ImmerScope} from "./scope"22import { proxyMap } from "./map"23import { proxySet } from "./set"2425// Do nothing before being finalized.26export function willFinalize() {}27
28 unmodified lines
56export 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 }6566 const scope = parent ? parent.scope : ImmerScope.current!6768 // 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 }7980 const state: ES6State<T> = {81 // Track which produce call this is associated with.82 scope,
29 unmodified lines
112 target = [state] as any113 traps = arrayTraps114 }115104 // 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 }110116 const {revoke, proxy} = Proxy.revocable(target, traps)117118 state.draft = proxy119 state.revoke = revoke
200 unmodified lines
320 entries: iterateMapValues,321 [hasSymbol ? Symbol.iterator : "@@iterator"]: iterateMapValues322})323319const iterateSetValues = makeIterateSetValues()320/**321 * Set drafts322 */323324const 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})360361/**362 * Helpers363 */364365324// Access a property without creating an Immer draft.325function peek(draft, prop) {326 const state = draft[DRAFT_STATE]327 const desc = Reflect.getOwnPropertyDescriptor(
63 unmodified lines
modified:src/set.ts+36 added-38 removed
8 unmodified lines
9 isSet,10 hasSymbol,11 shallowCopy,12 DRAFT_STATE,13 iterateMapValues,13 makeIterable,15 makeIterateSetValues,16 latest14 latest,15 original,16 makeIterable217} from "./common"1819// TODO: kill:20import {
20 unmodified lines
41}4243function 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}5253// Make sure DraftSet declarion doesn't die if Map is not avialable...
22 unmodified lines
76 get size(): number {77 return latest(this[DRAFT_STATE]).size78 }7980 has(value: V): boolean {81 return latest(this[DRAFT_STATE]).has(value)82 }8384 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 lines
100101 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 }106107 clear() {108 const state = this[DRAFT_STATE];
1 unmodified line
110 state.scope.immer.markChanged(state)111 return state.copy!.clear()112 }113105 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 }119109 // 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 }125126 keys(): IterableIterator<V> {127 return this.values()128 }129130 // TODO: factor out symbol131 [Symbol.iterator]() {132 return this.values()133 }134135 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 lines
150 }151 return new DraftSet(target, parent)152}153136if (hasSymbol) {137 Object.defineProperty(138 DraftSet.prototype,139 Symbol.iterator,140 // @ts-ignore TODO fix141 proxyMethod(iterateMapValues)142 )143}154// const iterateSetValues = makeIterateSetValues()155145const iterateSetValues = makeIterateSetValues()156147// TODO: kill?148function 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}157158export function hasSetChanges(state) {159 const {base, draft} = state160
12 unmodified lines