Skip to content
Draft
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
24 changes: 24 additions & 0 deletions src/adapter/11/bindings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from "vitest";
import {
getDisplayName,
getPropsVNodeDisplayName,
TYPE_FUNCTION,
} from "./bindings";

describe("Preact 11 bindings", () => {
it("should detect context providers without a private back-reference", () => {
const context: any = Object.assign(() => null, {
displayName: "Foobar",
__: "default value",
});
context.Provider = context;

const config = { Fragment: () => null };
expect(
getPropsVNodeDisplayName({ type: context, props: {} } as any, config),
).to.equal("Foobar.Provider");
expect(
getDisplayName({ type: context, flags: TYPE_FUNCTION } as any, config),
).to.equal("Foobar.Provider");
});
});
8 changes: 6 additions & 2 deletions src/adapter/11/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export function getSuspenseStateKey(c: Component) {
return null;
}

function getContextProvider(type: any) {
return type.Provider === type ? type : type._contextRef || type.__;
}

// Mangle accessors

// When serializing props we're dealing with vnodes instead of
Expand All @@ -112,7 +116,7 @@ export function getPropsVNodeDisplayName(vnode: VNode, config: RendererConfig) {
}

// Provider
const ctx = (type as any)._contextRef || (type as any).__;
const ctx = getContextProvider(type);
if (ctx && ctx.displayName) {
return `${ctx.displayName}.Provider`;
}
Expand Down Expand Up @@ -148,7 +152,7 @@ export function getDisplayName(internal: Internal, config: RendererConfig) {
}

// Provider
const ctx = (type as any)._contextRef || (type as any).__;
const ctx = getContextProvider(type);
if (ctx && ctx.displayName) {
return `${ctx.displayName}.Provider`;
}
Expand Down
Loading