Skip to content

Commit

Permalink
Add tests for forwardUnconsumedPropsV2
Browse files Browse the repository at this point in the history
  • Loading branch information
joebingham-wk committed Oct 15, 2020
1 parent cf2cdd3 commit 728f513
Showing 1 changed file with 150 additions and 94 deletions.
244 changes: 150 additions & 94 deletions test/over_react/util/map_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ import 'package:over_react/over_react.dart';
import 'package:test/test.dart';
import 'package:over_react/src/component_declaration/component_base.dart' as component_base;

typedef ForwardUnconsumedPropsFunction = void Function(Map props, {
bool omitReactProps,
bool onlyCopyDomProps,
Iterable keysToOmit,
Iterable<Iterable>
keySetsToOmit,
Map propsToUpdate
});

/// Main entrypoint for map_util testing
main() {
group('map_util part', () {
Expand Down Expand Up @@ -124,121 +133,154 @@ main() {
});
});

group('forwardUnconsumedProps() modifies a passed in props reference', () {
group('with React props', () {
test('omitted out by default', () {
Map startingProps = {
'key': 'my key',
'ref': 'my ref',
'other prop': 'my other prop'
};
void commonPropsForwardingUtilTests(ForwardUnconsumedPropsFunction functionToTest) {
group('(common prop forwarding tests) modifies a passed in props reference', () {
group('with React props', () {
test('omitted out by default', () {
Map startingProps = {
'key': 'my key',
'ref': 'my ref',
'other prop': 'my other prop'
};

Map actual = {};
Map actual = {};

forwardUnconsumedProps(startingProps, propsToUpdate: actual);
functionToTest(startingProps, propsToUpdate: actual);

var expected = {'other prop': 'my other prop'};
var expected = {'other prop': 'my other prop'};

expect(actual, equals(expected));
expect(actual, equals(expected));
});

test('not omitted when specified', () {
var actual = {};

functionToTest({
'key': 'my key',
'ref': 'my ref',
'other prop': 'my other prop'
}, omitReactProps: false, propsToUpdate: actual);

var expected = {
'key': 'my key',
'ref': 'my ref',
'other prop': 'my other prop'
};

expect(actual, equals(expected));
});
});

test('not omitted when specified', () {
test('with the specified keys omitted', () {
var actual = {};

forwardUnconsumedProps({
'key': 'my key',
'ref': 'my ref',
'other prop': 'my other prop'
}, omitReactProps: false, propsToUpdate: actual);
functionToTest({
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
}, keysToOmit: [
'prop 2',
'prop 4'
], propsToUpdate: actual);

var expected = {
'key': 'my key',
'ref': 'my ref',
'other prop': 'my other prop'
'prop 1': 'my prop #1',
'prop 3': 'my prop #3',
};

expect(actual, equals(expected));
});
});

test('with the specified keys omitted', () {
var actual = {};

forwardUnconsumedProps({
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
}, keysToOmit: [
'prop 2',
'prop 4'
], propsToUpdate: actual);
test('with the specified sets of keys omitted', () {
var actual = {};

var expected = {
'prop 1': 'my prop #1',
'prop 3': 'my prop #3',
};
functionToTest({
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
'prop 5': 'my prop #5',
'prop 6': 'my prop #6',
}, keySetsToOmit: [
[
'prop 1',
'prop 3'
],
[
'prop 4',
'prop 5'
],
], propsToUpdate: actual);

expect(actual, equals(expected));
});
var expected = {
'prop 2': 'my prop #2',
'prop 6': 'my prop #6',
};

test('with the specified sets of keys omitted', () {
var actual = {};
expect(actual, equals(expected));
});

forwardUnconsumedProps({
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
'prop 5': 'my prop #5',
'prop 6': 'my prop #6',
}, keySetsToOmit: [
[
'prop 1',
'prop 3'
],
[
'prop 4',
'prop 5'
],
], propsToUpdate: actual);
test('when keySetsToOmit is empty', () {
var actual = {};

var expected = {
'prop 2': 'my prop #2',
'prop 6': 'my prop #6',
};
functionToTest({
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
'prop 5': 'my prop #5',
'prop 6': 'my prop #6',
}, keySetsToOmit: [], propsToUpdate: actual);

expect(actual, equals(expected));
});

test('when keySetsToOmit is empty', () {
var actual = {};
var expected = {
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
'prop 5': 'my prop #5',
'prop 6': 'my prop #6',
};

forwardUnconsumedProps({
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
'prop 5': 'my prop #5',
'prop 6': 'my prop #6',
}, keySetsToOmit: [], propsToUpdate: actual);
expect(actual, equals(expected));
});

var expected = {
'prop 1': 'my prop #1',
'prop 2': 'my prop #2',
'prop 3': 'my prop #3',
'prop 4': 'my prop #4',
'prop 5': 'my prop #5',
'prop 6': 'my prop #6',
};
test('with only valid DOM/SVG props', () {
var actual = {};

expect(actual, equals(expected));
});
functionToTest({
'tabIndex': '0',
'className': 'my classname',
'cx': '0',
'stroke': 'red',
'data-test-prop': 'my data attr',
'aria-test-prop': 'my aria attr',
'classNameBlacklist': 'my classname blacklist',
'custom prop': 'my custom prop',
}, onlyCopyDomProps: true, propsToUpdate: actual);

test('with only valid DOM/SVG props', () {
var actual = {};
var expected = {
'tabIndex': '0',
'className': 'my classname',
'cx': '0',
'stroke': 'red',
'data-test-prop': 'my data attr',
'aria-test-prop': 'my aria attr',
};

forwardUnconsumedProps({
expect(actual, equals(expected));
});
});
}

// This test is necessary because `forwardUnconsumedProps` doesn't actually
// filter out DOM props that should be omitted. Therefore, a shared test
// for `forwardUnconsumedProps` and `forwardUnconsumedPropsV2` is useful
// to demonstrate the change in behavior.
void commonDomPropsFilteringTest(ForwardUnconsumedPropsFunction functionToTest, {bool shouldFilter = true}) {
test('(common DOM props filtering test) ${shouldFilter ? 'should' : 'shouldn\'t'} filter DOM props', () {
final actual = {};
const startingPropsMap = {
'tabIndex': '0',
'className': 'my classname',
'cx': '0',
Expand All @@ -247,19 +289,33 @@ main() {
'aria-test-prop': 'my aria attr',
'classNameBlacklist': 'my classname blacklist',
'custom prop': 'my custom prop',
}, onlyCopyDomProps: true, propsToUpdate: actual);
};

var expected = {
functionToTest(
startingPropsMap,
keysToOmit: [
'stroke',
'className',
], onlyCopyDomProps: true, propsToUpdate: actual);

final expected = shouldFilter ? {
'tabIndex': '0',
'className': 'my classname',
'cx': '0',
'stroke': 'red',
'data-test-prop': 'my data attr',
'aria-test-prop': 'my aria attr',
};
} : startingPropsMap;

expect(actual, equals(expected));
});
}
group('forwardUnconsumedProps', () {
commonPropsForwardingUtilTests(forwardUnconsumedProps);
commonDomPropsFilteringTest(forwardUnconsumedProps, shouldFilter: false);
});

group('forwardUnconsumedPropsV2', () {
commonPropsForwardingUtilTests(forwardUnconsumedPropsV2);
commonDomPropsFilteringTest(forwardUnconsumedPropsV2);
});

group('newStyleFromProps() returns', () {
Expand Down

0 comments on commit 728f513

Please sign in to comment.