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

AF-2922: add defaultProps getter to UiProp #196

Merged
merged 3 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions lib/src/component_declaration/component_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,11 @@ abstract class UiProps extends MapBase
}

Function get componentFactory;

Map get defaultProps => componentFactory is ReactDartComponentFactoryProxy
// ignore: avoid_as
? (componentFactory as ReactDartComponentFactoryProxy).defaultProps
: const {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a test case for this case?
e.g.,

expect(Dom.div().componentDefaultProps, equals({}));

}

/// A class that declares the `_map` getter shared by [PropsMapViewMixin]/[StateMapViewMixin] and [MapViewMixin].
Expand Down
3 changes: 3 additions & 0 deletions lib/src/util/react_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class UiPropsMapView extends MapView with ReactPropsMixin, UbiquitousDomPropsMix
@override
String get testId => getTestId();

@override
Map get defaultProps => throw new UnimplementedError('@PropsMixin instances do not implement defaultProps');

@override
ReactElement build([dynamic children]) =>
throw new UnimplementedError('@PropsMixin instances do not implement build');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ library over_react.component_declaration.transformer_integration_tests.component
import 'package:over_react/over_react.dart';
import 'package:test/test.dart';

import './required_prop_integration_test.dart';
import './required_prop_integration_test.dart' as r;
import '../../../test_util/test_util.dart';

main() {
Expand Down Expand Up @@ -97,6 +97,14 @@ main() {
expect(ComponentTest()..customKeyAndNamespaceProp = 'test',
containsPair('custom namespace~~custom key!', 'test'));
});

test('default props', () {
expect(ComponentTest().defaultProps, equals({'id':'testId'}));
});

test('empty map when no default props set', () {
expect(r.ComponentTest().defaultProps, equals({}));
});
});

test('omits props declared in the @Props() class when forwarding by default', () {
Expand All @@ -113,10 +121,10 @@ main() {
var shallowProps = getProps(shallowInstance);
Iterable<String> shallowPropKeys = shallowProps.keys;

expect(shallowPropKeys.where((String key) => !key.startsWith('data-prop-')), unorderedEquals(['extraneous', 'children']));
expect(shallowPropKeys.where((String key) => !key.startsWith('data-prop-')), unorderedEquals(['id', 'extraneous', 'children']));
});

requiredPropsIntegrationTest();
r.requiredPropsIntegrationTest();
});
}

Expand All @@ -142,6 +150,9 @@ class ComponentTestProps extends UiProps {

@Component()
class ComponentTestComponent extends UiComponent<ComponentTestProps> {
@override
Map getDefaultProps() => newProps()..id = 'testId';

@override
render() => (Dom.div()
..addProps(copyUnconsumedProps())
Expand Down
4 changes: 4 additions & 0 deletions test/over_react/util/react_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ main() {
expect(() => mapView.testId, throwsUnimplementedError);
});

test('`defaultProps`', () {
expect(() => mapView.defaultProps, throwsUnimplementedError);
});

test('`getTestId()`', () {
expect(() => mapView.getTestId(), throwsUnimplementedError);
});
Expand Down