Skip to content
Myr

Tests for plugins

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 ab49edc · Browse files · 13 files+110 added−32 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+110 added−32 removed

  • tests:+71 added−0 removed
  • other:+39 added−32 removed

13 files+110 added−32 removed

added:__tests__/__prod_snapshots__/plugins.js.snap+11 added
1// Jest Snapshot v1, https://goo.gl/fbAQLP23exports[`ES5 plugins should throw if no proxies are available error when using ES5 1`] = `"[Immer] minified error nr: 19 ES5. Find the full error at: https://bit.ly/38PiBHb"`;45exports[`error when using Maps 1`] = `"[Immer] minified error nr: 19 MapSet. Find the full error at: https://bit.ly/38PiBHb"`;67exports[`error when using patches - 1 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/38PiBHb"`;89exports[`error when using patches - 2 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/38PiBHb"`;1011exports[`error when using patches - 3 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/38PiBHb"`;
added:__tests__/__snapshots__/plugins.js.snap+11 added
1// Jest Snapshot v1, https://goo.gl/fbAQLP23exports[`ES5 plugins should throw if no proxies are available error when using ES5 1`] = `"[Immer] The plugin for 'ES5' has not been loaded into Immer. To enable the plugin, import and call \`enableES5()\` when initializing your application."`;45exports[`error when using Maps 1`] = `"[Immer] The plugin for 'MapSet' has not been loaded into Immer. To enable the plugin, import and call \`enableMapSet()\` when initializing your application."`;67exports[`error when using patches - 1 1`] = `"[Immer] The plugin for 'Patches' has not been loaded into Immer. To enable the plugin, import and call \`enablePatches()\` when initializing your application."`;89exports[`error when using patches - 2 1`] = `"[Immer] The plugin for 'Patches' has not been loaded into Immer. To enable the plugin, import and call \`enablePatches()\` when initializing your application."`;1011exports[`error when using patches - 3 1`] = `"[Immer] The plugin for 'Patches' has not been loaded into Immer. To enable the plugin, import and call \`enablePatches()\` when initializing your application."`;
added:__tests__/plugins.js+49 added
modified:src/core/finalize.ts+2 added-2 removed
modified:src/core/immerClass.ts+14 added-16 removed
17 unmodified lines
18 markChangedProxy,19 createProxyProxy,20 freeze,21 getPlugin,22 die22 die,23 hasProxies,24 isMinified25} from "../internal"2625/* istanbul ignore next */26function verifyMinified() {}2727interface ProducersFns {28 produce: IProduce29 produceWithPatches: IProduceWithPatches30}3132export class Immer implements ProducersFns {34 useProxies_: boolean =35 typeof Proxy !== "undefined" &&36 typeof Proxy.revocable !== "undefined" &&37 typeof Reflect !== "undefined"38 autoFreeze_: boolean = __DEV__39 ? false /* istanbul ignore next */40 : verifyMinified.name === "verifyMinified"33 useProxies_: boolean = hasProxies3435 autoFreeze_: boolean = __DEV__ ? true /* istanbul ignore next */ : !isMinified3637 constructor(config?: {useProxies?: boolean; autoFreeze?: boolean}) {38 if (typeof config?.useProxies === "boolean")39 this.setUseProxies(config!.useProxies)40 if (typeof config?.autoFreeze === "boolean")
131 unmodified lines
172 *173 * By default, feature detection is used, so calling this is rarely necessary.174 */175 setUseProxies(value: boolean) {176 if (!hasProxies) {177 die(20)178 }179 this.useProxies_ = value180 }181182 applyPatches(base: Objectish, patches: Patch[]) {
7 unmodified lines
190 break191 }192 }193196 const applyPatchesImpl = getPlugin("patches").applyPatches_194 const applyPatchesImpl = getPlugin("Patches").applyPatches_195 if (isDraft(base)) {196 // N.B: never hits if some patch a replacement, patches are never drafts197 return applyPatchesImpl(base, patches)198 }
10 unmodified lines
209 parent?: ImmerState210): Drafted<T, ImmerState> {211 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft212 const draft: Drafted = isMap(value)215 ? getPlugin("mapset").proxyMap_(value, parent)213 ? getPlugin("MapSet").proxyMap_(value, parent)214 : isSet(value)217 ? getPlugin("mapset").proxySet_(value, parent)215 ? getPlugin("MapSet").proxySet_(value, parent)216 : immer.useProxies_217 ? createProxyProxy(value, parent)220 : getPlugin("es5").createES5Proxy_(value, parent)218 : getPlugin("ES5").createES5Proxy_(value, parent)219220 const scope = parent ? parent.scope_ : ImmerScope.current_!221 scope.drafts_.push(draft)222 return draft
2 unmodified lines
225export function markChanged(immer: Immer, state: ImmerState) {226 if (immer.useProxies_) {227 markChangedProxy(state)228 } else {231 getPlugin("es5").markChangedES5_(state)229 getPlugin("ES5").markChangedES5_(state)230 }231}
modified:src/core/scope.ts+3 added-1 removed
4 unmodified lines
5 Immer,6 DRAFT_STATE,7 ImmerState,8 ProxyTypeProxyObject,9 ProxyTypeProxyArray9 ProxyTypeProxyArray,10 getPlugin11} from "../internal"1213/** Each scope represents a `produce` call. */14// TODO: non-class?
20 unmodified lines
35 }3637 usePatches_(patchListener?: PatchListener) {38 if (patchListener) {39 getPlugin("Patches") // assert we have the plugin40 this.patches_ = []41 this.inversePatches_ = []42 this.patchListener_ = patchListener43 }
31 unmodified lines
modified:src/crap/env.ts+8 added
2 unmodified lines
3// SOme things that should be evaluated before all else...4const hasSymbol = typeof Symbol !== "undefined"5export const hasMap = typeof Map !== "undefined"6export const hasSet = typeof Set !== "undefined"7export const hasProxies =8 typeof Proxy !== "undefined" &&9 typeof Proxy.revocable !== "undefined" &&10 typeof Reflect !== "undefined"1112/* istanbul ignore next */13function mini() {}14export const isMinified = mini.name !== "mini"1516/**17 * The sentinel value returned by producers to replace the draft with undefined.18 */19export const NOTHING: Nothing = hasSymbol
29 unmodified lines
modified:src/crap/errors.ts+4 added-5 removed
25 unmodified lines
26 17(op: string) {27 return "Unsupported patch operation: " + op28 },29 18(plugin: string) {30 return `The plugin ${plugin} has not been loaded into Immer. Make sure to call "enable${plugin[0].toUpperCase()}${plugin.substr(31 132 )}()" when initializing your application, just after requiring immer itself.`30 return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`31 },34 19: "plugin not loaded"32 19: "plugin not loaded",33 20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available"34} as const3536export function die(error: keyof typeof errors, ...args: any[]): never {37 if (__DEV__) {
7 unmodified lines
45 }46 throw new Error(47 `[Immer] minified error nr: ${error}${48 args.length ? " " + args.join(",") : ""50 }`49 }. Find the full error at: https://bit.ly/38PiBHb`50 )51}
modified:src/crap/plugins.ts+4 added-4 removed
15 unmodified lines
16} from "../internal"1718/** Plugin utilities */19const plugins: {20 patches?: {20 Patches?: {21 generatePatches_(22 state: ImmerState,23 basePath: PatchPath,24 patches: Patch[],25 inversePatches: Patch[]26 ): void27 applyPatches_<T>(draft: T, patches: Patch[]): T28 }29 es5?: {29 ES5?: {30 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void31 createES5Proxy_<T>(32 base: T,33 parent?: ImmerState34 ): Drafted<T, ES5ObjectState | ES5ArrayState>35 markChangedES5_(state: ImmerState): void36 }37 mapset?: {37 MapSet?: {38 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T39 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T40 }41} = {}
4 unmodified lines
46 pluginKey: K47): Exclude<Plugins[K], undefined> {48 const plugin = plugins[pluginKey]49 if (!plugin) {50 die(__DEV__ ? 18 : 19, plugin)50 die(__DEV__ ? 18 : 19, pluginKey)51 }52 // @ts-ignore53 return plugin54}
54 unmodified lines
modified:src/plugins/es5.ts+1 added-1 removed
303 unmodified lines
304 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {305 if (state.revoked_) die(3, JSON.stringify(latest(state)))306 }307308 loadPlugin("es5", {308 loadPlugin("ES5", {309 createES5Proxy_,310 markChangedES5_,311 willFinalizeES5_312 })
1 unmodified line
modified:src/plugins/mapset.ts+1 added-1 removed
334 unmodified lines
335 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {336 if (state.revoked_) die(3, JSON.stringify(latest(state)))337 }338339 loadPlugin("mapset", {proxyMap_, proxySet_})339 loadPlugin("MapSet", {proxyMap_, proxySet_})340}
modified:src/plugins/patches.ts+1 added-1 removed
264 unmodified lines
265 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])266 return cloned267 }268269 loadPlugin("patches", {applyPatches_, generatePatches_})269 loadPlugin("Patches", {applyPatches_, generatePatches_})270}
modified:notes.txt+1 added-1 removed
7 unmodified lines
8[x] clean up finalize9[x] remove die10[x] own invariant11[ ] update docs12[ ] plugin tests12[x] plugin tests13[ ] check if we can create more local vars14[x] verify performance15[ ] kill ownKeys?16[x] kill enumerations
101 unmodified lines