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

CPLAT-11722 Add uiForwardRef #612

Merged
merged 30 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a29da58
Merge branch 'function-components-wip' of github.com:Workiva/over_rea…
joebingham-wk Jul 29, 2020
53fd919
Initial implementation
joebingham-wk Jul 29, 2020
968de33
Add first go at examples
joebingham-wk Jul 29, 2020
c542033
Add extension method
joebingham-wk Jul 30, 2020
841d102
Add tests
joebingham-wk Aug 3, 2020
117cadb
Format tests
joebingham-wk Aug 3, 2020
68f4700
Merge in base
joebingham-wk Aug 3, 2020
96ba4ff
Clean up
joebingham-wk Aug 3, 2020
de94787
Rename FunctionComponentConfig to UiFactoryConfig
joebingham-wk Aug 3, 2020
ea10a41
Check for generated config
joebingham-wk Aug 3, 2020
d435a1f
Fix parsing
joebingham-wk Aug 4, 2020
d865b24
Fix up example
joebingham-wk Aug 5, 2020
378a58d
Fix CI
joebingham-wk Aug 5, 2020
7d288eb
Fix breakages from analyzer 0.39.16
joebingham-wk Aug 5, 2020
861fda7
Format
joebingham-wk Aug 5, 2020
fa6eb84
Address feedback
joebingham-wk Aug 6, 2020
1de88bb
Revert unintentional change
joebingham-wk Aug 6, 2020
d66db3f
Fix empty name issue
joebingham-wk Aug 6, 2020
0e3f5ef
Revert change to getFunctionName
joebingham-wk Aug 6, 2020
236f344
Fix doc comment
joebingham-wk Aug 6, 2020
ac8a42c
Improve example logging readability
joebingham-wk Aug 6, 2020
9e3f6d3
Improve deprecation doc comment
joebingham-wk Aug 6, 2020
e5001d2
Address parser feedback
joebingham-wk Aug 6, 2020
8189f1c
Fix up example
joebingham-wk Aug 7, 2020
49df84c
Add integration tests
joebingham-wk Aug 7, 2020
acf2582
Format
joebingham-wk Aug 7, 2020
af9e2e4
Update doc comment
joebingham-wk Aug 7, 2020
31b3414
Address more feedback :tada:
joebingham-wk Aug 7, 2020
b0f9877
Add displayName to doc comment
joebingham-wk Aug 7, 2020
d12bca5
Fix example title
joebingham-wk Aug 7, 2020
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
9 changes: 2 additions & 7 deletions lib/src/builder/parsing/ast_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@ extension InitializerHelperTopLevel on TopLevelVariableDeclaration {
/// The first variable in this list.
VariableDeclaration get firstVariable => variables.firstVariable;

/// Returns whether or not the config argument of a Function type is generated.
/// Returns whether or not there is a generated config being used.
bool get usesAGeneratedConfig {
return firstInitializer != null &&
anyDescendantIdentifiers(firstInitializer, (identifier) {
final uiFactoryDeclaration = identifier.thisOrAncestorOfType<VariableDeclaration>();

return firstInitializer != null &&
anyDescendantIdentifiers(firstInitializer, (identifier) {
return identifier.nameWithoutPrefix == '\$${uiFactoryDeclaration.name.name}Config';
});
return identifier.nameWithoutPrefix == '\$${firstVariable.name.name}Config';
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/src/component/ref_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,15 @@ UiFactory<TProps> Function(UiFactory<TProps>) forwardRef<TProps extends UiProps>
/// UiFactory<TProps> withLogging<TProps extends UiProps>(UiFactory<TProps> factoryToWrap) {
/// return uiForwardRef(
/// (props, ref) {
/// useEffect(() => 'the component rendered!');
/// useEffect(() => '${factoryToWrap().componentFactory.type} rendered!');
///
/// return (factoryToWrap()
/// ..addAll(props)
/// ..ref = ref
/// )(props.children);
/// },
/// factoryToBeWrapped.asForwardRefConfig(
/// displayName: 'withLogging(${getDisplayName(factoryToWrap)})',
/// displayName: 'WithLogging',
/// ),
/// );
/// }
Expand All @@ -326,7 +326,7 @@ UiFactory<TProps> Function(UiFactory<TProps>) forwardRef<TProps extends UiProps>
/// version wrapped in `uiForwardRef`:
///
/// ```dart
/// UiFactory<LogPropsProps> _Foo = _$_Foo; // ignore: undefined_identifier
/// UiFactory<FooProps> _Foo = _$_Foo; // ignore: undefined_identifier
/// mixin FooProps on UiProps {
/// // Private since we only use this to pass along the ref provided in
/// // uiForwardRef.
Expand All @@ -340,9 +340,9 @@ UiFactory<TProps> Function(UiFactory<TProps>) forwardRef<TProps extends UiProps>
/// return Dom.div()(
/// (Dom.input()
/// ..type = 'text'
/// ..ref = _inputRef
/// )();
/// )
/// ..ref = props._inputRef
/// )()
/// );
/// }
/// }
///
Expand Down Expand Up @@ -384,7 +384,7 @@ UiFactory<TProps> Function(UiFactory<TProps>) forwardRef<TProps extends UiProps>
/// }
///
/// class Foo2Props = UiProps with AnotherPropsMixin, FooProps;
/// final Foo2 = uiForwardRef<LogsPropsComponent2Props>((props, ref) {
/// final Foo2 = uiForwardRef<Foo2Props>((props, ref) {
/// print(props.anExampleAdditionalProp);
///
/// return (_Foo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,10 @@ main() {
functionComponentTestHelper(_Test, testId: '_testId');
});

group('with UiProps', () {
UiFactory<UiProps> TestUiProps = uiFunction(
(props) => (Dom.div()..addTestId('testId3'))('id: ${props.id}'),
UiFactoryConfig(),
);

test(
'renders a component from end to end, successfully reading props via typed getters',
() {
final jacket = mount((TestUiProps()..id = '1')());
final node = queryAllByTestId(jacket.mountNode, 'testId3').first;

expect(node, isA<DivElement>());
expect(node.text, 'id: 1');
});

group('initializes the factory variable with a function', () {
test('that returns a new props class implementation instance', () {
final instance = TestUiProps();
expect(instance, isA<UiProps>());
expect(instance, isA<Map>());
});

test(
'that returns a new props class implementation instance backed by an existing map',
() {
Map existingMap = {'key': 'test'};
final props = TestUiProps(existingMap);

expect(props.key, equals('test'));

props.key = 'modified';
expect(props.key, equals('modified'));
expect(existingMap['key'], equals('modified'));
});
});

test(
'generates prop getters/setters with the prop name as the key by default',
() {
expect(TestUiProps()..key = 'test', containsPair('key', 'test'));
expect(TestUiProps()..id = '2', containsPair('id', '2'));
});
});
uiPropsTest(uiFunction(
(props) => (Dom.div()..addTestId('testId3'))('id: ${props.id}'),
UiFactoryConfig(),
));

group('throws an error when', () {
test('config is null', () {
Expand All @@ -106,6 +66,94 @@ main() {
});
});
});

group('uiForwardRef', () {
group('with generated props config', () {
functionComponentTestHelper(BasicUiForwardRef);
});

group('with custom PropsFactory', () {
functionComponentTestHelper(CustomUiForwardRef, testId: 'testIdCustom');
});

group('with no left hand typing', () {
functionComponentTestHelper(NoLHSUiForwardRefTest, testId: 'testIdNoLHS');
});

group('with private prefix', () {
functionComponentTestHelper(_UiForwardRef, testId: '_testId');
});

uiPropsTest(uiForwardRef(
(props, ref) => (Dom.div()
..addTestId('testId3')
..ref = ref
)('id: ${props.id}'),
Dom.div.asForwardRefConfig(),
));

group('throws an error when', () {
test('config is null', () {
expect(
() => uiForwardRef<TestProps>(
(props, ref) => Dom.div()(),
null,
),
throwsArgumentError);
});

test('props factory is not provided when using custom props class', () {
expect(
() => uiForwardRef<TestProps>(
(props, ref) => Dom.div()(),
UiFactoryConfig(displayName: 'Foo'),
),
throwsArgumentError);
});
});
});
}

void uiPropsTest(UiFactory<UiProps> factory) {
group('with UiProps', () {
test(
'renders a component from end to end, successfully reading props via typed getters',
() {
final jacket = mount((factory()..id = '1')());
final node = queryAllByTestId(jacket.mountNode, 'testId3').first;

expect(node, isA<DivElement>());
expect(node.text, 'id: 1');
});

group('initializes the factory variable with a function', () {
test('that returns a new props class implementation instance', () {
final instance = factory();
expect(instance, isA<UiProps>());
expect(instance, isA<Map>());
});

test(
'that returns a new props class implementation instance backed by an existing map',
() {
Map existingMap = {'key': 'test'};
final props = factory(existingMap);

expect(props.key, equals('test'));

props.key = 'modified';
expect(props.key, equals('modified'));
expect(existingMap['key'], equals('modified'));
});
});

test(
'generates prop getters/setters with the prop name as the key by default',
() {
expect(factory()..key = 'test', containsPair('key', 'test'));
expect(factory()..id = '2', containsPair('id', '2'));
});
});
}

void functionComponentTestHelper(UiFactory<TestProps> factory,
Expand Down Expand Up @@ -186,6 +234,72 @@ void functionComponentTestHelper(UiFactory<TestProps> factory,
});
}

UiFactory<TestProps> BasicUiForwardRef = uiForwardRef(
(props, ref) {
return (Dom.div()
..ref = ref
..addTestId('testId')
..addProp('data-prop-string-prop', props.stringProp)
..addProp('data-prop-dynamic-prop', props.dynamicProp)
..addProp('data-prop-untyped-prop', props.untypedProp)
..addProp('data-prop-custom-key-prop', props.customKeyProp)
..addProp('data-prop-custom-namespace-prop', props.customNamespaceProp)
..addProp('data-prop-custom-key-and-namespace-prop',
props.customKeyAndNamespaceProp))('rendered content');
},
$TestConfig, // ignore: undefined_identifier
);

