@samitouri / QOS-React-2 / commits / 401065fe5c

Adds test for #15732. (#15747)

Caleb Meredith committed May 28, 2019 at 06:25 UTC 401065fe5c822d39a80f2ce4b2821596bdb5f99a
1 file changed +49
packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js
+49
@@ -1869,4 +1869,53 @@ describe('ReactHooks', () => {
1869 Scheduler.flushAll();
1870 expect(root).toMatchRenderedOutput('hello');
1871 });
1872 +
1873 + // Regression test for https://github.com/facebook/react/issues/15732
1874 + it('resets hooks when an error is thrown in the middle of a list of hooks', async () => {
1875 + const {useEffect, useState} = React;
1876 +
1877 + class ErrorBoundary extends React.Component {
1878 + state = {hasError: false};
1879 +
1880 + static getDerivedStateFromError() {
1881 + return {hasError: true};
1882 + }
1883 +
1884 + render() {
1885 + return (
1886 + <Wrapper>
1887 + {this.state.hasError ? 'Error!' : this.props.children}
1888 + </Wrapper>
1889 + );
1890 + }
1891 + }
1892 +
1893 + function Wrapper({children}) {
1894 + return children;
1895 + }
1896 +
1897 + let setShouldThrow;
1898 + function Thrower() {
1899 + const [shouldThrow, _setShouldThrow] = useState(false);
1900 + setShouldThrow = _setShouldThrow;
1901 +
1902 + if (shouldThrow) {
1903 + throw new Error('Throw!');
1904 + }
1905 +
1906 + useEffect(() => {}, []);
1907 +
1908 + return 'Throw!';
1909 + }
1910 +
1911 + const root = ReactTestRenderer.create(
1912 + <ErrorBoundary>
1913 + <Thrower />
1914 + </ErrorBoundary>,
1915 + );
1916 +
1917 + expect(root).toMatchRenderedOutput('Throw!');
1918 + act(() => setShouldThrow(true));
1919 + expect(root).toMatchRenderedOutput('Error!');
1920 + });
1921 });