diff --git a/.changeset/friendly-portals-repeat.md b/.changeset/friendly-portals-repeat.md new file mode 100644 index 00000000..911839f2 --- /dev/null +++ b/.changeset/friendly-portals-repeat.md @@ -0,0 +1,5 @@ +--- +'@ankhorage/runtime': patch +--- + +Preserve effective renderer configuration for manifest children rendered outside their original React provider ancestry. diff --git a/src/RuntimeRenderer.tsx b/src/RuntimeRenderer.tsx index 5d2f29da..db25dfb8 100644 --- a/src/RuntimeRenderer.tsx +++ b/src/RuntimeRenderer.tsx @@ -382,26 +382,35 @@ export function RuntimeRenderer(props: RuntimeRendererProps) { }); return ( - + replace + value={effectiveConfig} + > + + ); }), ) : shouldRenderRepeatEmptyState ? node.repeat.empty?.map((child) => ( - + + + )) : undefined : node.children?.map((child) => ( - + + + )); const propsWithEvents = wrapRuntimeEventProps({ diff --git a/src/RuntimeRendererConfig.tsx b/src/RuntimeRendererConfig.tsx index 669d09fe..db67b434 100644 --- a/src/RuntimeRendererConfig.tsx +++ b/src/RuntimeRendererConfig.tsx @@ -157,10 +157,11 @@ export function mergeRuntimeRendererConfig( export function RuntimeRendererConfigProvider(props: { value: RuntimeRendererConfig; children: React.ReactNode; + replace?: boolean; }) { - const { value, children } = props; + const { value, children, replace = false } = props; const inheritedConfig = use(RuntimeRendererConfigContext); - const mergedConfig = mergeRuntimeRendererConfig(value, inheritedConfig); + const mergedConfig = replace ? value : mergeRuntimeRendererConfig(value, inheritedConfig); return ( {children} diff --git a/src/runtimeRendererPortalConfig.test.tsx b/src/runtimeRendererPortalConfig.test.tsx new file mode 100644 index 00000000..6501a501 --- /dev/null +++ b/src/runtimeRendererPortalConfig.test.tsx @@ -0,0 +1,205 @@ +import type { ComponentDataBindingRegistry, StateAdapter, UiNode } from '@ankhorage/contracts'; +import { expect, it, mock } from 'bun:test'; +import React from 'react'; +import { act, create } from 'react-test-renderer'; + +import type { ComponentRegistry } from './registry.js'; +import { + RuntimeRendererConfigProvider, + useRuntimeRendererConfig, +} from './RuntimeRendererConfig.js'; +import { createRuntimeMemoryStateAdapter } from './runtimeStateAdapter.js'; + +( + globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean } +).IS_REACT_ACT_ENVIRONMENT = true; + +void mock.module('react-native', () => ({ + StyleSheet: { create: (styles: TStyles): TStyles => styles }, + Text: 'text', + View: 'view', +})); + +const { RuntimeRenderer } = await import('./RuntimeRenderer.js'); + +let capturedPortalChildren: React.ReactElement | undefined; +let expectedStateAdapter: StateAdapter | undefined; +let expectedExecutor: unknown; + +const Portal = ({ children }: { readonly children?: React.ReactNode }): React.ReactElement => { + capturedPortalChildren = <>{children}; + return ; +}; + +const Rows = ({ children }: { readonly children?: React.ReactNode }): React.ReactElement => ( +
{children}
+); + +const RowLabel = ({ value }: { readonly value?: unknown }): React.ReactElement => ( + +); + +const ConfigProbe = (): React.ReactElement => { + const config = useRuntimeRendererConfig(); + const context = config.bindingContext; + + return ( + + ); +}; + +const Action = ({ onPress }: { readonly onPress?: () => void }): React.ReactElement => ( + +); + +const registry: ComponentRegistry = { Action, ConfigProbe, Portal, RowLabel, Rows }; + +const node = { + children: [ + { + children: [ + { id: 'row-label', type: 'RowLabel' }, + { id: 'config-probe', type: 'ConfigProbe' }, + { id: 'action', type: 'Action' }, + ], + id: 'rows', + repeat: { itemAlias: 'row', source: { capability: 'context.rows' } }, + type: 'Rows', + }, + ], + id: 'portal', + type: 'Portal', +} satisfies UiNode; + +const dataBindings = { + action: { + componentId: 'action', + events: { press: [{ target: { capability: 'api.rows.select' } }] }, + }, + 'row-label': { + componentId: 'row-label', + props: { value: { value: { capability: 'context.row', path: 'label' } } }, + }, +} satisfies ComponentDataBindingRegistry; + +it('preserves effective Runtime configuration for repeated children rendered outside provider ancestry', async () => { + const stateAdapter = createRuntimeMemoryStateAdapter(); + const executeCapability = mock(() => Promise.resolve({ data: null, ok: true as const })); + expectedStateAdapter = stateAdapter; + expectedExecutor = executeCapability; + const mountedSourceRenderer = mountSourceRenderer({ + executeCapability, + rows: [{ label: 'First repository' }], + stateAdapter, + }); + const mountedPortalRenderer = mountCapturedPortalChildren(); + + expectPortalConfiguration(mountedPortalRenderer, 'First repository'); + + await act(async () => { + const buttonProps = mountedPortalRenderer.root.findByType('button').props as { + readonly onClick?: unknown; + }; + const { onClick } = buttonProps; + if (!isVoidCallback(onClick)) throw new Error('Expected the action button to have onClick.'); + onClick(); + await Promise.resolve(); + }); + expect(executeCapability).toHaveBeenCalledTimes(1); + + updateSourceRenderer(mountedSourceRenderer, { + executeCapability, + rows: [{ label: 'Updated repository' }], + stateAdapter, + }); + const updatedPortalChildren = getCapturedPortalChildren(); + act(() => { + mountedPortalRenderer.update(updatedPortalChildren); + }); + + expectPortalConfiguration(mountedPortalRenderer, 'Updated repository'); +}); + +function mountSourceRenderer(args: { + readonly executeCapability: ReturnType; + readonly rows: readonly { readonly label: string }[]; + readonly stateAdapter: StateAdapter; +}): ReturnType { + let renderer: ReturnType | undefined; + act(() => { + renderer = create(renderSource(args)); + }); + if (renderer === undefined) throw new Error('Expected the source renderer to mount.'); + return renderer; +} + +function updateSourceRenderer( + renderer: ReturnType, + args: { + readonly executeCapability: ReturnType; + readonly rows: readonly { readonly label: string }[]; + readonly stateAdapter: StateAdapter; + }, +): void { + act(() => { + renderer.update(renderSource(args)); + }); +} + +function renderSource(args: { + readonly executeCapability: ReturnType; + readonly rows: readonly { readonly label: string }[]; + readonly stateAdapter: StateAdapter; +}): React.ReactElement { + return ( + + + + ); +} + +function mountCapturedPortalChildren(): ReturnType { + let renderer: ReturnType | undefined; + act(() => { + renderer = create(getCapturedPortalChildren()); + }); + if (renderer === undefined) throw new Error('Expected detached portal children to render.'); + return renderer; +} + +function getCapturedPortalChildren(): React.ReactElement { + if (capturedPortalChildren === undefined) + throw new Error('Expected the portal to capture children.'); + return capturedPortalChildren; +} + +function isVoidCallback(value: unknown): value is () => void { + return typeof value === 'function'; +} + +function expectPortalConfiguration(renderer: ReturnType, row: string): void { + expect(renderer.root.findByProps({ 'data-row': row })).toBeDefined(); + expect(renderer.root.findByProps({ 'data-inherited': 'outer' })).toBeDefined(); + expect(renderer.root.findByProps({ 'data-local': 'root' })).toBeDefined(); + expect(renderer.root.findByProps({ 'data-row-alias': row })).toBeDefined(); + expect(renderer.root.findByProps({ 'data-state-adapter': 'true' })).toBeDefined(); + expect(renderer.root.findByProps({ 'data-executor': 'true' })).toBeDefined(); +}