Skip to content

Commit 49a8dca

Browse files
nickshiroryansolid
andauthored
test(web): pin Dynamic prop-memo hydration parity (#3797) (#3826)
Co-authored-by: Ryan Carniato <[email protected]>
1 parent b987c41 commit 49a8dca

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "dynamic-conditional-prop-before-child",
3+
"shell": "<button _hk=20 type=\"button\" aria-label=\"idle\" title=\"idle\" data-state=\"idle\"><span _hk=24>idle</span></button>",
4+
"rest": ""
5+
}

‎packages/web/test/harness/scenarios.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
onSettled,
2929
createProjection,
3030
createStore,
31+
merge,
32+
omit,
3133
Show,
3234
Switch,
3335
Match,
@@ -38,6 +40,7 @@ import {
3840
} from "solid-js";
3941
import {
4042
Portal,
43+
Dynamic,
4144
dynamic,
4245
httpStatus,
4346
httpHeader,
@@ -2213,6 +2216,37 @@ function DynamicChildrenKeyBeforePropMemo() {
22132216
return <Comp children={<i>icon</i>} aria-label={n() ? `a ${n()}` : `b ${n()}`} />;
22142217
}
22152218

2219+
// #3797: a conditional prop on <Dynamic> must not shift the child's
2220+
// hydration key between server rendering and client hydration.
2221+
let setDynamicConditionalLabel!: (v: boolean) => void;
2222+
function ForwardDynamic(props: Record<string, any>) {
2223+
const rest = omit(props, "as");
2224+
return <Dynamic component={props.as} {...rest} />;
2225+
}
2226+
function ConditionalButton(props: Record<string, any>) {
2227+
const merged = merge({ type: "button" }, props);
2228+
return <ForwardDynamic as="button" {...merged} />;
2229+
}
2230+
function ConditionalTrigger(props: Record<string, any>) {
2231+
const rest = omit(props, "onClick");
2232+
return <ConditionalButton {...rest} />;
2233+
}
2234+
function DynamicConditionalPropBeforeChild() {
2235+
const [active, set] = createSignal(false);
2236+
setDynamicConditionalLabel = set;
2237+
const activeLabel = () => "active";
2238+
const idleLabel = () => "idle";
2239+
return (
2240+
<ConditionalTrigger
2241+
aria-label={active() && active() ? activeLabel() : idleLabel()}
2242+
title={active() && active() ? activeLabel() : idleLabel()}
2243+
data-state={active() && active() ? activeLabel() : idleLabel()}
2244+
>
2245+
<span>{active() ? "active" : "idle"}</span>
2246+
</ConditionalTrigger>
2247+
);
2248+
}
2249+
22162250
export const scenarios: Scenario[] = [
22172251
{
22182252
name: "polymorphic-chain",
@@ -3068,5 +3102,14 @@ export const scenarios: Scenario[] = [
30683102
update: () => setDynamicLabelCountFirst(1),
30693103
stableSelector: "button, i",
30703104
adoptAll: true
3105+
},
3106+
{
3107+
name: "dynamic-conditional-prop-before-child",
3108+
App: DynamicConditionalPropBeforeChild,
3109+
expectedText: "idle",
3110+
update: () => setDynamicConditionalLabel(true),
3111+
expectedTextAfterUpdate: "active",
3112+
stableSelector: "button, span",
3113+
adoptAll: true
30713114
}
30723115
];

0 commit comments

Comments
 (0)