/** @jsxRuntime classic */ /** @jsx createElement */ import { render } from '@testing-library/react'; import { assertSlots, getSlotsNext, resolveShorthand, slot } from '@fluentui/react-utilities'; import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities'; import { createElement } from './createElement'; describe('createElement with getSlotsNext', () => { describe('general behavior tests', () => { it('handles a string', () => { const result = render(
Hello world
); expect(result.container.firstChild).toMatchInlineSnapshot(`
Hello world
`); }); it('handles an array', () => { const result = render(
{Array.from({ length: 3 }, (_, i) => (
{i}
))}
, ); expect(result.container.firstChild).toMatchInlineSnapshot(`
0
1
2
`); }); it('handles an array of children', () => { const result = render(
1
2
3
, ); expect(result.container.firstChild).toMatchInlineSnapshot(`
1
2
3
`); }); }); describe('custom behavior tests', () => { it('keeps children from "defaultProps" in a render callback', () => { type TestComponentSlots = { slot: Slot<'div'> }; type TestComponentState = ComponentState; type TestComponentProps = ComponentProps>; const TestComponent = (props: TestComponentProps) => { const state: TestComponentState = { components: { slot: 'div' }, // eslint-disable-next-line @typescript-eslint/no-deprecated slot: resolveShorthand(props.slot, { defaultProps: { children: 'Default Children', id: 'slot' }, }), }; // eslint-disable-next-line @typescript-eslint/no-deprecated const { slots, slotProps } = getSlotsNext(state); return ; }; const children = jest.fn().mockImplementation((Component, props) => (
)); const result = render(); expect(children).toHaveBeenCalledTimes(1); expect(children).toHaveBeenCalledWith('div', { children: 'Default Children', id: 'slot' }); expect(result.container.firstChild).toMatchInlineSnapshot(`
Default Children
`); }); it('keeps children from a render template in a render callback', () => { type TestComponentSlots = { outer: Slot<'div'>; inner: Slot<'div'> }; type TestComponentState = ComponentState; type TestComponentProps = ComponentProps>; const TestComponent = (props: TestComponentProps) => { const state: TestComponentState = { components: { inner: 'div', outer: 'div' }, // eslint-disable-next-line @typescript-eslint/no-deprecated inner: resolveShorthand(props.inner, { defaultProps: { id: 'inner' } }), // eslint-disable-next-line @typescript-eslint/no-deprecated outer: resolveShorthand(props.outer, { defaultProps: { id: 'outer' } }), }; // eslint-disable-next-line @typescript-eslint/no-deprecated const { slots, slotProps } = getSlotsNext(state); return ( ); }; const children = jest.fn().mockImplementation((Component, props) => (
)); const result = render(); expect(children).toHaveBeenCalledTimes(1); expect(children.mock.calls[0][0]).toBe('div'); expect(children.mock.calls[0][1].id).toBe('outer'); expect(children.mock.calls[0][1].children).toMatchInlineSnapshot(`
Inner children
`); expect(result.container.firstChild).toMatchInlineSnapshot(`
Inner children
`); }); }); }); describe('createElement with assertSlots', () => { describe('general behavior tests', () => { it('handles a string', () => { const result = render(
Hello world
); expect(result.container.firstChild).toMatchInlineSnapshot(`
Hello world
`); }); it('handles an array', () => { const result = render(
{Array.from({ length: 3 }, (_, i) => (
{i}
))}
, ); expect(result.container.firstChild).toMatchInlineSnapshot(`
0
1
2
`); }); it('handles an array of children', () => { const result = render(
1
2
, ); expect(result.container.firstChild).toMatchInlineSnapshot(`
1
2
`); }); }); describe('custom behavior tests', () => { it('keeps children from "defaultProps" in a render callback', () => { type TestComponentSlots = { someSlot: NonNullable>; }; type TestComponentProps = ComponentProps>; type TestComponentState = ComponentState; const TestComponent = (props: TestComponentProps) => { const state: TestComponentState = { components: { someSlot: 'div' }, someSlot: slot.always(props.someSlot, { elementType: 'div', defaultProps: { children: 'Default Children', id: 'slot' }, }), }; assertSlots(state); return ; }; const children = jest.fn().mockImplementation((Component, props) => (
)); const result = render(); expect(children).toHaveBeenCalledTimes(1); expect(children).toHaveBeenCalledWith('div', { children: 'Default Children', id: 'slot' }); expect(result.container.firstChild).toMatchInlineSnapshot(`
Default Children
`); }); it('keeps children from a render template in a render callback', () => { type TestComponentSlots = { outer: NonNullable>; inner: NonNullable> }; type TestComponentState = ComponentState; type TestComponentProps = ComponentProps>; const TestComponent = (props: TestComponentProps) => { const state: TestComponentState = { components: { outer: 'div', inner: 'div' }, inner: slot.always(props.inner, { defaultProps: { id: 'inner' }, elementType: 'div' }), outer: slot.always(props.outer, { defaultProps: { id: 'outer' }, elementType: 'div' }), }; assertSlots(state); return ( ); }; const children = jest.fn().mockImplementation((Component, props) => (
)); const result = render(); expect(children).toHaveBeenCalledTimes(1); expect(children.mock.calls[0][0]).toBe('div'); expect(children.mock.calls[0][1].id).toBe('outer'); expect(children.mock.calls[0][1].children).toMatchInlineSnapshot(`
Inner children
`); expect(result.container.firstChild).toMatchInlineSnapshot(`
Inner children
`); }); it("should support 'as' property to opt-out of base element type", () => { type TestComponentSlots = { slot: NonNullable> }; type TestComponentState = ComponentState; type TestComponentProps = ComponentProps>; const TestComponent = (props: TestComponentProps) => { const state: TestComponentState = { components: { slot: 'div' }, slot: slot.always(props.slot, { elementType: 'div' }), }; assertSlots(state); return ; }; const result = render(); expect(result.container.firstChild).toMatchInlineSnapshot(``); }); }); });