Debian Codegraph @

addons/point_of_sale/static/tests/unit/tools/lazy_getter.test.js

raw
1 import { afterEach, expect, test } from "@odoo/hoot";
2 import { animationFrame } from "@odoo/hoot-dom";
3 import { Component, onWillRender, reactive, useState, xml } from "@odoo/owl";
4 import {
5 mountWithCleanup,
6 allowTranslations,
7 patchWithCleanup,
8 } from "@web/../tests/web_test_helpers";
9
10 import {
11 WithLazyGetterTrap,
12 clearGettersCache,
13 createLazyGetter,
14 } from "@point_of_sale/lazy_getter";
15 import { zip } from "@web/core/utils/arrays";
16
17 /**
18 * @param {string} value
19 */
20 function unorderedStep(value) {
21 unorderedSteps.push(value);
22 }
23
24 /**
25 * Makes multiple assertions:
26 * - Are all items in `vals` in steps?
27 * - Are the items in `steps` ordered according to each item in `stepOrders`?
28 * Then it clears the `steps`.
29 * @param {string[]} expectedSteps
30 * @param {Iterable<string[]>} [stepOrders=[]]
31 */
32 function verifyUnorderedSteps(expectedSteps, stepOrders = []) {
33 expect([...unorderedSteps].sort()).toEqual([...expectedSteps].sort());
34 for (const stepOrder of stepOrders) {
35 expect(
36 zip(stepOrder.slice(0, -1), stepOrder.slice(1)).reduce(
37 (acc, [a, b]) => acc && unorderedSteps.indexOf(a) < unorderedSteps.indexOf(b),
38 true
39 )
40 ).toBe(true);
41 }
42 unorderedSteps = [];
43 }
44
45 let unorderedSteps = [];
46
47 allowTranslations();
48 afterEach(clearGettersCache);
49
50 class AppStore extends WithLazyGetterTrap {
51 constructor() {
52 super({ traps: {} });
53 this.a = 0;
54 this.b = 0;
55 this.c = 0;
56 this.d = 0;
57 }
58 get ab() {
59 return this.a + this.b;
60 }
61 get abc() {
62 let result = 0;
63 for (let i = 0; i < 10; i++) {
64 result += this.ab;
65 }
66 return result + this.c;
67 }
68 get bc() {
69 return this.b + this.c;
70 }
71 get cd() {
72 return this.c + this.d;
73 }
74 get x() {
75 return this.abc + this.bc;
76 }
77 get y() {
78 return this.cd + this.x;
79 }
80 }
81
82 class WithStore extends Component {
83 static props = {};
84 static template = xml`
85 <span t-att-class="property">
86 <t t-esc="constructor.name" />: <t t-esc="this.store[property]" />
87 </span>
88 `;
89
90 property = "";
91
92 setup() {
93 this.store = useState(this.env.store);
94 onWillRender(() => this.onWillRender());
95 }
96
97 onWillRender() {}
98 }
99
100 class A extends WithStore {
101 property = "a";
102 }
103
104 class B extends WithStore {
105 property = "b";
106 }
107
108 class C extends WithStore {
109 property = "c";
110 }
111
112 class D extends WithStore {
113 property = "d";
114 }
115
116 class AB extends WithStore {
117 property = "ab";
118 }
119
120 class ABC extends WithStore {
121 property = "abc";
122 }
123
124 class BC extends WithStore {
125 property = "bc";
126 }
127
128 class CD extends WithStore {
129 property = "cd";
130 }
131
132 class Root extends Component {
133 static components = { A, B, C, D, AB, ABC, BC, CD };
134 static props = {};
135 static template = xml`
136 <t t-foreach="constructor.components" t-as="key" t-key="key">
137 <t t-component="constructor.components[key]" />
138 </t>
139 `;
140 }
141
142 test("each getter should only be called once and only when needed", async () => {
143 patchWithCleanup(AppStore.prototype, {
144 get ab() {
145 unorderedStep("ab");
146 return super.ab;
147 },
148 get abc() {
149 unorderedStep("abc");
150 return super.abc;
151 },
152 get bc() {
153 unorderedStep("bc");
154 return super.bc;
155 },
156 get cd() {
157 unorderedStep("cd");
158 return super.cd;
159 },
160 });
161
162 const store = reactive(new AppStore());
163 await mountWithCleanup(Root, {
164 env: { store },
165 noMainContainer: true,
166 });
167
168 verifyUnorderedSteps(["ab", "abc", "bc", "cd"]);
169
170 store.a = 1;
171
172 // Getters should only be called after an interface re-render
173 verifyUnorderedSteps([]);
174 await animationFrame();
175 verifyUnorderedSteps(["ab", "abc"]);
176
177 store.b = 1;
178
179 verifyUnorderedSteps([]);
180 await animationFrame();
181 verifyUnorderedSteps(["bc", "ab", "abc"]);
182
183 store.c = 1;
184
185 verifyUnorderedSteps([]);
186 await animationFrame();
187 verifyUnorderedSteps(["cd", "bc", "abc"]);
188
189 store.d = 1;
190
191 verifyUnorderedSteps([]);
192 await animationFrame();
193 verifyUnorderedSteps(["cd"]);
194 });
195
196 test("only dependent components rerender", async () => {
197 patchWithCleanup(WithStore.prototype, {
198 onWillRender() {
199 unorderedStep(this.property);
200 },
201 });
202
203 const store = reactive(new AppStore());
204 await mountWithCleanup(Root, {
205 env: { store },
206 noMainContainer: true,
207 });
208
209 verifyUnorderedSteps(["a", "b", "c", "d", "ab", "abc", "bc", "cd"]);
210
211 store.a = 1;
212 await animationFrame();
213
214 verifyUnorderedSteps(["a", "ab", "abc"]);
215
216 store.b = 1;
217 await animationFrame();
218
219 verifyUnorderedSteps(["b", "ab", "abc", "bc"]);
220
221 store.c = 1;
222 await animationFrame();
223
224 verifyUnorderedSteps(["c", "abc", "bc", "cd"]);
225
226 store.d = 1;
227 await animationFrame();
228
229 verifyUnorderedSteps(["d", "cd"]);
230 });
231
232 test("only dependent getters are called and in correct order", () => {
233 patchWithCleanup(AppStore.prototype, {
234 get ab() {
235 const result = super.ab;
236 unorderedStep("ab");
237 return result;
238 },
239 get abc() {
240 const result = super.abc;
241 unorderedStep("abc");
242 return result;
243 },
244 get bc() {
245 const result = super.bc;
246 unorderedStep("bc");
247 return result;
248 },
249 get cd() {
250 const result = super.cd;
251 unorderedStep("cd");
252 return result;
253 },
254 get x() {
255 const result = super.x;
256 unorderedStep("x");
257 return result;
258 },
259 get y() {
260 const result = super.y;
261 unorderedStep("y");
262 return result;
263 },
264 });
265 const store = reactive(new AppStore());
266
267 expect(store.y).toBe(0);
268 verifyUnorderedSteps(["ab", "bc", "cd", "abc", "x", "y"], [["ab", "abc", "x", "y"]]);
269
270 store.a = 1;
271
272 expect(store.y).toBe(10);
273 verifyUnorderedSteps(["ab", "abc", "x", "y"], [["ab", "abc", "x", "y"]]);
274
275 store.b = 1;
276 expect(store.y).toBe(21);
277
278 verifyUnorderedSteps(
279 ["ab", "bc", "abc", "x", "y"],
280 [
281 ["ab", "abc", "x", "y"],
282 ["bc", "x", "y"],
283 ]
284 );
285
286 store.c = 1;
287 expect(store.y).toBe(24);
288
289 verifyUnorderedSteps(
290 ["abc", "bc", "cd", "x", "y"],
291 [
292 ["abc", "x", "y"],
293 ["bc", "x", "y"],
294 ["cd", "y"],
295 ]
296 );
297
298 store.d = 1;
299 expect(store.y).toBe(25);
300
301 verifyUnorderedSteps(["cd", "y"], [["cd", "y"]]);
302 });
303
304 test("dynamically creates a lazy getter", () => {
305 class DemoClass extends WithLazyGetterTrap {
306 constructor(params = {}) {
307 super(params);
308 }
309 }
310
311 const reactiveObj = reactive(new DemoClass());
312 reactiveObj.name = "demo";
313
314 let computeCallCount = 0;
315 function computeValue() {
316 computeCallCount++;
317 return "Hello " + this.name;
318 }
319
320 createLazyGetter(reactiveObj, "hello", computeValue);
321
322 expect(reactiveObj.hello).toBe("Hello demo");
323 expect(computeCallCount).toBe(1);
324
325 // On the second call, the computed method is not executed again.
326 expect(reactiveObj.hello).toBe("Hello demo");
327 expect(computeCallCount).toBe(1);
328
329 // Modifying the value will invalidate the computed value
330 expect(computeCallCount).toBe(1);
331 reactiveObj.name = "World";
332 expect(reactiveObj.hello).toBe("Hello World");
333 expect(computeCallCount).toBe(2);
334
335 reactiveObj.notRelatedValue = 1;
336 expect(reactiveObj.hello).toBe("Hello World");
337 expect(computeCallCount).toBe(2);
338 });