-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathreact_memo_test.dart
179 lines (146 loc) · 6.31 KB
/
react_memo_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@TestOn('browser')
library react.react_memo_test;
import 'dart:html';
import 'package:react/react.dart' as react;
import 'package:react/react_client.dart';
import 'package:react/react_client/react_interop.dart';
import 'package:react/react_test_utils.dart' as rtu;
import 'package:test/test.dart';
import 'factory/common_factory_tests.dart';
main() {
group('memo2', () {
sharedMemoTests(react.memo2);
test('can be passed a forwardRef component (regression test)', () {
late ReactComponentFactoryProxy factory;
expect(() => factory = react.memo2(react.forwardRef2((props, ref) => 'foo')), returnsNormally);
expect(() => rtu.renderIntoDocument(factory({})), returnsNormally);
});
group('- common factory behavior -', () {
final Memo2Test = react.memo2(react.registerFunctionComponent((props) {
props['onDartRender']?.call(props);
return react.div({...props});
}));
commonFactoryTests(
Memo2Test,
// ignore: invalid_use_of_protected_member
dartComponentVersion: ReactDartComponentVersion.component2,
);
});
});
}
typedef MemoFunction = react.ReactComponentFactoryProxy Function(ReactDartFunctionComponentFactoryProxy,
{bool Function(Map, Map)? areEqual});
void sharedMemoTests(MemoFunction memoFunction) {
late Ref<_MemoTestWrapperComponent?> memoTestWrapperComponentRef;
late Ref<Element?> localCountDisplayRef;
late Ref<Element?> valueMemoShouldIgnoreViaAreEqualDisplayRef;
late int childMemoRenderCount;
void renderMemoTest({
bool testAreEqual = false,
}) {
final customAreEqualFn = !testAreEqual
? null
: (prevProps, nextProps) {
return prevProps['localCount'] == nextProps['localCount'];
};
final MemoTest = memoFunction(react.registerFunctionComponent((props) {
childMemoRenderCount++;
return react.div(
{},
react.p(
{'ref': localCountDisplayRef},
props['localCount'],
),
react.p(
{'ref': valueMemoShouldIgnoreViaAreEqualDisplayRef},
props['valueMemoShouldIgnoreViaAreEqual'],
),
);
}), areEqual: customAreEqualFn);
rtu.renderIntoDocument(MemoTestWrapper({
'ref': memoTestWrapperComponentRef,
'memoComponentFactory': MemoTest,
}));
expect(localCountDisplayRef.current, isNotNull, reason: 'test setup sanity check');
expect(valueMemoShouldIgnoreViaAreEqualDisplayRef.current, isNotNull, reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.redrawCount, 0, reason: 'test setup sanity check');
expect(childMemoRenderCount, 1, reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.state['localCount'], 0, reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.state['valueMemoShouldIgnoreViaAreEqual'], 0,
reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.state['valueMemoShouldNotKnowAbout'], 0,
reason: 'test setup sanity check');
}
setUp(() {
memoTestWrapperComponentRef = react.createRef<_MemoTestWrapperComponent>();
localCountDisplayRef = react.createRef<Element>();
valueMemoShouldIgnoreViaAreEqualDisplayRef = react.createRef<Element>();
childMemoRenderCount = 0;
});
group('renders its child component when props change', () {
test('', () {
renderMemoTest();
memoTestWrapperComponentRef.current!.increaseLocalCount();
expect(memoTestWrapperComponentRef.current!.state['localCount'], 1, reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.redrawCount, 1, reason: 'test setup sanity check');
expect(childMemoRenderCount, 2);
expect(localCountDisplayRef.current!.text, '1');
memoTestWrapperComponentRef.current!.increaseValueMemoShouldIgnoreViaAreEqual();
expect(memoTestWrapperComponentRef.current!.state['valueMemoShouldIgnoreViaAreEqual'], 1,
reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.redrawCount, 2, reason: 'test setup sanity check');
expect(childMemoRenderCount, 3);
expect(valueMemoShouldIgnoreViaAreEqualDisplayRef.current!.text, '1');
});
test('unless the areEqual argument is set to a function that customizes when re-renders occur', () {
renderMemoTest(testAreEqual: true);
memoTestWrapperComponentRef.current!.increaseValueMemoShouldIgnoreViaAreEqual();
expect(memoTestWrapperComponentRef.current!.state['valueMemoShouldIgnoreViaAreEqual'], 1,
reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.redrawCount, 1, reason: 'test setup sanity check');
expect(childMemoRenderCount, 1);
expect(valueMemoShouldIgnoreViaAreEqualDisplayRef.current!.text, '0');
});
});
test('does not re-render its child component when parent updates and props remain the same', () {
renderMemoTest();
memoTestWrapperComponentRef.current!.increaseValueMemoShouldNotKnowAbout();
expect(memoTestWrapperComponentRef.current!.state['valueMemoShouldNotKnowAbout'], 1,
reason: 'test setup sanity check');
expect(memoTestWrapperComponentRef.current!.redrawCount, 1, reason: 'test setup sanity check');
expect(childMemoRenderCount, 1);
});
}
final MemoTestWrapper = react.registerComponent2(() => _MemoTestWrapperComponent());
class _MemoTestWrapperComponent extends react.Component2 {
int redrawCount = 0;
@override
get initialState => {
'localCount': 0,
'valueMemoShouldIgnoreViaAreEqual': 0,
'valueMemoShouldNotKnowAbout': 0,
};
@override
componentDidUpdate(Map prevProps, Map prevState, [dynamic snapshot]) {
redrawCount++;
}
void increaseLocalCount() {
setState({'localCount': state['localCount'] + 1});
}
void increaseValueMemoShouldIgnoreViaAreEqual() {
setState({'valueMemoShouldIgnoreViaAreEqual': state['valueMemoShouldIgnoreViaAreEqual'] + 1});
}
void increaseValueMemoShouldNotKnowAbout() {
setState({'valueMemoShouldNotKnowAbout': state['valueMemoShouldNotKnowAbout'] + 1});
}
@override
render() {
return react.div(
{},
props['memoComponentFactory']({
'localCount': state['localCount'],
'valueMemoShouldIgnoreViaAreEqual': state['valueMemoShouldIgnoreViaAreEqual'],
}),
);
}
}