Skip to content

Question: How to know if the loading status of Suspense resolved #14577

Closed
@chenesan

Description

@chenesan

Do you want to request a feature or report a bug?

Question

What is the current behavior?

I'm trying to implement supports of React.lazy and React.Suspense in enzyme (enzymejs/enzyme#1975). I'd like to have something like waitUntilLazyLoaded so we can write such test:

// in DynamicComponent.js

class DynamicComponent extends React.Component {
  render() {
    // render something
  }
}

// in test.js

const LazyComponent = lazy(() => import('./DynamicComponent'));
const Fallback = () => <div />;
const SuspenseComponent = () => (
  <Suspense fallback={<Fallback />}>
    <LazyComponent />
  </Suspense>
);

// mount the react element
const wrapper = mount(<SuspenseComponent />)

// On starter this should render Fallback
expect(wrapper.find('Fallback')).to.have.lengthOf(1)
expect(wrapper.find('DynamicComponent')).to.have.lenghtOf(0)

// wait for LazyComponent loading DynamicComponent and update of rendering
await wrapper.waitUntilLazyLoaded()

// render loaded component now
expect(wrapper.find('Fallback')).to.have.lengthOf(0)
expect(wrapper.find('DynamicComponent')).to.have.lengthOf(1)

Inside the mount implementation we call the ReactDOM.render to render it and get the root fiber node. Now my problem is: Given a Fiber node of Suspense, how could we know the loading status of Suspense so I can make sure whether (1) the module is loaded successfully loaded (or failed) and (2) React has rendered (or not rendered) the new component tree?

I'm not familiar with the implementation detail of Fiber and I'm still trying to investigate into this. That would be great if someone familiar with this could answer. Thanks!

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

^16.6

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions