|
| 1 | +// ignore_for_file: deprecated_member_use_from_same_package |
| 2 | +@TestOn('browser') |
| 3 | +import 'dart:html'; |
| 4 | + |
| 5 | +import 'package:react/react.dart' as react; |
| 6 | +import 'package:react/react_client/react_interop.dart'; |
| 7 | +import 'package:react/react_dom.dart' as react_dom; |
| 8 | +import 'package:react/react_test_utils.dart'; |
| 9 | +import 'package:test/test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + // These tests redundantly test some React behavior, but mostly serve to validate that |
| 13 | + // all supported inputs/outputs are handled properly by the Dart bindings and their typings. |
| 14 | + group('react_dom entrypoint APIs:', () { |
| 15 | + group('render', () { |
| 16 | + group( |
| 17 | + 'accepts and renders content of different supported types,' |
| 18 | + ' and returns a representation of what was mounted', () { |
| 19 | + test('ReactElement for a DOM component', () { |
| 20 | + final mountNode = DivElement(); |
| 21 | + final result = react_dom.render(react.button({}, 'test button'), mountNode); |
| 22 | + expect(mountNode.children, [isA<ButtonElement>()]); |
| 23 | + expect(mountNode.children.single.innerText, 'test button'); |
| 24 | + expect(result, isA<ButtonElement>()); |
| 25 | + }); |
| 26 | + |
| 27 | + test('ReactElement for a class component', () { |
| 28 | + final mountNode = DivElement(); |
| 29 | + final result = react_dom.render(classComponent({}), mountNode); |
| 30 | + expect(mountNode.innerText, 'class component content'); |
| 31 | + expect(result, isA<ReactComponent>().having((c) => c.dartComponent, 'dartComponent', isA<_ClassComponent>())); |
| 32 | + }); |
| 33 | + |
| 34 | + test('ReactElement for a function component', () { |
| 35 | + final mountNode = DivElement(); |
| 36 | + final result = react_dom.render(functionComponent({}), mountNode); |
| 37 | + expect(mountNode.innerText, 'function component content'); |
| 38 | + expect(result, isNull); |
| 39 | + }); |
| 40 | + |
| 41 | + group('other "ReactNode" types:', () { |
| 42 | + test('string', () { |
| 43 | + final mountNode = DivElement(); |
| 44 | + final result = react_dom.render('test string', mountNode); |
| 45 | + expect(mountNode.innerText, 'test string'); |
| 46 | + expect(result, isA<Node>()); |
| 47 | + }); |
| 48 | + |
| 49 | + test('lists and nested lists', () { |
| 50 | + final mountNode = DivElement(); |
| 51 | + final result = react_dom.render([ |
| 52 | + 'test string', |
| 53 | + ['test string 2', react.span({}, 'test span')] |
| 54 | + ], mountNode); |
| 55 | + expect(mountNode.innerText, 'test string' 'test string 2' 'test span'); |
| 56 | + expect(result, isA<Node>()); |
| 57 | + }); |
| 58 | + |
| 59 | + test('number', () { |
| 60 | + final mountNode = DivElement(); |
| 61 | + final result = react_dom.render(123, mountNode); |
| 62 | + expect(mountNode.innerText, '123'); |
| 63 | + expect(result, isA<Node>()); |
| 64 | + }); |
| 65 | + |
| 66 | + test('false', () { |
| 67 | + final mountNode = DivElement(); |
| 68 | + react_dom.render(react.span({}, 'test content that will be cleared'), mountNode); |
| 69 | + expect(mountNode.innerText, 'test content that will be cleared'); |
| 70 | + final result = react_dom.render(false, mountNode); |
| 71 | + expect(mountNode.innerText, isEmpty); |
| 72 | + expect(result, isNull); |
| 73 | + }); |
| 74 | + |
| 75 | + test('null', () { |
| 76 | + final mountNode = DivElement(); |
| 77 | + react_dom.render(react.span({}, 'test content that will be cleared'), mountNode); |
| 78 | + expect(mountNode.innerText, 'test content that will be cleared'); |
| 79 | + final result = react_dom.render(null, mountNode); |
| 80 | + expect(mountNode.innerText, isEmpty); |
| 81 | + expect(result, isNull); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + group('unmountComponentAtNode', () { |
| 88 | + test('unmounts a React tree at a node, and returns true to indicate it has unmounted', () { |
| 89 | + final mountNode = DivElement(); |
| 90 | + react_dom.render(react.span({}), mountNode); |
| 91 | + final result = react_dom.unmountComponentAtNode(mountNode); |
| 92 | + expect(result, isTrue); |
| 93 | + }); |
| 94 | + |
| 95 | + test('returns false when a React tree has already been unmounted', () { |
| 96 | + final mountNode = DivElement(); |
| 97 | + react_dom.render(react.span({}), mountNode); |
| 98 | + final result = react_dom.unmountComponentAtNode(mountNode); |
| 99 | + expect(result, isTrue, reason: 'test setup check'); |
| 100 | + final secondUnmountResult = react_dom.unmountComponentAtNode(mountNode); |
| 101 | + expect(secondUnmountResult, isFalse); |
| 102 | + }); |
| 103 | + |
| 104 | + test('returns false when no React tree has been mounted within a node', () { |
| 105 | + final result = react_dom.unmountComponentAtNode(DivElement()); |
| 106 | + expect(result, isFalse); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + group('findDOMNode', () { |
| 111 | + group('returns the mounted element when provided', () { |
| 112 | + test('a Dart class component instance', () { |
| 113 | + final ref = createRef<_ClassComponent>(); |
| 114 | + renderIntoDocument(classComponent({'ref': ref})); |
| 115 | + final dartComponentInstance = ref.current; |
| 116 | + expect(dartComponentInstance, isNotNull, reason: 'test setup check'); |
| 117 | + dartComponentInstance!; |
| 118 | + expect(react_dom.findDOMNode(dartComponentInstance), isA<AnchorElement>()); |
| 119 | + }); |
| 120 | + |
| 121 | + test('a JS class component instance', () { |
| 122 | + final ref = createRef<_ClassComponent>(); |
| 123 | + renderIntoDocument(classComponent({'ref': ref})); |
| 124 | + final jsComponentInstance = ref.current!.jsThis; |
| 125 | + expect(jsComponentInstance, isNotNull, reason: 'test setup check'); |
| 126 | + expect(react_dom.findDOMNode(jsComponentInstance), isA<AnchorElement>()); |
| 127 | + }); |
| 128 | + |
| 129 | + test('an element representing a mounted component', () { |
| 130 | + final ref = createRef<SpanElement>(); |
| 131 | + renderIntoDocument(react.span({'ref': ref})); |
| 132 | + final element = ref.current; |
| 133 | + expect(element, isNotNull, reason: 'test setup check'); |
| 134 | + element!; |
| 135 | + expect(react_dom.findDOMNode(element), same(element)); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + test('returns null when a component does not render any content', () { |
| 140 | + final ref = createRef<_ClassComponentThatRendersNothing>(); |
| 141 | + renderIntoDocument(classComponentThatRendersNothing({'ref': ref})); |
| 142 | + final dartComponentInstance = ref.current; |
| 143 | + expect(dartComponentInstance, isNotNull, reason: 'test setup check'); |
| 144 | + dartComponentInstance!; |
| 145 | + expect(react_dom.findDOMNode(dartComponentInstance), isNull); |
| 146 | + }); |
| 147 | + |
| 148 | + test('passes through a non-React-mounted element', () { |
| 149 | + final element = DivElement(); |
| 150 | + expect(react_dom.findDOMNode(element), same(element)); |
| 151 | + }); |
| 152 | + |
| 153 | + test('passes through null', () { |
| 154 | + expect(react_dom.findDOMNode(null), isNull); |
| 155 | + }); |
| 156 | + |
| 157 | + group('throws when passed other objects that don\'t represent mounted React components', () { |
| 158 | + test('arbitrary Dart objects', () { |
| 159 | + expect(() => react_dom.findDOMNode(Object()), |
| 160 | + throwsA(isA<Object>().havingToStringValue(contains('Argument appears to not be a ReactComponent')))); |
| 161 | + }); |
| 162 | + |
| 163 | + test('ReactElement', () { |
| 164 | + expect(() => react_dom.findDOMNode(react.span({})), |
| 165 | + throwsA(isA<Object>().havingToStringValue(contains('Argument appears to not be a ReactComponent')))); |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | +} |
| 171 | + |
| 172 | +final classComponent = react.registerComponent2(() => _ClassComponent()); |
| 173 | + |
| 174 | +class _ClassComponent extends react.Component2 { |
| 175 | + @override |
| 176 | + Object? render() => react.a({}, 'class component content'); |
| 177 | +} |
| 178 | + |
| 179 | +final classComponentThatRendersNothing = react.registerComponent2(() => _ClassComponentThatRendersNothing()); |
| 180 | + |
| 181 | +class _ClassComponentThatRendersNothing extends react.Component2 { |
| 182 | + @override |
| 183 | + Object? render() => null; |
| 184 | +} |
| 185 | + |
| 186 | +final functionComponent = react.registerFunctionComponent((props) { |
| 187 | + return react.pre({}, 'function component content'); |
| 188 | +}); |
| 189 | + |
| 190 | +extension<T> on TypeMatcher<T> { |
| 191 | + TypeMatcher<T> havingToStringValue(dynamic matcher) => having((o) => o.toString(), 'toString() value', matcher); |
| 192 | +} |
0 commit comments