-
Notifications
You must be signed in to change notification settings - Fork 122
feat: add testing library for ReactLynx #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "@lynx-js/react-lynx-testing-library": patch | ||
| "@lynx-js/lynx-dom-testing-library": patch | ||
| "@lynx-js/lynx-dom-jest-matchers": patch | ||
| "@lynx-js/lynx-dom": patch | ||
| --- | ||
|
|
||
| Add testing library for ReactLynx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { options } from 'preact'; | ||
| import type { VNode } from 'preact'; | ||
| import { COMPONENT, DIFF, DIFFED, FORCE } from '../renderToOpcodes/constants.js'; | ||
|
|
||
| export function runWithForce(cb: () => void): void { | ||
| // save vnode and its `_component` in WeakMap | ||
| const m = new WeakMap<VNode, any>(); | ||
|
|
||
| const oldDiff = options[DIFF]; | ||
|
|
||
| options[DIFF] = (vnode: VNode) => { | ||
| if (oldDiff) { | ||
| oldDiff(vnode); | ||
| } | ||
|
|
||
| // when `options[DIFF]` is called, a newVnode is passed in | ||
| // so its `vnode[COMPONENT]` should be null, | ||
| // but it will be set later | ||
| Object.defineProperty(vnode, COMPONENT, { | ||
| configurable: true, | ||
| set(c) { | ||
| m.set(vnode, c); | ||
| if (c) { | ||
| c[FORCE] = true; | ||
| } | ||
| }, | ||
| get() { | ||
| return m.get(vnode); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const oldDiffed = options[DIFFED]; | ||
|
|
||
| options[DIFFED] = (vnode: VNode) => { | ||
| if (oldDiffed) { | ||
| oldDiffed(vnode); | ||
| } | ||
|
|
||
| // delete is a reverse operation of previous `Object.defineProperty` | ||
| delete vnode[COMPONENT]; | ||
| // restore | ||
| vnode[COMPONENT] = m.get(vnode); | ||
| }; | ||
|
|
||
| try { | ||
| cb(); | ||
| } finally { | ||
| options[DIFF] = oldDiff as (vnode: VNode) => void; | ||
| options[DIFFED] = oldDiffed as (vnode: VNode) => void; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright 2024 The Lynx Authors. All rights reserved. | ||
| // Licensed under the Apache License Version 2.0 that can be found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
| import type { JsFnHandle, Worklet } from '@lynx-js/react/worklet-runtime/bindings'; | ||
| import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings'; | ||
|
|
||
| import { onPostWorkletCtx } from './ctx.js'; | ||
| import { enableRunOnBackground } from './functionality.js'; | ||
| import { lynxWorkletJsImpl } from './jsImpl.js'; | ||
| /** | ||
| * transform args of `runOnJS()`. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function transformToWorklet(obj: Function): JsFnHandle { | ||
| const impl = lynxWorkletJsImpl(); | ||
| const id = impl ? ++impl._workletJsFnLastId : 0; | ||
| if (typeof obj !== 'function') { | ||
| // We save the error message in the object, so that we can throw it later when the function is called on the main thread. | ||
| return { | ||
| _jsFnId: id, | ||
| _error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`, | ||
| }; | ||
| } | ||
| return { | ||
| _jsFnId: id, | ||
| _fn: obj, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * `runOnMainThread` allows triggering main thread functions on the main thread asynchronously. | ||
| * @param fn - The main thread functions to be called. | ||
| * @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread. | ||
| * @public | ||
| */ | ||
| export function runOnMainThread<Fn extends (...args: any[]) => any>(fn: Fn): (...args: Parameters<Fn>) => void { | ||
| if (__LEPUS__) { | ||
| throw new Error('runOnMainThread can only be used on the background thread.'); | ||
| } | ||
| const impl = lynxWorkletJsImpl(); | ||
| if (!impl) { | ||
| throw new Error('runOnMainThread requires Lynx sdk version 2.14.'); | ||
| } | ||
| return (...params: any[]): void => { | ||
| onPostWorkletCtx(fn as any as Worklet); | ||
| lynx.getCoreContext!().dispatchEvent({ | ||
| type: WorkletEvents.runWorkletCtx, | ||
| data: JSON.stringify({ | ||
| worklet: fn, | ||
| params, | ||
| }), | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * `runOnBackground` allows triggering js functions on the js context asynchronously. | ||
| * @param f - The js function to be called. | ||
| * @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the js context. | ||
| * @public | ||
| */ | ||
| export function runOnBackground<Fn extends (...args: any[]) => any>(f: Fn): (...args: Parameters<Fn>) => void { | ||
| if (!enableRunOnBackground()) { | ||
| throw new Error('runOnBackground requires Lynx sdk version 2.16.'); | ||
| } | ||
| if (__JS__) { | ||
| throw new Error('runOnBackground can only be used on the main thread.'); | ||
| } | ||
| const obj = f as any as JsFnHandle; | ||
| if (obj._error) { | ||
| throw new Error(obj._error); | ||
| } | ||
| return (...params: any[]): void => { | ||
| if (lynx.getJSContext) { | ||
| lynx.getJSContext().dispatchEvent({ | ||
| type: WorkletEvents.runOnBackground, | ||
| data: JSON.stringify({ | ||
| obj: { | ||
| _jsFnId: obj._jsFnId, | ||
| _execId: obj._execId!, | ||
| }, | ||
| params, | ||
| }), | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.