UiFactory<TestProps> CustomUiForwardRef = uiForwardRef(
(props, ref) {
return (Dom.div()
..ref = ref
..addTestId('testIdCustom')
..addProp('data-prop-string-prop', props.stringProp)
..addProp('data-prop-dynamic-prop', props.dynamicProp)
..addProp('data-prop-untyped-prop', props.untypedProp)
..addProp('data-prop-custom-key-prop', props.customKeyProp)
..addProp('data-prop-custom-namespace-prop', props.customNamespaceProp)
..addProp('data-prop-custom-key-and-namespace-prop',
props.customKeyAndNamespaceProp))('rendered content');
},
UiFactoryConfig(
propsFactory: PropsFactory.fromUiFactory(BasicUiForwardRef),
)
);

final NoLHSUiForwardRefTest = uiForwardRef<TestProps>(
(props, ref) {
return (Dom.div()
..ref = ref
..addTestId('testIdNoLHS')
..addProp('data-prop-string-prop', props.stringProp)
..addProp('data-prop-dynamic-prop', props.dynamicProp)
..addProp('data-prop-untyped-prop', props.untypedProp)
..addProp('data-prop-custom-key-prop', props.customKeyProp)
..addProp('data-prop-custom-namespace-prop', props.customNamespaceProp)
..addProp('data-prop-custom-key-and-namespace-prop',
props.customKeyAndNamespaceProp))('rendered content');
},
$NoLHSTestConfig, // ignore: undefined_identifier
);

UiFactory<TestProps> _UiForwardRef = uiForwardRef(
(props, ref) {
return (Dom.div()
..ref = ref
..addTestId('_testId')
..addProp('data-prop-string-prop', props.stringProp)
..addProp('data-prop-dynamic-prop', props.dynamicProp)
..addProp('data-prop-untyped-prop', props.untypedProp)
..addProp('data-prop-custom-key-prop', props.customKeyProp)
..addProp('data-prop-custom-namespace-prop', props.customNamespaceProp)
..addProp('data-prop-custom-key-and-namespace-prop',
props.customKeyAndNamespaceProp))('rendered content');
},
$_TestConfig, // ignore: undefined_identifier
);

UiFactory<TestProps> Test = uiFunction(
(props) {
return (Dom.div()
Expand Down
Loading