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

UIP-2529 Add validateProps method to UiComponent #88

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 23 additions & 2 deletions lib/src/component_declaration/component_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component {
);
}

/// Throws a [PropError] if [appliedProps] are invalid.
///
/// This is called automatically with during [componentWillReceiveProps] and [componentWillMount],
Copy link
Contributor

Choose a reason for hiding this comment

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

This is called automatically with during

Sorry, my typo here.

What about:

This is called automatically with the latest props available during

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:stare:

/// and can also be called manually for custom validation.
///
/// Override with a custom implementation to easily add validation (and don't forget to call super!)
///
/// @mustCallSuper
/// void validateProps(Map appliedProps) {
/// super.validateProps(appliedProps);
///
/// var tProps = typedPropsFactory(appliedProps);
/// if (tProps.items.length.isOdd) {
/// throw new PropError.value(tProps.items, 'items', 'must have an even number of items, because reasons');
/// }
/// }
@mustCallSuper
void validateProps(Map appliedProps) {
validateRequiredProps(appliedProps);
}

void validateRequiredProps(Map appliedProps) {
consumedProps?.forEach((ConsumedProps consumedProps) {
consumedProps.props.forEach((PropDescriptor prop) {
Expand All @@ -147,13 +168,13 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component {
@override
@mustCallSuper
void componentWillReceiveProps(Map newProps) {
validateRequiredProps(newProps);
validateProps(newProps);
}

@override
@mustCallSuper
void componentWillMount() {
validateRequiredProps(props);
validateProps(props);
}


Expand Down
43 changes: 43 additions & 0 deletions test/over_react/component_declaration/component_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,42 @@ main() {
expect(classes.toClassName(), equals('class-1'));
expect(classes.toClassNameBlacklist(), equals('blacklist-1'));
});

group('calls validateProps in', () {
test('componentWillMount', () {
var calls = [];
var appliedProps;
var initialProps = {
'onValidateProps': (Map propsMap) {
appliedProps = propsMap;
calls.add('onValidateProps');
},
};
component.props = initialProps;

component.componentWillMount();

expect(calls, ['onValidateProps']);
expect(appliedProps, initialProps);
});

test('componentWillReceiveProps', () {
var calls = [];
var appliedProps;
var newProps = {'key': 'value'};
component.props = {
'onValidateProps': (Map propsMap) {
appliedProps = propsMap;
calls.add('onValidateProps');
},
};

component.componentWillReceiveProps(newProps);

expect(calls, ['onValidateProps']);
expect(appliedProps, newProps);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

These should also test that the correct props maps are passed into them

});
});

group('UiStatefulComponent', () {
Expand Down Expand Up @@ -799,6 +835,13 @@ class TestComponentComponent extends UiComponent<TestComponentProps> {

@override
TestComponentProps typedPropsFactory(Map propsMap) => new TestComponentProps(propsMap);

@override
void validateProps(Map appliedProps) {
super.validateProps(appliedProps);

if (props['onValidateProps'] != null) props['onValidateProps'](appliedProps);
}
}


Expand Down