@praxisjs/decorators 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/dist/__tests__/component.test.d.ts +2 -0
- package/dist/__tests__/component.test.d.ts.map +1 -0
- package/dist/__tests__/component.test.js +146 -0
- package/dist/__tests__/component.test.js.map +1 -0
- package/dist/__tests__/compose.test.d.ts +2 -0
- package/dist/__tests__/compose.test.d.ts.map +1 -0
- package/dist/__tests__/compose.test.js +171 -0
- package/dist/__tests__/compose.test.js.map +1 -0
- package/dist/__tests__/events.test.d.ts +2 -0
- package/dist/__tests__/events.test.d.ts.map +1 -0
- package/dist/__tests__/events.test.js +216 -0
- package/dist/__tests__/events.test.js.map +1 -0
- package/dist/__tests__/functions.test.js +283 -31
- package/dist/__tests__/functions.test.js.map +1 -1
- package/dist/__tests__/lazy.test.d.ts +2 -0
- package/dist/__tests__/lazy.test.d.ts.map +1 -0
- package/dist/__tests__/lazy.test.js +163 -0
- package/dist/__tests__/lazy.test.js.map +1 -0
- package/dist/__tests__/memo.test.js +71 -22
- package/dist/__tests__/memo.test.js.map +1 -1
- package/dist/__tests__/properties.test.js +191 -1
- package/dist/__tests__/properties.test.js.map +1 -1
- package/dist/__tests__/resource.test.d.ts +2 -0
- package/dist/__tests__/resource.test.d.ts.map +1 -0
- package/dist/__tests__/resource.test.js +103 -0
- package/dist/__tests__/resource.test.js.map +1 -0
- package/dist/__tests__/slot.test.d.ts +2 -0
- package/dist/__tests__/slot.test.d.ts.map +1 -0
- package/dist/__tests__/slot.test.js +147 -0
- package/dist/__tests__/slot.test.js.map +1 -0
- package/dist/__tests__/virtual.test.d.ts +2 -0
- package/dist/__tests__/virtual.test.d.ts.map +1 -0
- package/dist/__tests__/virtual.test.js +242 -0
- package/dist/__tests__/virtual.test.js.map +1 -0
- package/dist/component/component.d.ts +1 -1
- package/dist/component/component.d.ts.map +1 -1
- package/dist/component/component.js +15 -10
- package/dist/component/component.js.map +1 -1
- package/dist/component/lazy.d.ts +1 -1
- package/dist/component/lazy.d.ts.map +1 -1
- package/dist/component/lazy.js +29 -27
- package/dist/component/lazy.js.map +1 -1
- package/dist/component/virtual.d.ts +1 -1
- package/dist/component/virtual.d.ts.map +1 -1
- package/dist/component/virtual.js +46 -50
- package/dist/component/virtual.js.map +1 -1
- package/dist/create-class-decorator.d.ts +16 -0
- package/dist/create-class-decorator.d.ts.map +1 -0
- package/dist/create-class-decorator.js +35 -0
- package/dist/create-class-decorator.js.map +1 -0
- package/dist/create-field-decorator.d.ts +14 -0
- package/dist/create-field-decorator.d.ts.map +1 -0
- package/dist/create-field-decorator.js +37 -0
- package/dist/create-field-decorator.js.map +1 -0
- package/dist/create-getter-decorator.d.ts +18 -0
- package/dist/create-getter-decorator.d.ts.map +1 -0
- package/dist/create-getter-decorator.js +15 -0
- package/dist/create-getter-decorator.js.map +1 -0
- package/dist/create-lifecycle-method-decorator.d.ts +6 -0
- package/dist/create-lifecycle-method-decorator.d.ts.map +1 -0
- package/dist/create-lifecycle-method-decorator.js +22 -0
- package/dist/create-lifecycle-method-decorator.js.map +1 -0
- package/dist/create-method-decorator.d.ts +6 -0
- package/dist/create-method-decorator.d.ts.map +1 -0
- package/dist/create-method-decorator.js +14 -0
- package/dist/create-method-decorator.js.map +1 -0
- package/dist/events/emit.d.ts +1 -1
- package/dist/events/emit.d.ts.map +1 -1
- package/dist/events/emit.js +24 -29
- package/dist/events/emit.js.map +1 -1
- package/dist/events/on-command.d.ts +1 -2
- package/dist/events/on-command.d.ts.map +1 -1
- package/dist/events/on-command.js +15 -32
- package/dist/events/on-command.js.map +1 -1
- package/dist/functions/bind.d.ts +1 -2
- package/dist/functions/bind.d.ts.map +1 -1
- package/dist/functions/bind.js +4 -11
- package/dist/functions/bind.js.map +1 -1
- package/dist/functions/debounce.d.ts +1 -2
- package/dist/functions/debounce.d.ts.map +1 -1
- package/dist/functions/debounce.js +12 -20
- package/dist/functions/debounce.js.map +1 -1
- package/dist/functions/log.d.ts +1 -2
- package/dist/functions/log.d.ts.map +1 -1
- package/dist/functions/log.js +40 -38
- package/dist/functions/log.js.map +1 -1
- package/dist/functions/memo.d.ts +1 -2
- package/dist/functions/memo.d.ts.map +1 -1
- package/dist/functions/memo.js +15 -27
- package/dist/functions/memo.js.map +1 -1
- package/dist/functions/once.d.ts +1 -2
- package/dist/functions/once.d.ts.map +1 -1
- package/dist/functions/once.js +14 -12
- package/dist/functions/once.js.map +1 -1
- package/dist/functions/retry.d.ts +1 -2
- package/dist/functions/retry.d.ts.map +1 -1
- package/dist/functions/retry.js +23 -20
- package/dist/functions/retry.js.map +1 -1
- package/dist/functions/throttle.d.ts +1 -2
- package/dist/functions/throttle.d.ts.map +1 -1
- package/dist/functions/throttle.js +9 -16
- package/dist/functions/throttle.js.map +1 -1
- package/dist/functions/watch.d.ts +1 -1
- package/dist/functions/watch.d.ts.map +1 -1
- package/dist/functions/watch.js +25 -31
- package/dist/functions/watch.js.map +1 -1
- package/dist/functions/when.d.ts +1 -2
- package/dist/functions/when.d.ts.map +1 -1
- package/dist/functions/when.js +10 -26
- package/dist/functions/when.js.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/properties/compose.d.ts +3 -0
- package/dist/properties/compose.d.ts.map +1 -0
- package/dist/properties/compose.js +40 -0
- package/dist/properties/compose.js.map +1 -0
- package/dist/properties/computed.d.ts +1 -2
- package/dist/properties/computed.d.ts.map +1 -1
- package/dist/properties/computed.js +10 -9
- package/dist/properties/computed.js.map +1 -1
- package/dist/properties/history.d.ts.map +1 -1
- package/dist/properties/history.js +37 -32
- package/dist/properties/history.js.map +1 -1
- package/dist/properties/index.d.ts +2 -0
- package/dist/properties/index.d.ts.map +1 -1
- package/dist/properties/index.js +2 -0
- package/dist/properties/index.js.map +1 -1
- package/dist/properties/persisted.d.ts +1 -2
- package/dist/properties/persisted.d.ts.map +1 -1
- package/dist/properties/persisted.js +13 -29
- package/dist/properties/persisted.js.map +1 -1
- package/dist/properties/prop.d.ts +1 -2
- package/dist/properties/prop.d.ts.map +1 -1
- package/dist/properties/prop.js +21 -25
- package/dist/properties/prop.js.map +1 -1
- package/dist/properties/resource.d.ts +14 -0
- package/dist/properties/resource.d.ts.map +1 -0
- package/dist/properties/resource.js +27 -0
- package/dist/properties/resource.js.map +1 -0
- package/dist/properties/slot.d.ts +1 -2
- package/dist/properties/slot.d.ts.map +1 -1
- package/dist/properties/slot.js +15 -20
- package/dist/properties/slot.js.map +1 -1
- package/dist/properties/state.d.ts +1 -2
- package/dist/properties/state.d.ts.map +1 -1
- package/dist/properties/state.js +13 -18
- package/dist/properties/state.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/component.test.ts +166 -0
- package/src/__tests__/compose.test.ts +202 -0
- package/src/__tests__/events.test.ts +268 -0
- package/src/__tests__/functions.test.ts +368 -30
- package/src/__tests__/lazy.test.ts +218 -0
- package/src/__tests__/memo.test.ts +89 -22
- package/src/__tests__/properties.test.ts +249 -1
- package/src/__tests__/resource.test.ts +113 -0
- package/src/__tests__/slot.test.ts +179 -0
- package/src/__tests__/virtual.test.ts +260 -0
- package/src/component/component.ts +27 -12
- package/src/component/lazy.ts +33 -35
- package/src/component/virtual.tsx +62 -75
- package/src/create-class-decorator.ts +61 -0
- package/src/create-field-decorator.ts +66 -0
- package/src/create-getter-decorator.ts +52 -0
- package/src/create-lifecycle-method-decorator.ts +38 -0
- package/src/create-method-decorator.ts +26 -0
- package/src/events/emit.ts +22 -33
- package/src/events/on-command.ts +22 -47
- package/src/functions/bind.ts +4 -15
- package/src/functions/debounce.ts +11 -26
- package/src/functions/log.ts +40 -43
- package/src/functions/memo.ts +15 -45
- package/src/functions/once.ts +13 -16
- package/src/functions/retry.ts +21 -22
- package/src/functions/throttle.ts +9 -23
- package/src/functions/watch.ts +27 -34
- package/src/functions/when.ts +11 -36
- package/src/index.ts +28 -0
- package/src/properties/compose.ts +53 -0
- package/src/properties/computed.ts +11 -13
- package/src/properties/history.ts +42 -41
- package/src/properties/index.ts +2 -0
- package/src/properties/persisted.ts +17 -58
- package/src/properties/prop.ts +21 -31
- package/src/properties/resource.ts +34 -0
- package/src/properties/slot.ts +17 -30
- package/src/properties/state.ts +14 -24
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { StatefulComponent } from "@praxisjs/core";
|
|
4
|
+
|
|
5
|
+
import { createCommand } from "../events/command";
|
|
6
|
+
import { Emit } from "../events/emit";
|
|
7
|
+
import { readProp } from "../events/helper";
|
|
8
|
+
import { OnCommand } from "../events/on-command";
|
|
9
|
+
|
|
10
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
class TestComponent extends StatefulComponent {
|
|
13
|
+
render() { return null; }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function methodCtx(name: string) {
|
|
17
|
+
const initializers: Array<(this: unknown) => void> = [];
|
|
18
|
+
return {
|
|
19
|
+
ctx: {
|
|
20
|
+
name,
|
|
21
|
+
kind: "method" as const,
|
|
22
|
+
addInitializer(fn: (this: unknown) => void) { initializers.push(fn); },
|
|
23
|
+
} as ClassMethodDecoratorContext,
|
|
24
|
+
run(instance: unknown) { initializers.forEach((fn) => { fn.call(instance); }); },
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ── createCommand ─────────────────────────────────────────────────────────────
|
|
29
|
+
|
|
30
|
+
describe("createCommand", () => {
|
|
31
|
+
it("trigger calls all subscribed handlers with the argument", () => {
|
|
32
|
+
const cmd = createCommand<number>();
|
|
33
|
+
const h1 = vi.fn();
|
|
34
|
+
const h2 = vi.fn();
|
|
35
|
+
cmd.subscribe(h1);
|
|
36
|
+
cmd.subscribe(h2);
|
|
37
|
+
cmd.trigger(42);
|
|
38
|
+
expect(h1).toHaveBeenCalledWith(42);
|
|
39
|
+
expect(h2).toHaveBeenCalledWith(42);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("trigger does nothing when no handlers are subscribed", () => {
|
|
43
|
+
const cmd = createCommand<string>();
|
|
44
|
+
expect(() => cmd.trigger("hello")).not.toThrow();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("subscribe returns an unsubscribe function", () => {
|
|
48
|
+
const cmd = createCommand<void>();
|
|
49
|
+
const handler = vi.fn();
|
|
50
|
+
const unsub = cmd.subscribe(handler);
|
|
51
|
+
unsub();
|
|
52
|
+
cmd.trigger();
|
|
53
|
+
expect(handler).not.toHaveBeenCalled();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("void command can be triggered without args", () => {
|
|
57
|
+
const cmd = createCommand();
|
|
58
|
+
const handler = vi.fn();
|
|
59
|
+
cmd.subscribe(handler);
|
|
60
|
+
cmd.trigger(undefined as unknown as void);
|
|
61
|
+
expect(handler).toHaveBeenCalledOnce();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ── readProp ──────────────────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
describe("readProp", () => {
|
|
68
|
+
it("returns value from _rawProps when defined", () => {
|
|
69
|
+
const instance = new TestComponent({ myProp: "fromParent" });
|
|
70
|
+
expect(readProp(instance, "myProp")).toBe("fromParent");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("falls back to _defaults when _rawProps does not define the prop", () => {
|
|
74
|
+
const instance = new TestComponent();
|
|
75
|
+
instance._defaults.myProp = "default";
|
|
76
|
+
expect(readProp(instance, "myProp")).toBe("default");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("returns undefined when prop is not in either", () => {
|
|
80
|
+
const instance = new TestComponent();
|
|
81
|
+
expect(readProp(instance, "missing")).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// ── Emit ─────────────────────────────────────────────────────────────────────
|
|
86
|
+
|
|
87
|
+
describe("Emit decorator", () => {
|
|
88
|
+
it("calls the prop callback with the return value of the method", () => {
|
|
89
|
+
const { ctx, run } = methodCtx("handleInput");
|
|
90
|
+
const original = function (this: object, value: string) { return value.toUpperCase(); };
|
|
91
|
+
Emit("onChange")(original as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
92
|
+
|
|
93
|
+
const callback = vi.fn();
|
|
94
|
+
const instance = new TestComponent({ onChange: callback });
|
|
95
|
+
run(instance);
|
|
96
|
+
|
|
97
|
+
const method = (instance as unknown as Record<string, (v: string) => void>).handleInput;
|
|
98
|
+
method.call(instance, "hello");
|
|
99
|
+
expect(callback).toHaveBeenCalledWith("HELLO");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("calls the prop callback with the method args when return value is undefined", () => {
|
|
103
|
+
const { ctx, run } = methodCtx("doSomething");
|
|
104
|
+
const original = function (this: object, a: number, b: number) { return undefined; };
|
|
105
|
+
Emit("onDo")(original as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
106
|
+
|
|
107
|
+
const callback = vi.fn();
|
|
108
|
+
const instance = new TestComponent({ onDo: callback });
|
|
109
|
+
run(instance);
|
|
110
|
+
|
|
111
|
+
const method = (instance as unknown as Record<string, (a: number, b: number) => void>).doSomething;
|
|
112
|
+
method.call(instance, 1, 2);
|
|
113
|
+
expect(callback).toHaveBeenCalledWith(1, 2);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("calls the prop callback with no args when return is undefined and no args", () => {
|
|
117
|
+
const { ctx, run } = methodCtx("fire");
|
|
118
|
+
const original = function (this: object) { return undefined; };
|
|
119
|
+
Emit("onFire")(original as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
120
|
+
|
|
121
|
+
const callback = vi.fn();
|
|
122
|
+
const instance = new TestComponent({ onFire: callback });
|
|
123
|
+
run(instance);
|
|
124
|
+
|
|
125
|
+
const method = (instance as unknown as Record<string, () => void>).fire;
|
|
126
|
+
method.call(instance);
|
|
127
|
+
expect(callback).toHaveBeenCalledOnce();
|
|
128
|
+
expect(callback).toHaveBeenCalledWith();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("does not throw when the prop callback is not a function", () => {
|
|
132
|
+
const { ctx, run } = methodCtx("submit");
|
|
133
|
+
const original = function (this: object) { return "result"; };
|
|
134
|
+
Emit("onSubmit")(original as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
135
|
+
|
|
136
|
+
const instance = new TestComponent(); // no onSubmit prop
|
|
137
|
+
run(instance);
|
|
138
|
+
|
|
139
|
+
const method = (instance as unknown as Record<string, () => void>).submit;
|
|
140
|
+
expect(() => method.call(instance)).not.toThrow();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("still returns the original method's return value", () => {
|
|
144
|
+
const { ctx, run } = methodCtx("compute");
|
|
145
|
+
const original = function (this: object) { return 99; };
|
|
146
|
+
Emit("onResult")(original as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
147
|
+
|
|
148
|
+
const callback = vi.fn();
|
|
149
|
+
const instance = new TestComponent({ onResult: callback });
|
|
150
|
+
run(instance);
|
|
151
|
+
|
|
152
|
+
const method = (instance as unknown as Record<string, () => number>).compute;
|
|
153
|
+
const result = method.call(instance);
|
|
154
|
+
expect(result).toBe(99);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// ── OnCommand ─────────────────────────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
describe("OnCommand decorator", () => {
|
|
161
|
+
it("subscribes to the command on mount and calls the method when triggered", () => {
|
|
162
|
+
const { ctx, run } = methodCtx("handleCmd");
|
|
163
|
+
const handler = vi.fn();
|
|
164
|
+
OnCommand("myCommand")(handler as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
165
|
+
|
|
166
|
+
const cmd = createCommand<string>();
|
|
167
|
+
const instance = new TestComponent({ myCommand: cmd });
|
|
168
|
+
run(instance);
|
|
169
|
+
|
|
170
|
+
instance.onMount?.();
|
|
171
|
+
cmd.trigger("payload");
|
|
172
|
+
expect(handler).toHaveBeenCalledWith("payload");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("unsubscribes from the command on unmount", () => {
|
|
176
|
+
const { ctx, run } = methodCtx("onMsg");
|
|
177
|
+
const handler = vi.fn();
|
|
178
|
+
OnCommand("msg")(handler as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
179
|
+
|
|
180
|
+
const cmd = createCommand<string>();
|
|
181
|
+
const instance = new TestComponent({ msg: cmd });
|
|
182
|
+
run(instance);
|
|
183
|
+
|
|
184
|
+
instance.onMount?.();
|
|
185
|
+
instance.onUnmount?.();
|
|
186
|
+
cmd.trigger("after-unmount");
|
|
187
|
+
expect(handler).not.toHaveBeenCalled();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("warns when the command prop is not provided", () => {
|
|
191
|
+
const { ctx, run } = methodCtx("onMissing");
|
|
192
|
+
OnCommand("missingProp")(vi.fn() as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
193
|
+
|
|
194
|
+
const instance = new TestComponent(); // no missingProp
|
|
195
|
+
run(instance);
|
|
196
|
+
|
|
197
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
198
|
+
instance.onMount?.();
|
|
199
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("[OnCommand]"));
|
|
200
|
+
warn.mockRestore();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("warns when the command prop is not a valid Command", () => {
|
|
204
|
+
const { ctx, run } = methodCtx("onBadCmd");
|
|
205
|
+
OnCommand("badProp")(vi.fn() as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
206
|
+
|
|
207
|
+
const instance = new TestComponent({ badProp: "not-a-command" });
|
|
208
|
+
run(instance);
|
|
209
|
+
|
|
210
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
211
|
+
instance.onMount?.();
|
|
212
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("[OnCommand]"));
|
|
213
|
+
warn.mockRestore();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("uses 'unknown' as component name when constructor.name is undefined (missing-prop branch)", () => {
|
|
217
|
+
const { ctx, run } = methodCtx("onNoName");
|
|
218
|
+
OnCommand("missingProp")(vi.fn() as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
219
|
+
|
|
220
|
+
// Instance whose constructor.name is undefined to hit ?? "unknown"
|
|
221
|
+
const fakeInstance = Object.assign(new TestComponent(), {
|
|
222
|
+
constructor: { name: undefined },
|
|
223
|
+
});
|
|
224
|
+
run(fakeInstance);
|
|
225
|
+
|
|
226
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
227
|
+
fakeInstance.onMount?.();
|
|
228
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("unknown"));
|
|
229
|
+
warn.mockRestore();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("uses 'unknown' as component name when constructor.name is undefined (invalid-command branch)", () => {
|
|
233
|
+
const { ctx, run } = methodCtx("onNoName2");
|
|
234
|
+
OnCommand("badProp")(vi.fn() as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
235
|
+
|
|
236
|
+
// Instance with a non-command prop and undefined constructor.name
|
|
237
|
+
const fakeInstance = Object.assign(new TestComponent({ badProp: "not-a-command" }), {
|
|
238
|
+
constructor: { name: undefined },
|
|
239
|
+
});
|
|
240
|
+
run(fakeInstance);
|
|
241
|
+
|
|
242
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
243
|
+
fakeInstance.onMount?.();
|
|
244
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("unknown"));
|
|
245
|
+
warn.mockRestore();
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("preserves the original onMount and onUnmount implementations", () => {
|
|
249
|
+
const { ctx, run } = methodCtx("onEvt");
|
|
250
|
+
OnCommand("evt")(vi.fn() as never, ctx as unknown as ClassMethodDecoratorContext<StatefulComponent>);
|
|
251
|
+
|
|
252
|
+
const cmd = createCommand<void>();
|
|
253
|
+
const instance = new TestComponent({ evt: cmd });
|
|
254
|
+
run(instance);
|
|
255
|
+
|
|
256
|
+
const mountSpy = vi.fn();
|
|
257
|
+
const unmountSpy = vi.fn();
|
|
258
|
+
const prevMount = instance.onMount?.bind(instance);
|
|
259
|
+
const prevUnmount = instance.onUnmount?.bind(instance);
|
|
260
|
+
instance.onMount = function () { mountSpy(); prevMount?.(); };
|
|
261
|
+
instance.onUnmount = function () { unmountSpy(); prevUnmount?.(); };
|
|
262
|
+
|
|
263
|
+
instance.onMount();
|
|
264
|
+
instance.onUnmount();
|
|
265
|
+
expect(mountSpy).toHaveBeenCalled();
|
|
266
|
+
expect(unmountSpy).toHaveBeenCalled();
|
|
267
|
+
});
|
|
268
|
+
});
|