-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup react-transform E2E test infra
- Loading branch information
1 parent
6802bfc
commit 08d73c9
Showing
6 changed files
with
604 additions
and
248 deletions.
There are no files selected for viewing
This file contains 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 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 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,87 @@ | ||
// @ts-expect-error - missing types | ||
import syntaxJsx from "@babel/plugin-syntax-jsx"; | ||
// @ts-expect-error - missing types | ||
import transformReactJsx from "@babel/plugin-transform-react-jsx"; | ||
import { transform } from "@babel/standalone"; | ||
import { signal } from "@preact/signals-core"; | ||
import { useSignals } from "@preact/signals-react/runtime"; | ||
import React, { createElement } from "react"; | ||
import { | ||
Root, | ||
createRoot, | ||
act, | ||
checkHangingAct, | ||
getConsoleErrorSpy, | ||
checkConsoleErrorLogs, | ||
} from "../../../react/test/shared/utils"; | ||
import signalsTransform, { PluginOptions } from "../../src/index"; | ||
|
||
function transformCode(code: string, options?: PluginOptions) { | ||
const signalsPluginConfig: any[] = [signalsTransform]; | ||
if (options) { | ||
signalsPluginConfig.push(options); | ||
} | ||
|
||
const result = transform(code, { | ||
plugins: [signalsPluginConfig, syntaxJsx, transformReactJsx], | ||
}); | ||
|
||
return result?.code || ""; | ||
} | ||
|
||
function createComponent(code: string, options?: PluginOptions) { | ||
const transformedCode = transformCode(code, options) | ||
// Remove the `import { useSignals } from "@preact/signals-react/runtime";` | ||
// line since we compiling code on the fly and can't use import statements. | ||
.trim() | ||
.split("\n") | ||
.slice(1) | ||
.join("\n"); | ||
|
||
const factory = new Function( | ||
"React", | ||
"_useSignals", | ||
`return ${transformedCode}` | ||
); | ||
return factory(React, useSignals); | ||
} | ||
|
||
describe("React Signals babel transfrom - browser E2E tests", () => { | ||
let scratch: HTMLDivElement; | ||
let root: Root; | ||
|
||
async function render(element: Parameters<Root["render"]>[0]) { | ||
await act(() => root.render(element)); | ||
} | ||
|
||
beforeEach(async () => { | ||
scratch = document.createElement("div"); | ||
document.body.appendChild(scratch); | ||
root = await createRoot(scratch); | ||
getConsoleErrorSpy().resetHistory(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await act(() => root.unmount()); | ||
scratch.remove(); | ||
|
||
checkConsoleErrorLogs(); | ||
checkHangingAct(); | ||
}); | ||
|
||
it("should rerender components when signals they use change", async () => { | ||
const App = createComponent(` | ||
function App({ name }) { | ||
return <div>Hello {name.value}</div>; | ||
}`); | ||
|
||
const name = signal("John"); | ||
await render(<App name={name} />); | ||
expect(scratch.innerHTML).to.equal("<div>Hello John</div>"); | ||
|
||
await act(() => { | ||
name.value = "Jane"; | ||
}); | ||
expect(scratch.innerHTML).to.equal("<div>Hello Jane</div>"); | ||
}); | ||
}); |
Oops, something went wrong.