|
| 1 | +@TestOn('browser') |
| 2 | +@JS() |
| 3 | +library react_test_utils_test; |
| 4 | + |
| 5 | +import 'dart:html'; |
| 6 | +import 'dart:js_util'; |
| 7 | + |
| 8 | +import 'package:js/js.dart'; |
| 9 | +import 'package:react/react.dart' as react; |
| 10 | +import 'package:react/react.dart'; |
| 11 | +import 'package:react/react_client/component_factory.dart'; |
| 12 | +import 'package:react/react_client/react_interop.dart'; |
| 13 | +import 'package:react/react_dom.dart' as react_dom; |
| 14 | +import 'package:react/src/js_interop_util.dart'; |
| 15 | +import 'package:test/test.dart'; |
| 16 | + |
| 17 | +import './react_suspense_lazy_component.dart' deferred as simple; |
| 18 | + |
| 19 | +@JS('React.lazy') |
| 20 | +external ReactClass jsLazy(Promise Function() factory); |
| 21 | + |
| 22 | +// Only intended for testing purposes, Please do not copy/paste this into repo. |
| 23 | +// This will most likely be added to the PUBLIC api in the future, |
| 24 | +// but needs more testing and Typing decisions to be made first. |
| 25 | +ReactJsComponentFactoryProxy lazy(Future<ReactComponentFactoryProxy> factory()) => ReactJsComponentFactoryProxy( |
| 26 | + jsLazy( |
| 27 | + allowInterop( |
| 28 | + () => futureToPromise( |
| 29 | + // React.lazy only supports "default exports" from a module. |
| 30 | + // This `{default: yourExport}` workaround can be found in the React.lazy RFC comments. |
| 31 | + // See: https://github.com/reactjs/rfcs/pull/64#issuecomment-431507924 |
| 32 | + (() async => jsify({'default': (await factory()).type}))(), |
| 33 | + ), |
| 34 | + ), |
| 35 | + ), |
| 36 | + ); |
| 37 | + |
| 38 | +main() { |
| 39 | + group('Suspense', () { |
| 40 | + test('renders fallback UI first followed by the real component', () async { |
| 41 | + final lazyComponent = lazy(() async { |
| 42 | + await simple.loadLibrary(); |
| 43 | + await Future.delayed(Duration(seconds: 1)); |
| 44 | + return simple.SimpleFunctionComponent; |
| 45 | + }); |
| 46 | + var wrappingDivRef; |
| 47 | + var mountElement = new Element.div(); |
| 48 | + react_dom.render( |
| 49 | + react.div({ |
| 50 | + 'ref': (ref) { |
| 51 | + wrappingDivRef = ref; |
| 52 | + } |
| 53 | + }, [ |
| 54 | + react.Suspense({ |
| 55 | + 'fallback': react.span({'id': 'loading'}, 'Loading...') |
| 56 | + }, [ |
| 57 | + lazyComponent({}) |
| 58 | + ]) |
| 59 | + ]), |
| 60 | + mountElement, |
| 61 | + ); |
| 62 | + |
| 63 | + expect(wrappingDivRef.querySelector('#loading'), isNotNull, |
| 64 | + reason: 'It should be showing the fallback UI for 2 seconds.'); |
| 65 | + expect(wrappingDivRef.querySelector('#simple-component'), isNull, |
| 66 | + reason: 'This component should not be present yet.'); |
| 67 | + await Future.delayed(Duration(seconds: 2)); |
| 68 | + expect(wrappingDivRef.querySelector('#simple-component'), isNotNull, |
| 69 | + reason: 'This component should be present now.'); |
| 70 | + expect(wrappingDivRef.querySelector('#loading'), isNull, reason: 'The loader should have hidden.'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('is instant after the lazy component has been loaded once', () async { |
| 74 | + final lazyComponent = lazy(() async { |
| 75 | + await simple.loadLibrary(); |
| 76 | + await Future.delayed(Duration(seconds: 1)); |
| 77 | + return simple.SimpleFunctionComponent; |
| 78 | + }); |
| 79 | + var wrappingDivRef; |
| 80 | + var wrappingDivRef2; |
| 81 | + var mountElement = new Element.div(); |
| 82 | + var mountElement2 = new Element.div(); |
| 83 | + |
| 84 | + react_dom.render( |
| 85 | + react.div({ |
| 86 | + 'ref': (ref) { |
| 87 | + wrappingDivRef = ref; |
| 88 | + } |
| 89 | + }, [ |
| 90 | + react.Suspense({ |
| 91 | + 'fallback': react.span({'id': 'loading'}, 'Loading...') |
| 92 | + }, [ |
| 93 | + lazyComponent({}) |
| 94 | + ]) |
| 95 | + ]), |
| 96 | + mountElement, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(wrappingDivRef.querySelector('#loading'), isNotNull, |
| 100 | + reason: 'It should be showing the fallback UI for 2 seconds.'); |
| 101 | + expect(wrappingDivRef.querySelector('#simple-component'), isNull, |
| 102 | + reason: 'This component should not be present yet.'); |
| 103 | + await Future.delayed(Duration(seconds: 2)); |
| 104 | + expect(wrappingDivRef.querySelector('#simple-component'), isNotNull, |
| 105 | + reason: 'This component should be present now.'); |
| 106 | + expect(wrappingDivRef.querySelector('#loading'), isNull, reason: 'The loader should have hidden.'); |
| 107 | + |
| 108 | + // Mounting to a new element should be instant since it was already loaded before |
| 109 | + react_dom.render( |
| 110 | + react.div({ |
| 111 | + 'ref': (ref) { |
| 112 | + wrappingDivRef2 = ref; |
| 113 | + } |
| 114 | + }, [ |
| 115 | + react.Suspense({ |
| 116 | + 'fallback': react.span({'id': 'loading'}, 'Loading...') |
| 117 | + }, [ |
| 118 | + lazyComponent({}) |
| 119 | + ]) |
| 120 | + ]), |
| 121 | + mountElement2, |
| 122 | + ); |
| 123 | + expect(wrappingDivRef2.querySelector('#simple-component'), isNotNull, |
| 124 | + reason: 'Its already been loaded, so this should appear instantly.'); |
| 125 | + expect(wrappingDivRef2.querySelector('#loading'), isNull, |
| 126 | + reason: 'Its already been loaded, so the loading UI shouldn\'t show.'); |
| 127 | + }); |
| 128 | + }); |
| 129 | +} |
0 commit comments