|
| 1 | +import { ClassModel, action, snapshottedView, getSnapshot, register, types } from "../src"; |
| 2 | + |
| 3 | +@register |
| 4 | +class ViewExample extends ClassModel({ key: types.identifier, name: types.string }) { |
| 5 | + @snapshottedView() |
| 6 | + get slug() { |
| 7 | + return this.name.toLowerCase().replace(/ /g, "-"); |
| 8 | + } |
| 9 | + |
| 10 | + @action |
| 11 | + setName(name: string) { |
| 12 | + this.name = name; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +describe("class model snapshotted views", () => { |
| 17 | + test("an observable instance saves the view value in a snapshot when changed", () => { |
| 18 | + const instance = ViewExample.create({ key: "1", name: "Test" }); |
| 19 | + expect(instance.slug).toEqual("test"); |
| 20 | + let snapshot = getSnapshot(instance); |
| 21 | + expect(snapshot).toEqual({ key: "1", name: "Test" }); // no snapshot output as the object hasn't changed yet |
| 22 | + instance.setName("New Name"); |
| 23 | + snapshot = getSnapshot(instance); |
| 24 | + expect(snapshot).toEqual({ key: "1", name: "New Name", slug: "new-name" }); |
| 25 | + }); |
| 26 | + |
| 27 | + test("an observable instance updates the saved view when the observed view value changes", () => { |
| 28 | + const instance = ViewExample.create({ key: "1", name: "Test" }); |
| 29 | + instance.setName("New Name"); |
| 30 | + expect(instance.slug).toEqual("new-name"); |
| 31 | + const snapshot = getSnapshot(instance); |
| 32 | + expect(snapshot).toEqual({ key: "1", name: "New Name", slug: "new-name" }); |
| 33 | + }); |
| 34 | + |
| 35 | + test("an observable instance ignores the input snapshot value as the logic may have changed", () => { |
| 36 | + const instance = ViewExample.create({ key: "1", name: "Test", slug: "outdated-cache" } as any); |
| 37 | + expect(instance.slug).toEqual("test"); |
| 38 | + }); |
| 39 | + |
| 40 | + test("an readonly instance returns the view value from the snapshot if present", () => { |
| 41 | + const instance = ViewExample.createReadOnly({ key: "1", name: "Test", slug: "test" } as any); |
| 42 | + expect(instance.slug).toEqual("test"); |
| 43 | + }); |
| 44 | + |
| 45 | + test("an readonly instance doesn't recompute the view value from the snapshot", () => { |
| 46 | + const instance = ViewExample.createReadOnly({ key: "1", name: "Test", slug: "whatever" } as any); |
| 47 | + expect(instance.slug).toEqual("whatever"); |
| 48 | + }); |
| 49 | + |
| 50 | + test("an readonly instance doesn't call the computed function if given a snapshot value", () => { |
| 51 | + const fn = jest.fn(); |
| 52 | + @register |
| 53 | + class Spy extends ClassModel({ name: types.string }) { |
| 54 | + @snapshottedView() |
| 55 | + get slug() { |
| 56 | + fn(); |
| 57 | + return this.name.toLowerCase().replace(/ /g, "-"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const instance = Spy.createReadOnly({ name: "Test", slug: "whatever" } as any); |
| 62 | + expect(instance.slug).toEqual("whatever"); |
| 63 | + expect(fn).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + test("an observable instance doesn't call the computed function until snapshotted", () => { |
| 67 | + const fn = jest.fn(); |
| 68 | + @register |
| 69 | + class Spy extends ClassModel({ name: types.string }) { |
| 70 | + @snapshottedView() |
| 71 | + get slug() { |
| 72 | + fn(); |
| 73 | + return this.name.toLowerCase().replace(/ /g, "-"); |
| 74 | + } |
| 75 | + @action |
| 76 | + setName(name: string) { |
| 77 | + this.name = name; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + const instance = Spy.create({ name: "Test", slug: "whatever" } as any); |
| 82 | + expect(fn).not.toHaveBeenCalled(); |
| 83 | + getSnapshot(instance); |
| 84 | + expect(fn).not.toHaveBeenCalled(); |
| 85 | + |
| 86 | + instance.setName("New Name"); |
| 87 | + expect(fn).toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + test("an readonly instance doesn't require the snapshot to include the cache", () => { |
| 91 | + const instance = ViewExample.createReadOnly({ key: "1", name: "Test" }); |
| 92 | + expect(instance.slug).toEqual("test"); |
| 93 | + }); |
| 94 | + |
| 95 | + test("snapshotted views can be passed nested within snapshots", () => { |
| 96 | + @register |
| 97 | + class Outer extends ClassModel({ examples: types.array(ViewExample) }) {} |
| 98 | + |
| 99 | + const instance = Outer.createReadOnly({ |
| 100 | + examples: [{ key: "1", name: "Test", slug: "test-foobar" } as any, { key: "2", name: "Test 2", slug: "test-qux" } as any], |
| 101 | + }); |
| 102 | + |
| 103 | + expect(instance.examples[0].slug).toEqual("test-foobar"); |
| 104 | + expect(instance.examples[1].slug).toEqual("test-qux"); |
| 105 | + }); |
| 106 | + |
| 107 | + test("an readonly instance returns the view value in a snapshot of itself when the view is given in the input snapshot", () => { |
| 108 | + const instance = ViewExample.createReadOnly({ key: "1", name: "Test", slug: "foobar" } as any); |
| 109 | + const snapshot = getSnapshot(instance); |
| 110 | + expect((snapshot as any).slug).toEqual("foobar"); |
| 111 | + }); |
| 112 | + |
| 113 | + test("an readonly instance returns the view value in a snapshot of itself when the view is not given in the input snapshot", () => { |
| 114 | + const instance = ViewExample.createReadOnly({ key: "1", name: "Test" } as any); |
| 115 | + const snapshot = getSnapshot(instance); |
| 116 | + expect((snapshot as any).slug).toEqual("test"); |
| 117 | + }); |
| 118 | + |
| 119 | + describe("with a hydrator", () => { |
| 120 | + @register |
| 121 | + class HydrateExample extends ClassModel({ url: types.string }) { |
| 122 | + @snapshottedView<URL>({ |
| 123 | + getSnapshot(value, snapshot, node) { |
| 124 | + expect(snapshot).toBeDefined(); |
| 125 | + expect(node).toBeDefined(); |
| 126 | + return value.toString(); |
| 127 | + }, |
| 128 | + createReadOnly(value, snapshot, node) { |
| 129 | + expect(snapshot).toBeDefined(); |
| 130 | + expect(node).toBeDefined(); |
| 131 | + return value ? new URL(value) : undefined; |
| 132 | + }, |
| 133 | + }) |
| 134 | + get withoutParams() { |
| 135 | + const url = new URL(this.url); |
| 136 | + for (const [key] of url.searchParams.entries()) { |
| 137 | + url.searchParams.delete(key); |
| 138 | + } |
| 139 | + return url; |
| 140 | + } |
| 141 | + |
| 142 | + @action |
| 143 | + setURL(url: string) { |
| 144 | + this.url = url; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + test("snapshotted views with processors can be accessed on observable instances", () => { |
| 149 | + const instance = HydrateExample.create({ url: "https://gadget.dev/blog/feature?utm=whatever" }); |
| 150 | + expect(instance.withoutParams).toEqual(new URL("https://gadget.dev/blog/feature")); |
| 151 | + }); |
| 152 | + |
| 153 | + test("snapshotted views with processors can be accessed on readonly instances when there's no input data", () => { |
| 154 | + const instance = HydrateExample.create({ url: "https://gadget.dev/blog/feature?utm=whatever" }); |
| 155 | + expect(instance.withoutParams).toEqual(new URL("https://gadget.dev/blog/feature")); |
| 156 | + }); |
| 157 | + |
| 158 | + test("snapshotted views with processors can be accessed on readonly instances when there is input data", () => { |
| 159 | + const instance = HydrateExample.createReadOnly({ |
| 160 | + url: "https://gadget.dev/blog/feature?utm=whatever", |
| 161 | + withoutParams: "https://gadget.dev/blog/feature/extra", // pass a different value so we can be sure it is what is being used |
| 162 | + } as any); |
| 163 | + expect(instance.withoutParams).toEqual(new URL("https://gadget.dev/blog/feature/extra")); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments