Skip to content

Commit

Permalink
Merge pull request #440 from Workiva/fix-over_react_redux-tests-redux…
Browse files Browse the repository at this point in the history
…-7.1.3

CPLAT-8834 fix tests for redux 7.1.3
  • Loading branch information
rm-astro-wf authored Dec 12, 2019
2 parents 3cb8efd + 2cc4379 commit b3e73a9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 83 deletions.
123 changes: 61 additions & 62 deletions test/over_react_redux/connect_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ main() {
group('connect', () {
UiFactory<CounterProps> ConnectedCounter;
TestJacket<CounterComponent> jacket;
dynamic counterRef;
final counterRef = createRef<CounterComponent>();

JsConnectOptions connectOptions;
var originalConnect = mockableJsConnect;
Expand Down Expand Up @@ -83,10 +83,10 @@ main() {

render(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref=(ref){counterRef = ref;})('test'),
(ConnectedCounter()..ref = counterRef)('test'),
),
);
expect(getDartComponent(counterRef), isA<CounterComponent>());
expect(counterRef.current, isA<CounterComponent>());
});
});

Expand All @@ -98,12 +98,12 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; })('test'),
(ConnectedCounter()..ref = counterRef)('test'),
),
);

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 0);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
expect(counterRef.current.props.currentCount, 0);
expect(jacket.mountNode.innerHtml, contains('Count: 0'));
});

test('after dispatch', () async {
Expand All @@ -113,21 +113,21 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; })('test'),
(ConnectedCounter()..ref = counterRef)('test'),
),
);

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 0);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
expect(counterRef.current.props.currentCount, 0);
expect(jacket.mountNode.innerHtml, contains('Count: 0'));

var dispatchButton = getByTestId(jacket.getInstance(), 'button-increment');
var dispatchButton = queryByTestId(jacket.mountNode, 'button-increment');
click(dispatchButton);

// wait for the next tick for the async dispatch to propagate
await Future(() {});

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 1);
expect(jacket.getNode().innerHtml, contains('Count: 1'));
expect(counterRef.current.props.currentCount, 1);
expect(jacket.mountNode.innerHtml, contains('Count: 1'));
});
});

Expand All @@ -139,12 +139,12 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; })('test'),
(ConnectedCounter()..ref = counterRef)('test'),
),
);

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 0);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
expect(counterRef.current.props.currentCount, 0);
expect(jacket.mountNode.innerHtml, contains('Count: 0'));
});

test('after dispatch', () async {
Expand All @@ -154,21 +154,21 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; })('test'),
(ConnectedCounter()..ref = counterRef)('test'),
),
);

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 0);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
expect(counterRef.current.props.currentCount, 0);
expect(jacket.mountNode.innerHtml, contains('Count: 0'));

var dispatchButton = getByTestId(jacket.getInstance(), 'button-increment');
var dispatchButton = queryByTestId(jacket.mountNode, 'button-increment');
click(dispatchButton);

// wait for the next tick for the async dispatch to propagate
await Future(() {});

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 1);
expect(jacket.getNode().innerHtml, contains('Count: 1'));
expect(counterRef.current.props.currentCount, 1);
expect(jacket.mountNode.innerHtml, contains('Count: 1'));
});
});

Expand All @@ -187,24 +187,24 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; })('test'),
(ConnectedCounter()..ref = counterRef)('test'),
),
);

expect(getDartComponent<CounterComponent>(counterRef).props.decrement, isA<Function>());
expect(counterRef.current.props.decrement, isA<Function>());

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 0);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
expect(counterRef.current.props.currentCount, 0);
expect(jacket.mountNode.innerHtml, contains('Count: 0'));

// Click button mapped to trigger `propFromDispatch` prop.
var dispatchButton = getByTestId(jacket.getInstance(), 'button-decrement');
var dispatchButton = queryByTestId(jacket.mountNode, 'button-decrement');
click(dispatchButton);

// wait for the next tick for the async dispatch to propagate
await Future(() {});

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, -1);
expect(jacket.getNode().innerHtml, contains('Count: -1'));
expect(counterRef.current.props.currentCount, -1);
expect(jacket.mountNode.innerHtml, contains('Count: -1'));
});
});

Expand All @@ -223,24 +223,24 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; })('test'),
(ConnectedCounter()..ref = counterRef)('test'),
),
);

expect(getDartComponent<CounterComponent>(counterRef).props.decrement, isA<Function>());
expect(counterRef.current.props.decrement, isA<Function>());

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 0);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
expect(counterRef.current.props.currentCount, 0);
expect(jacket.mountNode.innerHtml, contains('Count: 0'));

// Click button mapped to trigger `propFromDispatch` prop.
var dispatchButton = getByTestId(jacket.getInstance(), 'button-decrement');
var dispatchButton = queryByTestId(jacket.mountNode, 'button-decrement');
click(dispatchButton);

// wait for the next tick for the async dispatch to propagate
await Future(() {});

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, -1);
expect(jacket.getNode().innerHtml, contains('Count: -1'));
expect(counterRef.current.props.currentCount, -1);
expect(jacket.mountNode.innerHtml, contains('Count: -1'));
});
});

Expand Down Expand Up @@ -268,30 +268,30 @@ main() {
jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()
..ref = (ref){ counterRef = ref; }
..ref = counterRef
// make `decrement` increment
..decrement = () {store1.dispatch(IncrementAction());}
..currentCount = 900
)('test'),
),
);
// `button-decrement` will be incrementing now
var dispatchButton = getByTestId(jacket.getInstance(), 'button-decrement');
var dispatchButton = queryByTestId(jacket.mountNode, 'button-decrement');

expect(getDartComponent<CounterComponent>(counterRef).props.decrement, isA<Function>());
expect(counterRef.current.props.decrement, isA<Function>());

// state.count is at 0
expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 900);
expect(jacket.getNode().innerHtml, contains('Count: 900'));
expect(counterRef.current.props.currentCount, 900);
expect(jacket.mountNode.innerHtml, contains('Count: 900'));

// Click button mapped to trigger `propFromDispatch` prop.
click(dispatchButton); // state.count is now equal to 1

// wait for the next tick for the async dispatch to propagate
await Future(() {});

expect(getDartComponent<CounterComponent>(counterRef).props.currentCount, 1);
expect(jacket.getNode().innerHtml, contains('Count: 1'));
expect(counterRef.current.props.currentCount, 1);
expect(jacket.mountNode.innerHtml, contains('Count: 1'));
});
});

Expand Down Expand Up @@ -328,7 +328,6 @@ main() {
areStatePropsEqual: (next, prev) {
expect(next, isA<CounterProps>());
expect(prev, isA<CounterProps>());
expect(next.currentCount, 1);
methodsCalled.add('areStatePropsEqual');
// Force it to always be true, meaing it shouldnt re-render if they change.
return true;
Expand All @@ -338,13 +337,13 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; }..currentCount = 0)('test'),
(ConnectedCounter()..ref = counterRef..currentCount = 0)('test'),
),
);
expect(methodsCalled, ['mapStateToProps']);
methodsCalled.clear();

var dispatchButton = getByTestId(jacket.getInstance(), 'button-increment');
var dispatchButton = queryByTestId(jacket.mountNode, 'button-increment');
click(dispatchButton);

// wait for the next tick for the async dispatch to propagate
Expand All @@ -353,7 +352,7 @@ main() {
// store.state.count should be 1 but does not re-render due to override in `areStatePropsEqual`

expect(methodsCalled, ['mapStateToProps', 'areStatePropsEqual']);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
expect(jacket.mountNode.innerHtml, contains('Count: 0'));
});
});

Expand Down Expand Up @@ -398,24 +397,24 @@ main() {

jacket = mount(
(ReduxProvider()..store = store1)(
(ConnectedCounter()..ref = (ref){ counterRef = ref; }..currentCount = 0)('test'),
(ConnectedCounter()..ref = counterRef..currentCount = 0)('test'),
),
);

// `mapStateToProps` is called once,
// then `areStatesEqual` shows up 2 times due to `initialState`.
expect(methodsCalled, ['mapStateToProps', 'areStatesEqual', 'areStatesEqual']);
expect(methodsCalled, contains('mapStateToProps'));
expect(methodsCalled, contains('areStatesEqual'));
methodsCalled.clear();

var dispatchButton = getByTestId(jacket.getInstance(), 'button-increment');
var dispatchButton = queryByTestId(jacket.mountNode, 'button-increment');
click(dispatchButton);

// wait for the next tick for the async dispatch to propagate
await Future(() {});

// only checks `areStatesEqual` and does not call `mapStateToProps` since it returned `true`.
expect(methodsCalled, ['areStatesEqual']);
expect(jacket.getNode().innerHtml, contains('Count: 0'));
// only calls `areStatesEqual` and does not call `mapStateToProps` since it returned `true`.
expect(methodsCalled, isNot(contains('mapStateToProps')));
expect(methodsCalled, contains('areStatesEqual'));
expect(jacket.mountNode.innerHtml, contains('Count: 0'));
});
});
});
Expand Down Expand Up @@ -449,13 +448,13 @@ main() {
),
));

var bigCounter = getDartComponent(getByTestId(jacket.getInstance(), 'big-counter'));
var dispatchButton = queryByTestId(findDomNode(bigCounter), 'button-increment');
var bigCounter = queryByTestId(jacket.mountNode, 'big-counter');
var dispatchButton = queryByTestId(bigCounter, 'button-increment');
click(dispatchButton);

await Future((){});

expect(findDomNode(bigCounter).innerHtml, contains('Count: 100'));
expect(bigCounter.innerHtml, contains('Count: 100'));
});

test('correctly renderes when contexts are nested', () async {
Expand Down Expand Up @@ -492,11 +491,11 @@ main() {
)
);

var bigCounter = getDartComponent(getByTestId(jacket.getInstance(), 'big-counter'));
var smallCounter = getDartComponent(getByTestId(jacket.getInstance(), 'small-counter'));
var bigCounter = queryByTestId(jacket.mountNode, 'big-counter');
var smallCounter = queryByTestId(jacket.mountNode, 'small-counter');

var smallDispatchButton = queryByTestId(findDomNode(smallCounter), 'button-increment');
var dispatchButton = queryByTestId(findDomNode(bigCounter), 'button-increment');
var smallDispatchButton = queryByTestId(smallCounter, 'button-increment');
var dispatchButton = queryByTestId(bigCounter, 'button-increment');

click(dispatchButton);
click(smallDispatchButton);
Expand All @@ -507,7 +506,7 @@ main() {
expect(findDomNode(bigCounter).innerHtml, contains('Count: 100'), reason: 'Should have a count of 100');

// Normal counter incremented only 1 at both instances
expect(findDomNode(getByTestId(jacket.getInstance(), 'outside')).innerHtml, contains('Count: 1</div>'));
expect(findDomNode(queryByTestId(jacket.mountNode, 'outside')).innerHtml, contains('Count: 1</div>'));
expect(findDomNode(bigCounter).innerHtml, contains('Count: 1</div>'));
});
});
Expand Down
45 changes: 24 additions & 21 deletions test/over_react_redux/fixtures/counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,32 @@ class _$CounterProps extends UiProps with ConnectPropsMixin {
class CounterComponent extends UiComponent2<CounterProps> {
@override
render() {
return (Dom.div()..style = props.wrapperStyles)(
Dom.div()('Count: ${props.currentCount}'),
(Dom.button()
..addTestId('button-increment')
..onClick = (_) {
if (props.increment != null) {
props.increment();
} else if (props.dispatch != null) {
props.dispatch(IncrementAction());
}
return (Dom.div()
..modifyProps(addUnconsumedProps)
..style = props.wrapperStyles
)(
Dom.div()('Count: ${props.currentCount}'),
(Dom.button()
..addTestId('button-increment')
..onClick = (_) {
if (props.increment != null) {
props.increment();
} else if (props.dispatch != null) {
props.dispatch(IncrementAction());
}
)('+'),
(Dom.button()
..addTestId('button-decrement')
..onClick = (_) {
if (props.decrement != null) {
props.decrement();
} else if (props.dispatch != null) {
props.dispatch(DecrementAction());
}
}
)('+'),
(Dom.button()
..addTestId('button-decrement')
..onClick = (_) {
if (props.decrement != null) {
props.decrement();
} else if (props.dispatch != null) {
props.dispatch(DecrementAction());
}
)('-'),
props.children
}
)('-'),
props.children
);
}
}

0 comments on commit b3e73a9

Please sign in to comment.