Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React Native] Improve errors for invalid ViewConfig getter functions #16879

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -78,6 +78,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
'Tried to register two views with the same name %s',
name,
);
invariant(
typeof callback === 'function',
'View config getter callback for component `%s` must be a function (received `%s`)',
name,
callback === null ? 'null' : typeof callback,
);
viewConfigCallbacks.set(name, callback);
return name;
};
Expand All @@ -94,8 +100,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
if (typeof callback !== 'function') {
invariant(
false,
'View config not found for name %s.%s',
'View config getter callback for component `%s` must be a function (received `%s`).%s',
name,
callback === null ? 'null' : typeof callback,
typeof name[0] === 'string' && /[a-z]/.test(name[0])
? ' Make sure to start component names with a capital letter.'
: '',
Expand Down
Expand Up @@ -32,6 +32,18 @@ describe('ReactNativeError', () => {
.computeComponentStackForErrorReporting;
});

it('should throw error if null component registration getter is used', () => {
expect(() => {
try {
createReactNativeComponentClass('View', null);
} catch (e) {
throw new Error(e.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

}
}).toThrow(
'Invariant Violation: View config getter callback for component `View` must be a function (received `null`)',
);
});

it('should be able to extract a component stack from a native view', () => {
const View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
Expand Down
Expand Up @@ -75,6 +75,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
'Tried to register two views with the same name %s',
name,
);
invariant(
typeof callback === 'function',
'View config getter callback for component `%s` must be a function (received `%s`)',
name,
callback === null ? 'null' : typeof callback,
);
viewConfigCallbacks.set(name, callback);
return name;
};
Expand All @@ -91,8 +97,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
if (typeof callback !== 'function') {
invariant(
false,
'View config not found for name %s.%s',
'View config getter callback for component `%s` must be a function (received `%s`).%s',
name,
callback === null ? 'null' : typeof callback,
typeof name[0] === 'string' && /[a-z]/.test(name[0])
? ' Make sure to start component names with a capital letter.'
: '',
Expand Down