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
Provenance
- Commit
db16520- Tree
dd4c0fc- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
ab49edc
13 files changed+110 added−32 removed
added:__tests__/__prod_snapshots__/plugins.js.snap+11 added
1+// Jest Snapshot v1, https://goo.gl/fbAQLP2+3+exports[`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"`;4+5+exports[`error when using Maps 1`] = `"[Immer] minified error nr: 19 MapSet. Find the full error at: https://bit.ly/38PiBHb"`;6+7+exports[`error when using patches - 1 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/38PiBHb"`;8+9+exports[`error when using patches - 2 1`] = `"[Immer] minified error nr: 19 Patches. Find the full error at: https://bit.ly/38PiBHb"`;10+11+exports[`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/fbAQLP2+3+exports[`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."`;4+5+exports[`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."`;6+7+exports[`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."`;8+9+exports[`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."`;10+11+exports[`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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import produce, { setUseProxies, produceWithPatches, applyPatches} from "../src/immer"
describe("ES5 plugins should throw if no proxies are available", () => { beforeEach(() => { setUseProxies(false) })
afterEach(() => { setUseProxies(true) })
test("error when using ES5", () => { expect(() => { produce({}, function() {}) }).toThrowErrorMatchingSnapshot() })})
test("error when using Maps", () => { expect(() => { produce(new Map(), function() {}) }).toThrowErrorMatchingSnapshot()})
test("error when using patches - 1", () => { expect(() => { produce( {}, function() {}, function() {} ) }).toThrowErrorMatchingSnapshot()})
test("error when using patches - 2", () => { expect(() => { produceWithPatches({}, function() {}) }).toThrowErrorMatchingSnapshot()})
test("error when using patches - 3", () => { expect(() => { applyPatches({}, []) }).toThrowErrorMatchingSnapshot()})modified:src/core/finalize.ts+2 added-2 removed
24 unmodified lines2526272829293031323362 unmodified lines9697989910010010110210310458 unmodified lines24 unmodified lines scope.unfinalizedDrafts_ = scope.drafts_.length const baseDraft = scope.drafts_![0] const isReplaced = result !== undefined && result !== baseDraft if (!scope.immer_.useProxies_) getPlugin("es5").willFinalizeES5_(scope, result, isReplaced) getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced) if (isReplaced) { if (baseDraft[DRAFT_STATE].modified_) { scope.revoke_() die(4)62 unmodified lines // everything inside is frozen, we can freeze here maybeFreeze(rootScope, result, false) // first time finalizing, let's create those patches if (path && rootScope.patches_) { getPlugin("patches").generatePatches_( getPlugin("Patches").generatePatches_( state, path, rootScope.patches_, rootScope.inversePatches_!58 unmodified linesmodified:src/core/immerClass.ts+14 added-16 removed
17 unmodified lines18 markChangedProxy,19 createProxyProxy,20 freeze,21 getPlugin,22- die22+ die,23+ hasProxies,24+ isMinified25 } from "../internal"26 25-/* istanbul ignore next */26-function verifyMinified() {}27-27 interface ProducersFns {28 produce: IProduce29 produceWithPatches: IProduceWithPatches30 }31 32 export 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 = hasProxies34 35+ autoFreeze_: boolean = __DEV__ ? true /* istanbul ignore next */ : !isMinified36+37 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 lines172 *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 }181 182 applyPatches(base: Objectish, patches: Patch[]) {7 unmodified lines190 break191 }192 }193 196- 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 lines209 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)219 220 const scope = parent ? parent.scope_ : ImmerScope.current_!221 scope.drafts_.push(draft)222 return draft2 unmodified lines225 export 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 lines5 Immer,6 DRAFT_STATE,7 ImmerState,8 ProxyTypeProxyObject,9- ProxyTypeProxyArray9+ ProxyTypeProxyArray,10+ getPlugin11 } from "../internal"12 13 /** Each scope represents a `produce` call. */14 // TODO: non-class?20 unmodified lines35 }36 37 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 linesmodified:src/crap/env.ts+8 added
2 unmodified lines3 // SOme things that should be evaluated before all else...4 const hasSymbol = typeof Symbol !== "undefined"5 export const hasMap = typeof Map !== "undefined"6 export const hasSet = typeof Set !== "undefined"7+export const hasProxies =8+ typeof Proxy !== "undefined" &&9+ typeof Proxy.revocable !== "undefined" &&10+ typeof Reflect !== "undefined"11 12+/* istanbul ignore next */13+function mini() {}14+export const isMinified = mini.name !== "mini"15+16 /**17 * The sentinel value returned by producers to replace the draft with undefined.18 */19 export const NOTHING: Nothing = hasSymbol29 unmodified linesmodified:src/crap/errors.ts+4 added-5 removed
25 unmodified lines26 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 const35 36 export function die(error: keyof typeof errors, ...args: any[]): never {37 if (__DEV__) {7 unmodified lines45 }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 lines16 } from "../internal"17 18 /** Plugin utilities */19 const 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 lines46 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 linesmodified:src/plugins/es5.ts+1 added-1 removed
303 unmodified lines304 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {305 if (state.revoked_) die(3, JSON.stringify(latest(state)))306 }307 308- loadPlugin("es5", {308+ loadPlugin("ES5", {309 createES5Proxy_,310 markChangedES5_,311 willFinalizeES5_312 })1 unmodified linemodified:src/plugins/mapset.ts+1 added-1 removed
334 unmodified lines335 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {336 if (state.revoked_) die(3, JSON.stringify(latest(state)))337 }338 339- loadPlugin("mapset", {proxyMap_, proxySet_})339+ loadPlugin("MapSet", {proxyMap_, proxySet_})340 }modified:src/plugins/patches.ts+1 added-1 removed
264 unmodified lines265 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])266 return cloned267 }268 269- loadPlugin("patches", {applyPatches_, generatePatches_})269+ loadPlugin("Patches", {applyPatches_, generatePatches_})270 }modified:notes.txt+1 added-1 removed
7 unmodified lines8 [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 enumerations101 unmodified lines