Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-beans-enter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/web-worker-runtime": patch
---

feat: support for using `lynx.queueMicrotask`.
6 changes: 6 additions & 0 deletions .changeset/fluffy-insects-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lynx-js/testing-environment": patch
"@lynx-js/react": patch
---

rename @lynx-js/test-environment to @lynx-js/testing-environment
2 changes: 1 addition & 1 deletion .changeset/young-seals-train.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"@lynx-js/test-environment": minor
"@lynx-js/testing-environment": minor
---

Switch to ESM package format by setting `"type": "module"`.
1 change: 1 addition & 0 deletions cspell.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
// lighthouse CI
"lhci",
"lh",
"queueMicrotask",
],
// Glob
"ignorePaths": [
Expand Down
4 changes: 2 additions & 2 deletions packages/react/runtime/src/lynx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { initDelayUnmount } from './lifecycle/delayUnmount.js';
import { replaceCommitHook, replaceRequestAnimationFrame } from './lifecycle/patch/commit.js';
import { injectUpdateMainThread } from './lifecycle/patch/updateMainThread.js';
import { injectCalledByNative } from './lynx/calledByNative.js';
import { setupLynxEnv } from './lynx/env.js';
import { setupLynxTestingEnv } from './lynx/env.js';
import { injectLepusMethods } from './lynx/injectLepusMethods.js';
import { initTimingAPI } from './lynx/performance.js';
import { injectTt } from './lynx/tt.js';
Expand Down Expand Up @@ -53,4 +53,4 @@ if (__JS__) {
}
}

setupLynxEnv();
setupLynxTestingEnv();
2 changes: 1 addition & 1 deletion packages/react/runtime/src/lynx/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// LICENSE file in the root directory of this source tree.
import type { DataProcessorDefinition } from '../lynx-api.js';

