diff --git a/README.md b/README.md index c13fde7..56a5c8e 100644 --- a/README.md +++ b/README.md @@ -510,7 +510,7 @@ const readOnlyExample = TransformExample.createReadOnly(snapshot); readOnlyExample.withoutParams; // => URL { href: "https://example.com" } ``` -Snapshotted views emit patches when their values change. If you don't want snapshotted views to emit a patch when they change, you can pass a `shouldEmitPatchOnChange` function that returns `false` to the `@snapshottedView`, or you can pass `false` to `setDefaultShouldEmitPatchOnChange` to disable patch emission for all snapshotted views. +If your snapshotted views need to emit patches when their values change, you can pass a `shouldEmitPatchOnChange` function that returns `true` to the `@snapshottedView` decorator, or you can pass `true` to `setDefaultShouldEmitPatchOnChange` to enable patch emission for all snapshotted views. ##### Snapshotted view semantics diff --git a/spec/__snapshots__/class-model-snapshotted-views.spec.ts.snap b/spec/__snapshots__/class-model-snapshotted-views.spec.ts.snap index 948a91e..5889628 100644 --- a/spec/__snapshots__/class-model-snapshotted-views.spec.ts.snap +++ b/spec/__snapshots__/class-model-snapshotted-views.spec.ts.snap @@ -1,30 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`class model snapshotted views an observable instance emits a patch when the view value changes 1`] = ` -[MockFunction] { - "calls": [ - [ - { - "op": "replace", - "path": "/__snapshottedViewsEpoch", - "value": 1, - }, - { - "op": "replace", - "path": "/__snapshottedViewsEpoch", - "value": 0, - }, - ], - ], - "results": [ - { - "type": "return", - "value": undefined, - }, - ], -} -`; - exports[`class model snapshotted views references references to models with snapshotted views can be instantiated 1`] = ` { "examples": { diff --git a/spec/class-model-snapshotted-views.spec.ts b/spec/class-model-snapshotted-views.spec.ts index d4c8c5b..60d665b 100644 --- a/spec/class-model-snapshotted-views.spec.ts +++ b/spec/class-model-snapshotted-views.spec.ts @@ -72,27 +72,6 @@ describe("class model snapshotted views", () => { expect(instance.slug).toEqual("test"); }); - test("an observable instance emits a patch when the view value changes", () => { - const observableArray = observable.array([]); - - @register - class MyViewExample extends ClassModel({ key: types.identifier, name: types.string }) { - @snapshottedView() - get arrayLength() { - return observableArray.length; - } - } - - const fn = jest.fn(); - const instance = MyViewExample.create({ key: "1", name: "Test" }); - onPatch(instance, fn); - - runInAction(() => { - observableArray.push("a"); - }); - expect(fn).toMatchSnapshot(); - }); - test("an observable instance's snapshot includes the snapshotted views epoch", () => { const instance = ViewExample.create({ key: "1", name: "Test" }); expect(getSnapshot(instance)).toEqual({ __snapshottedViewsEpoch: 0, key: "1", name: "Test" }); @@ -236,7 +215,7 @@ describe("class model snapshotted views", () => { @register class Child extends ClassModel({}) { - @snapshottedView({ onError }) + @snapshottedView({ onError, shouldEmitPatchOnChange: () => true }) get parentsChildLength() { const parent: Parent = getParent(this, 2); return parent.children.size; @@ -290,7 +269,7 @@ describe("class model snapshotted views", () => { describe("shouldEmitPatchOnChange", () => { afterEach(() => { // reset the default value - setDefaultShouldEmitPatchOnChange(true); + setDefaultShouldEmitPatchOnChange(false); }); test("readonly instances don't use the shouldEmitPatchOnChange option", () => { @@ -356,9 +335,15 @@ describe("class model snapshotted views", () => { }); expect(onPatchFn).toHaveBeenCalled(); + expect(onPatchFn.mock.calls).toEqual([ + [ + { op: "replace", path: "/__snapshottedViewsEpoch", value: 1 }, + { op: "replace", path: "/__snapshottedViewsEpoch", value: 0 }, + ], + ]); }); - test("observable instances do emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange hasn't been called", () => { + test("observable instances don't emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange hasn't been called", () => { const observableArray = observable.array([]); @register @@ -378,7 +363,7 @@ describe("class model snapshotted views", () => { observableArray.push("a"); }); - expect(onPatchFn).toHaveBeenCalled(); + expect(onPatchFn).not.toHaveBeenCalled(); }); test("observable instances do emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange was passed true", () => { @@ -404,6 +389,13 @@ describe("class model snapshotted views", () => { }); expect(onPatchFn).toHaveBeenCalled(); + expect(onPatchFn).toHaveBeenCalled(); + expect(onPatchFn.mock.calls).toEqual([ + [ + { op: "replace", path: "/__snapshottedViewsEpoch", value: 1 }, + { op: "replace", path: "/__snapshottedViewsEpoch", value: 0 }, + ], + ]); }); test("observable instances don't emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange was passed false", () => { diff --git a/src/class-model.ts b/src/class-model.ts index 83f176d..cb49864 100644 --- a/src/class-model.ts +++ b/src/class-model.ts @@ -560,7 +560,7 @@ export const isClassModel = (type: IAnyType): type is IClassModelType