|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:developer'; |
| 3 | +@TestOn('browser') |
| 4 | +import 'dart:html'; |
| 5 | +import 'dart:js_util'; |
| 6 | + |
| 7 | +import 'package:react/react.dart' as react; |
| 8 | +import 'package:react/react_client.dart'; |
| 9 | +import 'package:react/react_test_utils.dart' as rtu; |
| 10 | +import 'package:test/test.dart'; |
| 11 | + |
| 12 | +main() { |
| 13 | + setClientConfiguration(); |
| 14 | + |
| 15 | + Ref<_MemoTestWrapperComponent> memoTestWrapperComponentRef; |
| 16 | + Ref<Element> localCountDisplayRef; |
| 17 | + Ref<Element> valueMemoShouldIgnoreViaAreEqualDisplayRef; |
| 18 | + int childMemoRenderCount; |
| 19 | + |
| 20 | + void renderMemoTest({ |
| 21 | + bool testAreEqual = false, |
| 22 | + String displayName, |
| 23 | + }) { |
| 24 | + expect(memoTestWrapperComponentRef, isNotNull, reason: 'test setup sanity check'); |
| 25 | + expect(localCountDisplayRef, isNotNull, reason: 'test setup sanity check'); |
| 26 | + expect(valueMemoShouldIgnoreViaAreEqualDisplayRef, isNotNull, reason: 'test setup sanity check'); |
| 27 | + |
| 28 | + final customAreEqualFn = !testAreEqual |
| 29 | + ? null |
| 30 | + : (prevProps, nextProps) { |
| 31 | + return prevProps['localCount'] == nextProps['localCount']; |
| 32 | + }; |
| 33 | + |
| 34 | + final MemoTest = react.memo((Map props) { |
| 35 | + childMemoRenderCount++; |
| 36 | + return react.div( |
| 37 | + {}, |
| 38 | + react.p( |
| 39 | + {'ref': localCountDisplayRef}, |
| 40 | + props['localCount'], |
| 41 | + ), |
| 42 | + react.p( |
| 43 | + {'ref': valueMemoShouldIgnoreViaAreEqualDisplayRef}, |
| 44 | + props['valueMemoShouldIgnoreViaAreEqual'], |
| 45 | + ), |
| 46 | + ); |
| 47 | + }, areEqual: customAreEqualFn, displayName: displayName); |
| 48 | + |
| 49 | + rtu.renderIntoDocument(MemoTestWrapper({ |
| 50 | + 'ref': memoTestWrapperComponentRef, |
| 51 | + 'memoComponentFactory': MemoTest, |
| 52 | + })); |
| 53 | + |
| 54 | + expect(localCountDisplayRef.current, isNotNull, reason: 'test setup sanity check'); |
| 55 | + expect(valueMemoShouldIgnoreViaAreEqualDisplayRef.current, isNotNull, reason: 'test setup sanity check'); |
| 56 | + expect(memoTestWrapperComponentRef.current.redrawCount, 0, reason: 'test setup sanity check'); |
| 57 | + expect(childMemoRenderCount, 1, reason: 'test setup sanity check'); |
| 58 | + expect(memoTestWrapperComponentRef.current.state['localCount'], 0, reason: 'test setup sanity check'); |
| 59 | + expect(memoTestWrapperComponentRef.current.state['valueMemoShouldIgnoreViaAreEqual'], 0, |
| 60 | + reason: 'test setup sanity check'); |
| 61 | + expect(memoTestWrapperComponentRef.current.state['valueMemoShouldNotKnowAbout'], 0, |
| 62 | + reason: 'test setup sanity check'); |
| 63 | + } |
| 64 | + |
| 65 | + group('memo', () { |
| 66 | + setUp(() { |
| 67 | + memoTestWrapperComponentRef = react.createRef<_MemoTestWrapperComponent>(); |
| 68 | + localCountDisplayRef = react.createRef<Element>(); |
| 69 | + valueMemoShouldIgnoreViaAreEqualDisplayRef = react.createRef<Element>(); |
| 70 | + childMemoRenderCount = 0; |
| 71 | + }); |
| 72 | + |
| 73 | + tearDown(() { |
| 74 | + memoTestWrapperComponentRef = null; |
| 75 | + localCountDisplayRef = null; |
| 76 | + valueMemoShouldIgnoreViaAreEqualDisplayRef = null; |
| 77 | + }); |
| 78 | + |
| 79 | + group('renders its child component when props change', () { |
| 80 | + test('', () { |
| 81 | + renderMemoTest(); |
| 82 | + |
| 83 | + memoTestWrapperComponentRef.current.increaseLocalCount(); |
| 84 | + expect(memoTestWrapperComponentRef.current.state['localCount'], 1, reason: 'test setup sanity check'); |
| 85 | + expect(memoTestWrapperComponentRef.current.redrawCount, 1, reason: 'test setup sanity check'); |
| 86 | + |
| 87 | + expect(childMemoRenderCount, 2); |
| 88 | + expect(localCountDisplayRef.current.text, '1'); |
| 89 | + |
| 90 | + memoTestWrapperComponentRef.current.increaseValueMemoShouldIgnoreViaAreEqual(); |
| 91 | + expect(memoTestWrapperComponentRef.current.state['valueMemoShouldIgnoreViaAreEqual'], 1, |
| 92 | + reason: 'test setup sanity check'); |
| 93 | + expect(memoTestWrapperComponentRef.current.redrawCount, 2, reason: 'test setup sanity check'); |
| 94 | + |
| 95 | + expect(childMemoRenderCount, 3); |
| 96 | + expect(valueMemoShouldIgnoreViaAreEqualDisplayRef.current.text, '1'); |
| 97 | + }); |
| 98 | + |
| 99 | + test('unless the areEqual argument is set to a function that customizes when re-renders occur', () { |
| 100 | + renderMemoTest(testAreEqual: true); |
| 101 | + |
| 102 | + memoTestWrapperComponentRef.current.increaseValueMemoShouldIgnoreViaAreEqual(); |
| 103 | + expect(memoTestWrapperComponentRef.current.state['valueMemoShouldIgnoreViaAreEqual'], 1, |
| 104 | + reason: 'test setup sanity check'); |
| 105 | + expect(memoTestWrapperComponentRef.current.redrawCount, 1, reason: 'test setup sanity check'); |
| 106 | + |
| 107 | + expect(childMemoRenderCount, 1); |
| 108 | + expect(valueMemoShouldIgnoreViaAreEqualDisplayRef.current.text, '0'); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + test('does not re-render its child component when parent updates and props remain the same', () { |
| 113 | + renderMemoTest(); |
| 114 | + |
| 115 | + memoTestWrapperComponentRef.current.increaseValueMemoShouldNotKnowAbout(); |
| 116 | + expect(memoTestWrapperComponentRef.current.state['valueMemoShouldNotKnowAbout'], 1, |
| 117 | + reason: 'test setup sanity check'); |
| 118 | + expect(memoTestWrapperComponentRef.current.redrawCount, 1, reason: 'test setup sanity check'); |
| 119 | + |
| 120 | + expect(childMemoRenderCount, 1); |
| 121 | + }); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +final MemoTestWrapper = react.registerComponent(() => _MemoTestWrapperComponent()); |
| 126 | + |
| 127 | +class _MemoTestWrapperComponent extends react.Component2 { |
| 128 | + int redrawCount = 0; |
| 129 | + |
| 130 | + get initialState => { |
| 131 | + 'localCount': 0, |
| 132 | + 'valueMemoShouldIgnoreViaAreEqual': 0, |
| 133 | + 'valueMemoShouldNotKnowAbout': 0, |
| 134 | + }; |
| 135 | + |
| 136 | + @override |
| 137 | + void componentDidUpdate(Map prevProps, Map prevState, [dynamic snapshot]) { |
| 138 | + redrawCount++; |
| 139 | + } |
| 140 | + |
| 141 | + void increaseLocalCount() { |
| 142 | + this.setState({'localCount': this.state['localCount'] + 1}); |
| 143 | + } |
| 144 | + |
| 145 | + void increaseValueMemoShouldIgnoreViaAreEqual() { |
| 146 | + this.setState({'valueMemoShouldIgnoreViaAreEqual': this.state['valueMemoShouldIgnoreViaAreEqual'] + 1}); |
| 147 | + } |
| 148 | + |
| 149 | + void increaseValueMemoShouldNotKnowAbout() { |
| 150 | + this.setState({'valueMemoShouldNotKnowAbout': this.state['valueMemoShouldNotKnowAbout'] + 1}); |
| 151 | + } |
| 152 | + |
| 153 | + @override |
| 154 | + render() { |
| 155 | + return react.div( |
| 156 | + {}, |
| 157 | + props['memoComponentFactory']({ |
| 158 | + 'localCount': this.state['localCount'], |
| 159 | + 'valueMemoShouldIgnoreViaAreEqual': this.state['valueMemoShouldIgnoreViaAreEqual'], |
| 160 | + }), |
| 161 | + ); |
| 162 | + } |
| 163 | +} |
0 commit comments