Skip to content

Commit b813748

Browse files
authored
Merge branch 'master' into users/srmukher/RTLPackage
2 parents 866f620 + f9f2b69 commit b813748

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "chore: tests for slot.resolveShorthand behavior",
4+
"packageName": "@fluentui/react-utilities",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/react-components/react-utilities/src/compose/slot.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,44 @@ describe('slot', () => {
8484
[SLOT_RENDER_FUNCTION_SYMBOL]: expect.any(Function),
8585
});
8686
});
87+
describe('.resolveShorthand', () => {
88+
it('resolves a string', () => {
89+
expect(slot.resolveShorthand('hello')).toEqual({ children: 'hello' });
90+
});
91+
92+
it('resolves a JSX element', () => {
93+
const jsx = <div>hello</div>;
94+
expect(slot.resolveShorthand(jsx)).toEqual({ children: jsx });
95+
});
96+
97+
it('resolves a number', () => {
98+
expect(slot.resolveShorthand(42)).toEqual({ children: 42 });
99+
});
100+
it('resolves an array', () => {
101+
expect(slot.resolveShorthand([])).toEqual({ children: [] });
102+
});
103+
104+
it('resolves an object as the same object', () => {
105+
const slotA = {};
106+
const resolvedProps = slot.resolveShorthand(slotA);
107+
expect(resolvedProps).toEqual({});
108+
expect(resolvedProps).toBe(slotA);
109+
});
110+
111+
it('resolves "null" without creating a child element', () => {
112+
expect(slot.resolveShorthand(null)).toEqual(null);
113+
});
114+
115+
it('resolves undefined without creating a child element', () => {
116+
expect(slot.resolveShorthand(undefined)).toEqual(undefined);
117+
});
118+
119+
it('should console an error when a function is passed as value', () => {
120+
console.error = jest.fn();
121+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
122+
const fn: any = () => null;
123+
expect(slot.resolveShorthand(fn)).toBe(fn);
124+
expect(console.error).toHaveBeenCalled();
125+
});
126+
});
87127
});

packages/react-components/react-utilities/src/compose/slot.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ export function resolveShorthand<Props extends UnknownSlotProps | null | undefin
9191
) {
9292
return { children: value } as Props;
9393
}
94+
if (value && typeof value !== 'object' && process.env.NODE_ENV !== 'production') {
95+
// TODO: would be nice to have a link to slot documentation in this error message
96+
// eslint-disable-next-line no-console
97+
console.error(
98+
[
99+
`[slot.resolveShorthand]: A slot got invalid value "${value}" (${typeof value}).`,
100+
'A valid value is a slot shorthand or slot properties object.',
101+
'Slot shorthands can be strings, numbers, arrays or JSX elements',
102+
].join('\n'),
103+
);
104+
}
94105

95106
return value;
96107
}

0 commit comments

Comments
 (0)