Skip to content
Merged
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
46 changes: 46 additions & 0 deletions runtime/slots/hooks.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { renderHook } from '@testing-library/react';
import { ReactNode } from 'react';
import { getSiteConfig, setSiteConfig } from '../config';
import { useSlotOperations } from './hooks';
import SlotContext from './SlotContext';
import { WidgetOperationTypes } from './widget';

const enclosingSlotId = 'enclosing-slot.ui';
const otherSlotId = 'other-slot.ui';

function wrapper({ children }: { children: ReactNode }) {
return (
<SlotContext.Provider value={{ id: enclosingSlotId, idAliases: ['enclosing-alias.ui'], children: <div>Default content</div> }}>
{children}
</SlotContext.Provider>
);
}

describe('useSlotOperations', () => {
beforeEach(() => {
setSiteConfig({
...getSiteConfig(),
apps: [{
appId: 'test-app',
slots: [
{ slotId: 'enclosing-alias.ui', op: WidgetOperationTypes.APPEND, id: 'alias-widget', element: <div>Alias</div> },
{ slotId: otherSlotId, op: WidgetOperationTypes.APPEND, id: 'other-widget', element: <div>Other</div> },
],
}],
});
});

afterEach(() => {
setSiteConfig({ ...getSiteConfig(), apps: [] });
});

it('includes the default content and aliases of the enclosing slot when asked for it', () => {
const { result } = renderHook(() => useSlotOperations(enclosingSlotId), { wrapper });
expect(result.current.map(operation => 'id' in operation && operation.id)).toEqual(['defaultContent', 'alias-widget']);
});

it('ignores the default content and aliases of the enclosing slot when asked for another slot', () => {
const { result } = renderHook(() => useSlotOperations(otherSlotId), { wrapper });
expect(result.current.map(operation => 'id' in operation && operation.id)).toEqual(['other-widget']);
});
});
8 changes: 7 additions & 1 deletion runtime/slots/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { createWidgetAppendOperation } from './widget';
/**
* Resolves the operations registered for a given slot id (plus any aliases),
* prepended with a default-content operation built from the slot's children.
*
* Children and aliases come from the enclosing slot, so they only apply when
* that slot is the one being asked for. Read from outside, a slot has neither.
*/
export function useSlotOperations(id: string) {
const { children, idAliases } = useSlotContext();
const slotContext = useSlotContext();
const isEnclosingSlot = slotContext.id === id;
const children = isEnclosingSlot ? slotContext.children : undefined;
const idAliases = isEnclosingSlot ? slotContext.idAliases : undefined;

return useMemo(() => {
const defaultOperation = createWidgetAppendOperation('defaultContent', id, children);
Expand Down