export function setupLynxEnv(): void {
export function setupLynxTestingEnv(): void {
if (!__LEPUS__) {
const { initData, updateData } = lynxCoreInject.tt._params;
// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion packages/react/testing-library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('renders options.wrapper around node', async () => {
});
```

💡 Since our testing environment (`@lynx-js/test-environment`) is based on jsdom, You may also be interested in installing `@testing-library/jest-dom` so you can use
💡 Since our testing environment (`@lynx-js/testing-environment`) is based on jsdom, You may also be interested in installing `@testing-library/jest-dom` so you can use
[the custom jest matchers](https://github.com/testing-library/jest-dom).

## Examples
Expand Down
15 changes: 0 additions & 15 deletions packages/react/testing-library/api-extractor.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import { BoundFunction } from '@testing-library/dom';
import { ComponentChild } from 'preact';
import { ComponentType } from 'preact';
import { ElementTree } from '@lynx-js/test-environment';
import { LynxElement } from '@lynx-js/test-environment';
import { LynxEnv } from '@lynx-js/test-environment';
import { ElementTree } from '@lynx-js/testing-environment';
import { LynxElement } from '@lynx-js/testing-environment';
import { LynxTestingEnv } from '@lynx-js/testing-environment';
import { Queries } from '@testing-library/dom';
import { queries } from '@testing-library/dom';

Expand All @@ -18,7 +18,7 @@ export function cleanup(): void;

export { ElementTree }

export { LynxEnv }
export { LynxTestingEnv }

// @public
export function render<Q extends Queries>(
Expand Down
5 changes: 2 additions & 3 deletions packages/react/testing-library/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"name": "@lynx-js/react-lynx-testing-library",
"name": "@lynx-js/reactlynx-testing-library",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"api-extractor": "api-extractor run --verbose",
"build": "rslib build",
"dev": "rslib build --watch",
"test": "vitest",
"test:ui": "vitest --ui"
},
"devDependencies": {
"@lynx-js/react": "workspace:*",
"@lynx-js/test-environment": "workspace:*",
"@lynx-js/testing-environment": "workspace:*",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ test('state change will cause re-render', async () => {

const isBackground = !__MAIN_THREAD__;

const callLepusMethod = lynxEnv.backgroundThread.lynx.getNativeApp().callLepusMethod;
const callLepusMethod = lynxTestingEnv.backgroundThread.lynx.getNativeApp().callLepusMethod;
// callLepusMethodCalls such as rLynxChange
globalThis.lynxEnv.switchToMainThread();
globalThis.lynxTestingEnv.switchToMainThread();
expect(callLepusMethod.mock.calls).toMatchInlineSnapshot(`
[
[
Expand Down Expand Up @@ -133,7 +133,7 @@ test('state change will cause re-render', async () => {

// restore the original thread state
if (isBackground) {
globalThis.lynxEnv.switchToBackgroundThread();
globalThis.lynxTestingEnv.switchToBackgroundThread();
}

expect(elementTree.root).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('rerender will flush pending hooks effects', async () => {
const { rerender } = render(<Component />);
const { findByText } = rerender(<Component />);
vi.spyOn(lynx.getNativeApp(), 'callLepusMethod');
const callLepusMethod = lynxEnv.backgroundThread.lynx.getNativeApp().callLepusMethod;
const callLepusMethod = lynxTestingEnv.backgroundThread.lynx.getNativeApp().callLepusMethod;
expect(callLepusMethod.mock.calls).toMatchInlineSnapshot(`[]`);

await findByText('1');
Expand Down
2 changes: 1 addition & 1 deletion packages/react/testing-library/src/env/vitest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import env from '@lynx-js/test-environment/env/vitest';
import env from '@lynx-js/testing-environment/env/vitest';

export default env;
8 changes: 4 additions & 4 deletions packages/react/testing-library/src/fire-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export const fireEvent: any = (elemOrNodesRef, ...args) => {
const isMainThread = __MAIN_THREAD__;

// switch to background thread
lynxEnv.switchToBackgroundThread();
lynxTestingEnv.switchToBackgroundThread();

const elem = getElement(elemOrNodesRef);

let ans = domFireEvent(elem, ...args);

if (isMainThread) {
// switch back to main thread
lynxEnv.switchToMainThread();
lynxTestingEnv.switchToMainThread();
}

return ans;
Expand Down Expand Up @@ -153,7 +153,7 @@ Object.keys(eventMap).forEach((key) => {
fireEvent[key] = (elemOrNodesRef, init = {}) => {
const isMainThread = __MAIN_THREAD__;
// switch to background thread
lynxEnv.switchToBackgroundThread();
lynxTestingEnv.switchToBackgroundThread();

const elem = getElement(elemOrNodesRef);
const eventType = init?.['eventType'] || 'bindEvent';
Expand All @@ -177,7 +177,7 @@ Object.keys(eventMap).forEach((key) => {

if (isMainThread) {
// switch back to main thread
lynxEnv.switchToMainThread();
lynxTestingEnv.switchToMainThread();
}

return ans;
Expand Down
4 changes: 2 additions & 2 deletions packages/react/testing-library/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ if (
if (typeof afterEach === 'function') {
afterEach(() => {
cleanup();
lynxEnv.resetLynxEnv();
lynxTestingEnv.reset();
});
} else if (typeof teardown === 'function') {
// eslint-disable-next-line no-undef
teardown(() => {
cleanup();
lynxEnv.resetLynxEnv();
lynxTestingEnv.reset();
});
}
}
Expand Down
18 changes: 9 additions & 9 deletions packages/react/testing-library/src/pure.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,48 +59,48 @@ export function render(
const compMainThread = cloneElement(comp);
const compBackgroundThread = cloneElement(comp);

globalThis.lynxEnv.switchToMainThread();
globalThis.lynxTestingEnv.switchToMainThread();
__root.__jsx = enableMainThread ? compMainThread : null;
renderPage();
if (enableBackgroundThread) {
globalThis.lynxEnv.switchToBackgroundThread();
globalThis.lynxTestingEnv.switchToBackgroundThread();
act(() => {
preactRender(compBackgroundThread, __root);
flushDelayedLifecycleEvents();
});
}

return {
container: lynxEnv.mainThread.elementTree.root,
container: lynxTestingEnv.mainThread.elementTree.root,
unmount: cleanup,
rerender: (rerenderUi) => {
lynxEnv.resetLynxEnv();
lynxTestingEnv.reset();
return render(wrapUiIfNeeded(rerenderUi), {
queries,
wrapper: WrapperComponent,
enableMainThread,
enableBackgroundThread,
});
},
...getQueriesForElement(lynxEnv.mainThread.elementTree.root, queries),
...getQueriesForElement(lynxTestingEnv.mainThread.elementTree.root, queries),
};
}

export function cleanup() {
const isMainThread = __MAIN_THREAD__;

// Ensure componentWillUnmount is called
globalThis.lynxEnv.switchToBackgroundThread();
globalThis.lynxTestingEnv.switchToBackgroundThread();
act(() => {
preactRender(null, __root);
});

lynxEnv.mainThread.elementTree.root = undefined;
lynxTestingEnv.mainThread.elementTree.root = undefined;
clearPage();
lynxEnv.jsdom.window.document.body.innerHTML = '';
lynxTestingEnv.jsdom.window.document.body.innerHTML = '';

if (isMainThread) {
globalThis.lynxEnv.switchToMainThread();
globalThis.lynxTestingEnv.switchToMainThread();
}
}

Expand Down
20 changes: 10 additions & 10 deletions packages/react/testing-library/src/vitest-global-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { initWorklet } from '../../worklet-runtime/lib/workletRuntime.js';
const {
onInjectMainThreadGlobals,
onInjectBackgroundThreadGlobals,
onResetLynxEnv,
onResetLynxTestingEnv,
onSwitchedToMainThread,
onSwitchedToBackgroundThread,
onInitWorkletRuntime,
Expand Down Expand Up @@ -123,7 +123,7 @@ globalThis.onInjectBackgroundThreadGlobals = (target) => {

// TODO: can we only inject to target(mainThread.globalThis) instead of globalThis?
// packages/react/runtime/src/lynx.ts
// intercept lynxCoreInject assignments to lynxEnv.backgroundThread.globalThis.lynxCoreInject
// intercept lynxCoreInject assignments to lynxTestingEnv.backgroundThread.globalThis.lynxCoreInject
const oldLynxCoreInject = globalThis.lynxCoreInject;
globalThis.lynxCoreInject = target.lynxCoreInject;
injectTt();
Expand All @@ -133,20 +133,20 @@ globalThis.onInjectBackgroundThreadGlobals = (target) => {
deinitGlobalSnapshotPatch();
clearCommitTaskId();
};
globalThis.onResetLynxEnv = () => {
if (onResetLynxEnv) {
onResetLynxEnv();
globalThis.onResetLynxTestingEnv = () => {
if (onResetLynxTestingEnv) {
onResetLynxTestingEnv();
}
if (process.env.DEBUG) {
console.log('onResetLynxEnv');
console.log('onResetLynxTestingEnv');
}

flushDelayedLifecycleEvents();
destroyWorklet();

lynxEnv.switchToMainThread();
lynxTestingEnv.switchToMainThread();
initEventListeners();
lynxEnv.switchToBackgroundThread();
lynxTestingEnv.switchToBackgroundThread();
};

globalThis.onSwitchedToMainThread = () => {
Expand All @@ -173,8 +173,8 @@ globalThis.onSwitchedToBackgroundThread = () => {
};

globalThis.onInjectMainThreadGlobals(
globalThis.lynxEnv.mainThread.globalThis,
globalThis.lynxTestingEnv.mainThread.globalThis,
);
globalThis.onInjectBackgroundThreadGlobals(
globalThis.lynxEnv.backgroundThread.globalThis,
globalThis.lynxTestingEnv.backgroundThread.globalThis,
);
2 changes: 1 addition & 1 deletion packages/react/testing-library/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"build": {
"dependsOn": [
"@lynx-js/react-transform#build:wasm",
"@lynx-js/test-environment#build"
"@lynx-js/testing-environment#build"
],
"inputs": [
"src"
Expand Down
4 changes: 2 additions & 2 deletions packages/react/testing-library/types/entry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/

import { queries, Queries, BoundFunction } from '@testing-library/dom';
import { LynxElement, type ElementTree, type LynxEnv } from '@lynx-js/test-environment';
import { LynxElement, type ElementTree, type LynxTestingEnv } from '@lynx-js/testing-environment';
import { ComponentChild, ComponentType } from 'preact';
export * from '@testing-library/dom';
export { ElementTree, LynxEnv };
export { ElementTree, LynxTestingEnv };

/**
* The options for {@link render}.
Expand Down
6 changes: 3 additions & 3 deletions packages/react/testing-library/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// @ts-nocheck
export * from '../dist/index.d.ts';
import { ElementTree, LynxEnv } from '../dist/index.d.ts';
import { ElementTree, LynxTestingEnv } from '../dist/index.d.ts';

declare global {
var lynxEnv: LynxEnv;
var lynxTestingEnv: LynxTestingEnv;
var elementTree: ElementTree;

function onInjectBackgroundThreadGlobals(globals: any): void;
function onInjectMainThreadGlobals(globals: any): void;
function onSwitchedToBackgroundThread(): void;
function onSwitchedToMainThread(): void;
function onResetLynxEnv(): void;
function onResetLynxTestingEnv(): void;
function onInitWorkletRuntime(): void;
}
4 changes: 2 additions & 2 deletions packages/react/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"@lynx-js/react-refresh#build",
"@lynx-js/react-transform#build:wasm",
"@lynx-js/react-worklet-runtime#build",
"@lynx-js/react-lynx-testing-library#build",
"@lynx-js/test-environment#build"
"@lynx-js/reactlynx-testing-library#build",
"@lynx-js/testing-environment#build"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/testing-library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Unit testing library for lynx, same as https://github.com/testing-library.

| Package | Description | Equivalent |
| ---------------------------------------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------ |
| [@lynx-js/test-environment](./lynx-environment/) | Lynx equivalent of jsdom | [jsdom](https://github.com/jsdom/jsdom) |
| [@lynx-js/testing-environment](./lynx-environment/) | Lynx equivalent of jsdom | [jsdom](https://github.com/jsdom/jsdom) |
| [@lynx-js/react/testing-library](../react/testing-library) | Lynx equivalent of preact-testing-library | [@testing-library/preact](https://github.com/testing-library/preact-testing-library) |

## Documentation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @lynx-js/test-environment
# @lynx-js/testing-environment

## 0.0.1

Expand Down
Loading