question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 (airbnb/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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
chenesancommented, Jan 20, 2019

Ah, I found out that to test lazy synchronously we can just handle it with mock utility in jest or sinon, no need to have resetLoader.


// in LazyComponent.js
const LazyComponent = React.lazy(() => import("./InternalComponent"))

// in some component using LazyComponent
const ComponentToTest = () => {
  return (<Suspense fallback={<Fallback />}>
    <LazyComponent />
  </Suspense)
}

// in test code (using jest)

beforeAll(() => {
  jest.mock('/path/to/LazyComponent', () => InternalComponent) // or pass synchronous thenable to React.lazy
})

// will render ComponentToTest with InternalComponent in initial mount.
const ComponentToTest = require('./ComponentToTest')
ReactDOM.render(<ComponentToTest />)

Still thanks for discussion 😃

1reaction
tmkasuncommented, Nov 14, 2019

I’m too having this testing issue,Simply my problem is, How to test components with <Suspense/> and lazy loads. I’m using babel-plugin-dynamic-import-node for dynamic lazy imports, But couldn’t find a proper solution for thenable <Suspense/> output. The most viable method I could found was, mocking the React.Suspense implementation to make it synchronous.

I couldn’t understand how @gaearon’s https://github.com/facebook/react/pull/14626 PR could help to solve this testing issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading States with Suspense - Relay
To render loading states while a query is being fetched, we rely on React Suspense. ... When the resource finally loads, React will...
Read more >
Suspense for Data Fetching (Experimental) - React
This poses a question of how do we know what to fetch before rendering the next screen. There are several ways to solve...
Read more >
How does React.Suspense know that a promise has resolved?
This example works as expected. We see the loading message and then the component fully renders once the data has been fetched. However,...
Read more >
React Suspense: Lessons Learned While Loading Data
If any of the pages are uncached (which really should only be the last page in the list) then a promise is thrown,...
Read more >
How to Use React Suspense to Improve Your UI Load Time
React knows under the hood to call this read() method to determine when to trigger a component rerender. If we call this component...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found