@lynx-js/testing-environment 0.1.9 → 0.1.11

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @lynx-js/testing-environment
2
2
 
3
+ ## 0.1.11
4
+
5
+ ### Patch Changes
6
+
7
+ - Remove element api calls alog by default, and only enable it when `__ALOG_ELEMENT_API__` is defined to `true` or environment variable `REACT_ALOG_ELEMENT_API` is set to `true`. ([#2192](https://github.com/lynx-family/lynx-stack/pull/2192))
8
+
9
+ ## 0.1.10
10
+
11
+ ### Patch Changes
12
+
13
+ - Fix the error "lynxTestingEnv is not defined" ([#2118](https://github.com/lynx-family/lynx-stack/pull/2118))
14
+
3
15
  ## 0.1.9
4
16
 
5
17
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -72,12 +72,11 @@ function __injectElementApi(target) {
72
72
  function createPolyfills() {
73
73
  const app = {
74
74
  callLepusMethod: (...rLynxChange)=>{
75
- var _rLynxChange_;
76
75
  const isBackground = !__MAIN_THREAD__;
77
76
  globalThis.lynxTestingEnv.switchToMainThread();
78
77
  globalThis[rLynxChange[0]](rLynxChange[1]);
79
78
  globalThis.lynxTestingEnv.switchToBackgroundThread();
80
- null == (_rLynxChange_ = rLynxChange[2]) || _rLynxChange_.call(rLynxChange);
79
+ rLynxChange[2]?.();
81
80
  globalThis.lynxTestingEnv.switchToMainThread();
82
81
  if (isBackground) globalThis.lynxTestingEnv.switchToBackgroundThread();
83
82
  },
@@ -118,6 +117,7 @@ function createPolyfills() {
118
117
  };
119
118
  const ee = new (external_events_default())();
120
119
  ee.dispatchEvent = ({ type, data })=>{
120
+ if (!globalThis.lynxTestingEnv) return;
121
121
  const origin = __MAIN_THREAD__ ? 'CoreContext' : 'JSContext';
122
122
  if ('CoreContext' === origin) lynxTestingEnv.switchToBackgroundThread();
123
123
  else lynxTestingEnv.switchToMainThread();
@@ -136,10 +136,8 @@ function createPolyfills() {
136
136
  const isBackground = !__MAIN_THREAD__;
137
137
  globalThis.lynxTestingEnv.switchToMainThread();
138
138
  let ans;
139
- if ('worklet-runtime' === chunkName) {
140
- var _globalThis_onInitWorkletRuntime, _globalThis;
141
- ans = null == (_globalThis_onInitWorkletRuntime = (_globalThis = globalThis).onInitWorkletRuntime) ? void 0 : _globalThis_onInitWorkletRuntime.call(_globalThis);
142
- } else throw new Error(`__LoadLepusChunk: Unknown chunk name: ${chunkName}`);
139
+ if ('worklet-runtime' === chunkName) ans = globalThis.onInitWorkletRuntime?.();
140
+ else throw new Error(`__LoadLepusChunk: Unknown chunk name: ${chunkName}`);
143
141
  if (isBackground) globalThis.lynxTestingEnv.switchToBackgroundThread();
144
142
  return ans;
145
143
  }
@@ -159,13 +157,13 @@ function createPreconfiguredConsole() {
159
157
  return console1;
160
158
  }
161
159
  function injectMainThreadGlobals(target, polyfills) {
162
- var _globalThis_onInjectMainThreadGlobals, _globalThis;
163
160
  __injectElementApi(target);
164
161
  const { performance, CoreContext, JsContext, __LoadLepusChunk } = polyfills || {};
165
162
  if (void 0 === target) target = globalThis;
166
163
  target.__DEV__ = true;
167
164
  target.__PROFILE__ = true;
168
165
  target.__ALOG__ = true;
166
+ target.__ALOG_ELEMENT_API__ = true;
169
167
  target.__JS__ = false;
170
168
  target.__LEPUS__ = true;
171
169
  target.__BACKGROUND__ = false;
@@ -175,8 +173,28 @@ function injectMainThreadGlobals(target, polyfills) {
175
173
  target.__TESTING_FORCE_RENDER_TO_OPCODE__ = false;
176
174
  target.__ENABLE_SSR__ = false;
177
175
  target.globDynamicComponentEntry = '__Card__';
176
+ const native = {
177
+ _listeners: {},
178
+ onTriggerEvent: void 0,
179
+ postMessage: (_message)=>{},
180
+ addEventListener: (type, listener)=>{
181
+ if (!native._listeners[type]) native._listeners[type] = [];
182
+ native._listeners[type].push(listener);
183
+ },
184
+ removeEventListener: (type, listener)=>{
185
+ if (native._listeners[type]) native._listeners[type] = native._listeners[type].filter((l)=>l !== listener);
186
+ },
187
+ dispatchEvent: (event)=>{
188
+ const listeners = native._listeners[event.type];
189
+ if (listeners) listeners.forEach((listener)=>listener(event));
190
+ return {
191
+ canceled: false
192
+ };
193
+ }
194
+ };
178
195
  target.lynx = {
179
196
  performance,
197
+ getNative: ()=>native,
180
198
  getCoreContext: ()=>CoreContext,
181
199
  getJSContext: ()=>JsContext,
182
200
  reportError: (e)=>{
@@ -187,7 +205,7 @@ function injectMainThreadGlobals(target, polyfills) {
187
205
  target.cancelAnimationFrame = clearTimeout;
188
206
  target.console = createPreconfiguredConsole();
189
207
  target.__LoadLepusChunk = __LoadLepusChunk;
190
- null == (_globalThis_onInjectMainThreadGlobals = (_globalThis = globalThis).onInjectMainThreadGlobals) || _globalThis_onInjectMainThreadGlobals.call(_globalThis, target);
208
+ globalThis.onInjectMainThreadGlobals?.(target);
191
209
  }
192
210
  const IGNORE_LIST_GLOBALS = [
193
211
  'globalThis',
@@ -220,12 +238,12 @@ class NodesRef {
220
238
  }
221
239
  }
222
240
  function injectBackgroundThreadGlobals(target, polyfills) {
223
- var _globalThis_onInjectBackgroundThreadGlobals, _globalThis;
224
241
  const { app, performance, CoreContext, JsContext, __LoadLepusChunk } = polyfills || {};
225
242
  if (void 0 === target) target = globalThis;
226
243
  target.__DEV__ = true;
227
244
  target.__PROFILE__ = true;
228
245
  target.__ALOG__ = true;
246
+ target.__ALOG_ELEMENT_API__ = true;
229
247
  target.__JS__ = true;
230
248
  target.__LEPUS__ = false;
231
249
  target.__BACKGROUND__ = true;
@@ -283,7 +301,7 @@ function injectBackgroundThreadGlobals(target, polyfills) {
283
301
  lynxSdkVersion: '3.0'
284
302
  };
285
303
  target.__LoadLepusChunk = __LoadLepusChunk;
286
- null == (_globalThis_onInjectBackgroundThreadGlobals = (_globalThis = globalThis).onInjectBackgroundThreadGlobals) || _globalThis_onInjectBackgroundThreadGlobals.call(_globalThis, target);
304
+ globalThis.onInjectBackgroundThreadGlobals?.(target);
287
305
  }
288
306
  class LynxTestingEnv {
289
307
  injectGlobals() {
@@ -292,38 +310,34 @@ class LynxTestingEnv {
292
310
  injectMainThreadGlobals(this.mainThread.globalThis, polyfills);
293
311
  }
294
312
  switchToBackgroundThread() {
295
- var _globalThis_onSwitchedToBackgroundThread, _globalThis;
296
313
  this.originals = new Map();
297
314
  Object.getOwnPropertyNames(this.backgroundThread.globalThis).forEach((key)=>{
298
315
  if (IGNORE_LIST_GLOBALS.includes(key)) return;
299
316
  this.originals.set(key, global[key]);
300
317
  global[key] = this.backgroundThread.globalThis[key];
301
318
  });
302
- null == (_globalThis = globalThis) || null == (_globalThis_onSwitchedToBackgroundThread = _globalThis.onSwitchedToBackgroundThread) || _globalThis_onSwitchedToBackgroundThread.call(_globalThis);
319
+ globalThis?.onSwitchedToBackgroundThread?.();
303
320
  }
304
321
  switchToMainThread() {
305
- var _globalThis_onSwitchedToMainThread, _globalThis;
306
322
  this.originals = new Map();
307
323
  Object.getOwnPropertyNames(this.mainThread.globalThis).forEach((key)=>{
308
324
  if (IGNORE_LIST_GLOBALS.includes(key)) return;
309
325
  this.originals.set(key, global[key]);
310
326
  global[key] = this.mainThread.globalThis[key];
311
327
  });
312
- null == (_globalThis = globalThis) || null == (_globalThis_onSwitchedToMainThread = _globalThis.onSwitchedToMainThread) || _globalThis_onSwitchedToMainThread.call(_globalThis);
328
+ globalThis?.onSwitchedToMainThread?.();
313
329
  }
314
330
  clearGlobal() {
315
- var _this_originals, _this_originals1;
316
- null == (_this_originals = this.originals) || _this_originals.forEach((v, k)=>{
331
+ this.originals?.forEach((v, k)=>{
317
332
  global[k] = v;
318
333
  });
319
- null == (_this_originals1 = this.originals) || _this_originals1.clear();
334
+ this.originals?.clear();
320
335
  }
321
336
  reset() {
322
- var _globalThis_onResetLynxTestingEnv, _globalThis;
323
337
  this.injectGlobals();
324
338
  this.switchToMainThread();
325
339
  this.switchToBackgroundThread();
326
- null == (_globalThis_onResetLynxTestingEnv = (_globalThis = globalThis).onResetLynxTestingEnv) || _globalThis_onResetLynxTestingEnv.call(_globalThis);
340
+ globalThis.onResetLynxTestingEnv?.();
327
341
  }
328
342
  constructor(jsdom){
329
343
  _define_property(this, "originals", new Map());
package/dist/index.js CHANGED
@@ -33,12 +33,11 @@ function __injectElementApi(target) {
33
33
  function createPolyfills() {
34
34
  const app = {
35
35
  callLepusMethod: (...rLynxChange)=>{
36
- var _rLynxChange_;
37
36
  const isBackground = !__MAIN_THREAD__;
38
37
  globalThis.lynxTestingEnv.switchToMainThread();
39
38
  globalThis[rLynxChange[0]](rLynxChange[1]);
40
39
  globalThis.lynxTestingEnv.switchToBackgroundThread();
41
- null == (_rLynxChange_ = rLynxChange[2]) || _rLynxChange_.call(rLynxChange);
40
+ rLynxChange[2]?.();
42
41
  globalThis.lynxTestingEnv.switchToMainThread();
43
42
  if (isBackground) globalThis.lynxTestingEnv.switchToBackgroundThread();
44
43
  },
@@ -79,6 +78,7 @@ function createPolyfills() {
79
78
  };
80
79
  const ee = new events();
81
80
  ee.dispatchEvent = ({ type, data })=>{
81
+ if (!globalThis.lynxTestingEnv) return;
82
82
  const origin = __MAIN_THREAD__ ? 'CoreContext' : 'JSContext';
83
83
  if ('CoreContext' === origin) lynxTestingEnv.switchToBackgroundThread();
84
84
  else lynxTestingEnv.switchToMainThread();
@@ -97,10 +97,8 @@ function createPolyfills() {
97
97
  const isBackground = !__MAIN_THREAD__;
98
98
  globalThis.lynxTestingEnv.switchToMainThread();
99
99
  let ans;
100
- if ('worklet-runtime' === chunkName) {
101
- var _globalThis_onInitWorkletRuntime, _globalThis;
102
- ans = null == (_globalThis_onInitWorkletRuntime = (_globalThis = globalThis).onInitWorkletRuntime) ? void 0 : _globalThis_onInitWorkletRuntime.call(_globalThis);
103
- } else throw new Error(`__LoadLepusChunk: Unknown chunk name: ${chunkName}`);
100
+ if ('worklet-runtime' === chunkName) ans = globalThis.onInitWorkletRuntime?.();
101
+ else throw new Error(`__LoadLepusChunk: Unknown chunk name: ${chunkName}`);
104
102
  if (isBackground) globalThis.lynxTestingEnv.switchToBackgroundThread();
105
103
  return ans;
106
104
  }
@@ -120,13 +118,13 @@ function createPreconfiguredConsole() {
120
118
  return console1;
121
119
  }
122
120
  function injectMainThreadGlobals(target, polyfills) {
123
- var _globalThis_onInjectMainThreadGlobals, _globalThis;
124
121
  __injectElementApi(target);
125
122
  const { performance, CoreContext, JsContext, __LoadLepusChunk } = polyfills || {};
126
123
  if (void 0 === target) target = globalThis;
127
124
  target.__DEV__ = true;
128
125
  target.__PROFILE__ = true;
129
126
  target.__ALOG__ = true;
127
+ target.__ALOG_ELEMENT_API__ = true;
130
128
  target.__JS__ = false;
131
129
  target.__LEPUS__ = true;
132
130
  target.__BACKGROUND__ = false;
@@ -136,8 +134,28 @@ function injectMainThreadGlobals(target, polyfills) {
136
134
  target.__TESTING_FORCE_RENDER_TO_OPCODE__ = false;
137
135
  target.__ENABLE_SSR__ = false;
138
136
  target.globDynamicComponentEntry = '__Card__';
137
+ const native = {
138
+ _listeners: {},
139
+ onTriggerEvent: void 0,
140
+ postMessage: (_message)=>{},
141
+ addEventListener: (type, listener)=>{
142
+ if (!native._listeners[type]) native._listeners[type] = [];
143
+ native._listeners[type].push(listener);
144
+ },
145
+ removeEventListener: (type, listener)=>{
146
+ if (native._listeners[type]) native._listeners[type] = native._listeners[type].filter((l)=>l !== listener);
147
+ },
148
+ dispatchEvent: (event)=>{
149
+ const listeners = native._listeners[event.type];
150
+ if (listeners) listeners.forEach((listener)=>listener(event));
151
+ return {
152
+ canceled: false
153
+ };
154
+ }
155
+ };
139
156
  target.lynx = {
140
157
  performance,
158
+ getNative: ()=>native,
141
159
  getCoreContext: ()=>CoreContext,
142
160
  getJSContext: ()=>JsContext,
143
161
  reportError: (e)=>{
@@ -148,7 +166,7 @@ function injectMainThreadGlobals(target, polyfills) {
148
166
  target.cancelAnimationFrame = clearTimeout;
149
167
  target.console = createPreconfiguredConsole();
150
168
  target.__LoadLepusChunk = __LoadLepusChunk;
151
- null == (_globalThis_onInjectMainThreadGlobals = (_globalThis = globalThis).onInjectMainThreadGlobals) || _globalThis_onInjectMainThreadGlobals.call(_globalThis, target);
169
+ globalThis.onInjectMainThreadGlobals?.(target);
152
170
  }
153
171
  const IGNORE_LIST_GLOBALS = [
154
172
  'globalThis',
@@ -181,12 +199,12 @@ class NodesRef {
181
199
  }
182
200
  }
183
201
  function injectBackgroundThreadGlobals(target, polyfills) {
184
- var _globalThis_onInjectBackgroundThreadGlobals, _globalThis;
185
202
  const { app, performance, CoreContext, JsContext, __LoadLepusChunk } = polyfills || {};
186
203
  if (void 0 === target) target = globalThis;
187
204
  target.__DEV__ = true;
188
205
  target.__PROFILE__ = true;
189
206
  target.__ALOG__ = true;
207
+ target.__ALOG_ELEMENT_API__ = true;
190
208
  target.__JS__ = true;
191
209
  target.__LEPUS__ = false;
192
210
  target.__BACKGROUND__ = true;
@@ -244,7 +262,7 @@ function injectBackgroundThreadGlobals(target, polyfills) {
244
262
  lynxSdkVersion: '3.0'
245
263
  };
246
264
  target.__LoadLepusChunk = __LoadLepusChunk;
247
- null == (_globalThis_onInjectBackgroundThreadGlobals = (_globalThis = globalThis).onInjectBackgroundThreadGlobals) || _globalThis_onInjectBackgroundThreadGlobals.call(_globalThis, target);
265
+ globalThis.onInjectBackgroundThreadGlobals?.(target);
248
266
  }
249
267
  class LynxTestingEnv {
250
268
  injectGlobals() {
@@ -253,38 +271,34 @@ class LynxTestingEnv {
253
271
  injectMainThreadGlobals(this.mainThread.globalThis, polyfills);
254
272
  }
255
273
  switchToBackgroundThread() {
256
- var _globalThis_onSwitchedToBackgroundThread, _globalThis;
257
274
  this.originals = new Map();
258
275
  Object.getOwnPropertyNames(this.backgroundThread.globalThis).forEach((key)=>{
259
276
  if (IGNORE_LIST_GLOBALS.includes(key)) return;
260
277
  this.originals.set(key, global[key]);
261
278
  global[key] = this.backgroundThread.globalThis[key];
262
279
  });
263
- null == (_globalThis = globalThis) || null == (_globalThis_onSwitchedToBackgroundThread = _globalThis.onSwitchedToBackgroundThread) || _globalThis_onSwitchedToBackgroundThread.call(_globalThis);
280
+ globalThis?.onSwitchedToBackgroundThread?.();
264
281
  }
265
282
  switchToMainThread() {
266
- var _globalThis_onSwitchedToMainThread, _globalThis;
267
283
  this.originals = new Map();
268
284
  Object.getOwnPropertyNames(this.mainThread.globalThis).forEach((key)=>{
269
285
  if (IGNORE_LIST_GLOBALS.includes(key)) return;
270
286
  this.originals.set(key, global[key]);
271
287
  global[key] = this.mainThread.globalThis[key];
272
288
  });
273
- null == (_globalThis = globalThis) || null == (_globalThis_onSwitchedToMainThread = _globalThis.onSwitchedToMainThread) || _globalThis_onSwitchedToMainThread.call(_globalThis);
289
+ globalThis?.onSwitchedToMainThread?.();
274
290
  }
275
291
  clearGlobal() {
276
- var _this_originals, _this_originals1;
277
- null == (_this_originals = this.originals) || _this_originals.forEach((v, k)=>{
292
+ this.originals?.forEach((v, k)=>{
278
293
  global[k] = v;
279
294
  });
280
- null == (_this_originals1 = this.originals) || _this_originals1.clear();
295
+ this.originals?.clear();
281
296
  }
282
297
  reset() {
283
- var _globalThis_onResetLynxTestingEnv, _globalThis;
284
298
  this.injectGlobals();
285
299
  this.switchToMainThread();
286
300
  this.switchToBackgroundThread();
287
- null == (_globalThis_onResetLynxTestingEnv = (_globalThis = globalThis).onResetLynxTestingEnv) || _globalThis_onResetLynxTestingEnv.call(_globalThis);
301
+ globalThis.onResetLynxTestingEnv?.();
288
302
  }
289
303
  constructor(jsdom){
290
304
  _define_property(this, "originals", new Map());
@@ -117,8 +117,7 @@ const initElementTree = ()=>{
117
117
  e.setAttribute(key, JSON.stringify(value));
118
118
  }
119
119
  __AddEvent(e, eventType, eventName, eventHandler) {
120
- var _e_eventMap;
121
- if (null == (_e_eventMap = e.eventMap) ? void 0 : _e_eventMap[`${eventType}:${eventName}`]) {
120
+ if (e.eventMap?.[`${eventType}:${eventName}`]) {
122
121
  e.removeEventListener(`${eventType}:${eventName}`, e.eventMap[`${eventType}:${eventName}`]);
123
122
  delete e.eventMap[`${eventType}:${eventName}`];
124
123
  }
@@ -144,8 +143,7 @@ const initElementTree = ()=>{
144
143
  });
145
144
  }
146
145
  __GetEvent(e, eventType, eventName) {
147
- var _e_eventMap;
148
- const jsFunction = null == (_e_eventMap = e.eventMap) ? void 0 : _e_eventMap[`${eventType}:${eventName}`];
146
+ const jsFunction = e.eventMap?.[`${eventType}:${eventName}`];
149
147
  if (void 0 !== jsFunction) return {
150
148
  type: eventType,
151
149
  name: eventName,
@@ -6,18 +6,6 @@
6
6
  * @public
7
7
  */
8
8
  export interface LynxElement extends HTMLElement {
9
- /**
10
- * The unique id of the element.
11
- *
12
- * @internal
13
- */
14
- $$uiSign: number;
15
- /**
16
- * The unique id of the parent of the element.
17
- *
18
- * @internal
19
- */
20
- parentComponentUniqueId: number;
21
9
  /**
22
10
  * The map of events bound to the element.
23
11
  */
@@ -89,8 +89,7 @@ const initElementTree = ()=>{
89
89
  e.setAttribute(key, JSON.stringify(value));
90
90
  }
91
91
  __AddEvent(e, eventType, eventName, eventHandler) {
92
- var _e_eventMap;
93
- if (null == (_e_eventMap = e.eventMap) ? void 0 : _e_eventMap[`${eventType}:${eventName}`]) {
92
+ if (e.eventMap?.[`${eventType}:${eventName}`]) {
94
93
  e.removeEventListener(`${eventType}:${eventName}`, e.eventMap[`${eventType}:${eventName}`]);
95
94
  delete e.eventMap[`${eventType}:${eventName}`];
96
95
  }
@@ -116,8 +115,7 @@ const initElementTree = ()=>{
116
115
  });
117
116
  }
118
117
  __GetEvent(e, eventType, eventName) {
119
- var _e_eventMap;
120
- const jsFunction = null == (_e_eventMap = e.eventMap) ? void 0 : _e_eventMap[`${eventType}:${eventName}`];
118
+ const jsFunction = e.eventMap?.[`${eventType}:${eventName}`];
121
119
  if (void 0 !== jsFunction) return {
122
120
  type: eventType,
123
121
  name: eventName,
@@ -5,14 +5,6 @@ export declare const createGlobalThis: () => LynxGlobalThis;
5
5
  * @public
6
6
  */
7
7
  export interface LynxGlobalThis {
8
- /**
9
- * @internal
10
- */
11
- _globalObject: any;
12
- /**
13
- * @internal
14
- */
15
- _globalProxy: any;
16
8
  /**
17
9
  * The globalThis object.
18
10
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/testing-environment",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "A subset of a Lynx environment to be useful for testing",
5
5
  "keywords": [
6
6
  "Lynx",
@@ -49,7 +49,7 @@
49
49
  "devDependencies": {
50
50
  "@testing-library/jest-dom": "^6.9.1",
51
51
  "@types/jsdom": "^21.1.7",
52
- "rsbuild-plugin-publint": "0.3.3"
52
+ "rsbuild-plugin-publint": "0.3.4"
53
53
  },
54
54
  "scripts": {
55
55
  "api-extractor": "api-extractor run --verbose",