Skip to content

Commit c273973

Browse files
authored
feat(engine-server): Add parameter check to renderComponent (salesforce#1964)
1 parent ec70a09 commit c273973

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2020, salesforce.com, inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: MIT
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6+
*/
7+
8+
import { renderComponent, LightningElement } from '../index';
9+
10+
class Test extends LightningElement {}
11+
12+
describe('renderComponent', () => {
13+
it('returns the rendered tree as string', () => {
14+
expect(renderComponent('x-test', Test)).toBe(
15+
'<x-test><template shadow-root></template></x-test>'
16+
);
17+
});
18+
19+
it.each([undefined, null, 1, {}, () => {}])(
20+
'asserts the first parameter is a string (type: %p)',
21+
(value) => {
22+
expect(() => renderComponent(value as any, Test, {})).toThrow(
23+
`"renderComponent" expects a string as the first parameter but instead received ${value}.`
24+
);
25+
}
26+
);
27+
28+
it.each([undefined, null, 1, 'test', {}])(
29+
'asserts the seconds parameter is a function (type: %p)',
30+
(value) => {
31+
expect(() => renderComponent('x-test', value as any, {})).toThrow(
32+
`"renderComponent" expects a valid component constructor as the second parameter but instead received ${value}.`
33+
);
34+
}
35+
);
36+
37+
it.each([null, 1, 'test', () => {}])(
38+
'asserts the third parameter is an object (type: %p)',
39+
(value) => {
40+
expect(() => renderComponent('x-test', Test, value as any)).toThrow(
41+
`"renderComponent" expects an object as the third parameter but instead received ${value}.`
42+
);
43+
}
44+
);
45+
});

packages/@lwc/engine-server/src/apis/render-component.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,34 @@ import {
1111
setElementProto,
1212
LightningElement,
1313
} from '@lwc/engine-core';
14+
import { isString, isFunction, isObject, isNull } from '@lwc/shared';
1415

1516
import { renderer } from '../renderer';
1617
import { serializeElement } from '../serializer';
1718

1819
export function renderComponent(
1920
tagName: string,
2021
Ctor: typeof LightningElement,
21-
props: { [name: string]: any }
22+
props: { [name: string]: any } = {}
2223
): string {
24+
if (!isString(tagName)) {
25+
throw new TypeError(
26+
`"renderComponent" expects a string as the first parameter but instead received ${tagName}.`
27+
);
28+
}
29+
30+
if (!isFunction(Ctor)) {
31+
throw new TypeError(
32+
`"renderComponent" expects a valid component constructor as the second parameter but instead received ${Ctor}.`
33+
);
34+
}
35+
36+
if (!isObject(props) || isNull(props)) {
37+
throw new TypeError(
38+
`"renderComponent" expects an object as the third parameter but instead received ${props}.`
39+
);
40+
}
41+
2342
const element = renderer.createElement(tagName);
2443

2544
const def = getComponentInternalDef(Ctor);

0 commit comments

Comments
 (0)