/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow */ import typeof ReactTestRenderer from 'react-test-renderer'; import type {FrontendBridge} from 'react-devtools-shared/src/bridge'; import type Store from 'react-devtools-shared/src/devtools/store'; import type { DispatcherContext, StateContext, } from 'react-devtools-shared/src/devtools/views/Components/TreeContext'; import {getVersionedRenderImplementation} from './utils'; describe('TreeListContext', () => { let React; let TestRenderer: ReactTestRenderer; let bridge: FrontendBridge; let store: Store; let utils; let withErrorsOrWarningsIgnored; let BridgeContext; let StoreContext; let TreeContext; let dispatch: DispatcherContext; let state: StateContext; beforeEach(() => { global.IS_REACT_ACT_ENVIRONMENT = true; utils = require('./utils'); utils.beforeEachProfiling(); withErrorsOrWarningsIgnored = utils.withErrorsOrWarningsIgnored; bridge = global.bridge; store = global.store; store.collapseNodesByDefault = false; React = require('react'); TestRenderer = utils.requireTestRenderer(); BridgeContext = require('react-devtools-shared/src/devtools/views/context').BridgeContext; StoreContext = require('react-devtools-shared/src/devtools/views/context').StoreContext; TreeContext = require('react-devtools-shared/src/devtools/views/Components/TreeContext'); }); const {render, unmount, createContainer} = getVersionedRenderImplementation(); afterEach(() => { // Reset between tests dispatch = ((null: any): DispatcherContext); state = ((null: any): StateContext); }); const Capture = () => { dispatch = React.useContext(TreeContext.TreeDispatcherContext); state = React.useContext(TreeContext.TreeStateContext); return null; }; const Contexts = () => { return ( ); }; describe('tree state', () => { it('should select the next and previous elements in the tree', () => { const Grandparent = () => ; const Parent = () => ( ); const Child = () => null; utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Test stepping through to the end utils.act(() => dispatch({type: 'SELECT_NEXT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); utils.act(() => dispatch({type: 'SELECT_NEXT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_NEXT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); utils.act(() => dispatch({type: 'SELECT_NEXT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Test stepping back to the beginning utils.act(() => dispatch({type: 'SELECT_PREVIOUS_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); utils.act(() => dispatch({type: 'SELECT_PREVIOUS_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_PREVIOUS_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); // Test wrap around behavior utils.act(() => dispatch({type: 'SELECT_PREVIOUS_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); utils.act(() => dispatch({type: 'SELECT_NEXT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); }); it('should select child elements', () => { const Grandparent = () => ( ); const Parent = () => ( ); const Child = () => null; utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ `); utils.act(() => dispatch({type: 'SELECT_ELEMENT_AT_INDEX', payload: 0})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); utils.act(() => dispatch({type: 'SELECT_CHILD_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_CHILD_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // There are no more children to select, so this should be a no-op utils.act(() => dispatch({type: 'SELECT_CHILD_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); }); it('should select parent elements and then collapse', () => { const Grandparent = () => ( ); const Parent = () => ( ); const Child = () => null; utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ `); const lastChildID = store.getElementIDAtIndex(store.numElements - 1); // Select the last child utils.act(() => dispatch({type: 'SELECT_ELEMENT_BY_ID', payload: lastChildID}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Select its parent utils.act(() => dispatch({type: 'SELECT_PARENT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); // Select grandparent utils.act(() => dispatch({type: 'SELECT_PARENT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); // No-op utils.act(() => dispatch({type: 'SELECT_PARENT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); const previousState = state; // There are no more ancestors to select, so this should be a no-op utils.act(() => dispatch({type: 'SELECT_PARENT_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toEqual(previousState); }); it('should clear selection if the selected element is unmounted', async () => { const Grandparent = props => props.children || null; const Parent = props => props.children || null; const Child = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Select the second child utils.act(() => dispatch({type: 'SELECT_ELEMENT_AT_INDEX', payload: 3})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Remove the child (which should auto-select the parent) await utils.actAsync(() => render( , ), ); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Unmount the root (so that nothing is selected) await utils.actAsync(() => unmount()); expect(state).toMatchInlineSnapshot(``); }); it('should navigate next/previous sibling and skip over children in between', () => { const Grandparent = () => ( ); const Parent = ({numChildren}) => new Array(numChildren) .fill(true) .map((_, index) => ); const Child = () => null; utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); const firstParentID = ((store.getElementIDAtIndex(1): any): number); utils.act(() => dispatch({type: 'SELECT_ELEMENT_BY_ID', payload: firstParentID}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_NEXT_SIBLING_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_NEXT_SIBLING_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_NEXT_SIBLING_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_PREVIOUS_SIBLING_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_PREVIOUS_SIBLING_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_PREVIOUS_SIBLING_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_PREVIOUS_SIBLING_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); }); it('should navigate the owner hierarchy', () => { const Wrapper = ({children}) => children; const Grandparent = () => ( ); const Parent = ({numChildren}) => new Array(numChildren) .fill(true) .map((_, index) => ); const Child = () => null; utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); const childID = ((store.getElementIDAtIndex(7): any): number); utils.act(() => dispatch({type: 'SELECT_ELEMENT_BY_ID', payload: childID}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Basic navigation test utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_PREVIOUS_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_PREVIOUS_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); // Noop (since we're at the root already) utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_PREVIOUS_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_NEXT_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_NEXT_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Noop (since we're at the leaf node) utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_NEXT_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Other navigational actions should clear out the temporary owner chain. utils.act(() => dispatch({type: 'SELECT_PREVIOUS_ELEMENT_IN_TREE'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ `); // Start a new tree on parent const parentID = ((store.getElementIDAtIndex(5): any): number); utils.act(() => dispatch({type: 'SELECT_ELEMENT_BY_ID', payload: parentID}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_PREVIOUS_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); // Noop (since we're at the top) utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_PREVIOUS_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → ▾ `); utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_NEXT_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); // Noop (since we're at the leaf of this owner tree) // It should not be possible to navigate beyond the owner chain leaf. utils.act(() => dispatch({type: 'SELECT_OWNER_LIST_NEXT_ELEMENT_IN_TREE'}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); }); }); describe('search state', () => { it('should find elements matching search text', () => { const Foo = () => null; const Bar = () => null; const Baz = () => null; const Qux = () => null; Qux.displayName = `withHOC(${Qux.name})`; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] [withHOC] `); // NOTE: multi-match utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'ba'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] [withHOC] `); // NOTE: single match utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'f'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → [withHOC] `); // NOTE: no match utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'y'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] → [withHOC] `); // NOTE: HOC match utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'w'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] [withHOC] `); }); it('should select the next and previous items within the search results', () => { const Foo = () => null; const Bar = () => null; const Baz = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] `); // search for "ba" utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'ba'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // go to second result utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // go to third result utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // go to second result utils.act(() => dispatch({type: 'GO_TO_PREVIOUS_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // go to first result utils.act(() => dispatch({type: 'GO_TO_PREVIOUS_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // wrap to last result utils.act(() => dispatch({type: 'GO_TO_PREVIOUS_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // wrap to first result utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); }); it('should jump directly to a specific search result by index', () => { const Foo = () => null; const Bar = () => null; const Baz = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); // search for "ba" (matches both elements and ) utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'ba'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // jump directly to the third result utils.act(() => dispatch({type: 'GO_TO_SEARCH_RESULT', payload: 2})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // jump directly back to the first result utils.act(() => dispatch({type: 'GO_TO_SEARCH_RESULT', payload: 0})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // out-of-range indices are clamped to the valid range utils.act(() => dispatch({type: 'GO_TO_SEARCH_RESULT', payload: 99})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => dispatch({type: 'GO_TO_SEARCH_RESULT', payload: -5})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); }); it('should do nothing when jumping to a result with no search matches', () => { const Foo = () => null; const Bar = () => null; const Baz = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'nomatch'})); utils.act(() => renderer.update()); expect(state.searchResults).toHaveLength(0); expect(state.searchIndex).toBe(null); utils.act(() => dispatch({type: 'GO_TO_SEARCH_RESULT', payload: 0})); utils.act(() => renderer.update()); expect(state.searchIndex).toBe(null); expect(state.inspectedElementID).toBe(null); expect(state).toMatchInlineSnapshot(` [root] `); }); it('should advance past the selected result when retyping the same search', () => { const Foo = () => null; const Bar = () => null; const Baz = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); // search for "ba" and step to the second result () utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'ba'})); utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // clear the search; the matched element stays selected utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: ''})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // retype the same query: instead of snapping back to the still-selected // , the search advances to the next match (find-next semantics) utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'ba'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); }); it('should add newly mounted elements to the search results set if they match the current text', async () => { const Foo = () => null; const Bar = () => null; const Baz = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'ba'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); await utils.actAsync(() => render( , ), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); }); it('should remove unmounted elements from the search results set', async () => { const Foo = () => null; const Bar = () => null; const Baz = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => dispatch({type: 'SET_SEARCH_TEXT', payload: 'ba'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); await utils.actAsync(() => render( , ), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); // Noop since the list is now one item long utils.act(() => dispatch({type: 'GO_TO_NEXT_SEARCH_RESULT'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] `); }); }); describe('owners state', () => { it('should support entering and existing the owners tree view', () => { const Grandparent = () => ; const Parent = () => ( ); const Child = () => null; utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ `); const parentID = ((store.getElementIDAtIndex(1): any): number); utils.act(() => dispatch({type: 'SELECT_OWNER', payload: parentID})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [owners] → ▾ `); utils.act(() => dispatch({type: 'RESET_OWNER_STACK'})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ `); }); it('should remove an element from the owners list if it is unmounted', async () => { const Grandparent = ({count}) => ; const Parent = ({count}) => new Array(count).fill(true).map((_, index) => ); const Child = () => null; utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ `); const parentID = ((store.getElementIDAtIndex(1): any): number); utils.act(() => dispatch({type: 'SELECT_OWNER', payload: parentID})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [owners] → ▾ `); await utils.actAsync(() => render()); expect(state).toMatchInlineSnapshot(` [owners] → ▾ `); await utils.actAsync(() => render()); expect(state).toMatchInlineSnapshot(` [owners] → `); }); it('should exit the owners list if the current owner is unmounted', async () => { const Parent = props => props.children || null; const Child = () => null; utils.act(() => render( , ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ `); const childID = ((store.getElementIDAtIndex(1): any): number); utils.act(() => dispatch({type: 'SELECT_OWNER', payload: childID})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [owners] → `); await utils.actAsync(() => render()); expect(state).toMatchInlineSnapshot(` [root] → `); const parentID = ((store.getElementIDAtIndex(0): any): number); utils.act(() => dispatch({type: 'SELECT_OWNER', payload: parentID})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [owners] → `); await utils.actAsync(() => unmount()); expect(state).toMatchInlineSnapshot(``); }); // This tests ensures support for toggling Suspense boundaries outside of the active owners list. it('should exit the owners list if an element outside the list is selected', () => { const Grandchild = () => null; const Child = () => ( ); const Parent = () => ( ); utils.act(() => render()); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` [root] ▾ [suspense-root] rects={null} `); const outerSuspenseID = ((store.getElementIDAtIndex(1): any): number); const childID = ((store.getElementIDAtIndex(2): any): number); const innerSuspenseID = ((store.getElementIDAtIndex(3): any): number); utils.act(() => dispatch({type: 'SELECT_OWNER', payload: childID})); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [owners] → ▾ `); // Toggling a Suspense boundary inside of the flat list should update selected index utils.act(() => dispatch({type: 'SELECT_ELEMENT_BY_ID', payload: innerSuspenseID}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [owners] ▾ → ▾ `); // Toggling a Suspense boundary outside of the flat list should exit owners list and update index utils.act(() => dispatch({type: 'SELECT_ELEMENT_BY_ID', payload: outerSuspenseID}), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` [root] ▾ → ▾ [suspense-root] rects={null} `); }); }); describe('inline errors/warnings state', () => { const { clearErrorsAndWarnings: clearErrorsAndWarningsAPI, clearErrorsForElement: clearErrorsForElementAPI, clearWarningsForElement: clearWarningsForElementAPI, } = require('react-devtools-shared/src/backendAPI'); function clearAllErrors() { utils.act(() => clearErrorsAndWarningsAPI({bridge, store})); // flush events to the renderer jest.runAllTimers(); } function clearErrorsForElement(id) { const rendererID = store.getRendererIDForElement(id); utils.act(() => clearErrorsForElementAPI({bridge, id, rendererID})); // flush events to the renderer jest.runAllTimers(); } function clearWarningsForElement(id) { const rendererID = store.getRendererIDForElement(id); utils.act(() => clearWarningsForElementAPI({bridge, id, rendererID})); // flush events to the renderer jest.runAllTimers(); } function selectNextErrorOrWarning() { utils.act(() => dispatch({type: 'SELECT_NEXT_ELEMENT_WITH_ERROR_OR_WARNING_IN_TREE'}), ); } function selectPreviousErrorOrWarning() { utils.act(() => dispatch({ type: 'SELECT_PREVIOUS_ELEMENT_WITH_ERROR_OR_WARNING_IN_TREE', }), ); } function Child({logError = false, logWarning = false}) { if (logError === true) { console.error('test-only: error'); } if (logWarning === true) { console.warn('test-only: warning'); } return null; } it('should handle when there are no errors/warnings', () => { utils.act(() => render( , ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` [root] `); // Next/previous errors should be a no-op selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => dispatch({type: 'SELECT_ELEMENT_AT_INDEX', payload: 0})); expect(state).toMatchInlineSnapshot(` [root] → `); // Next/previous errors should still be a no-op selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] → `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] → `); }); it('should cycle through the next errors/warnings and wrap around', () => { withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); }); it('should cycle through the previous errors/warnings and wrap around', () => { withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); }); it('should cycle through the next errors/warnings and wrap around with multiple roots', () => { withErrorsOrWarningsIgnored(['test-only:'], () => { utils.act(() => { render( , , ); createContainer(); render( , ); }); }); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); }); it('should cycle through the previous errors/warnings and wrap around with multiple roots', () => { withErrorsOrWarningsIgnored(['test-only:'], () => { utils.act(() => { render( , , ); createContainer(); render( , ); }); }); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ [root] `); }); it('should select the next or previous element relative to the current selection', () => { withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); utils.act(() => dispatch({type: 'SELECT_ELEMENT_AT_INDEX', payload: 2})); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ → `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); utils.act(() => dispatch({type: 'SELECT_ELEMENT_AT_INDEX', payload: 2})); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ⚠ → `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] `); }); it('should update correctly when errors/warnings are cleared for a fiber in the list', () => { withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 2 [root] ⚠ `); // Select the first item in the list selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 2 [root] → ⚠ `); // Clear warnings (but the next Fiber has only errors) clearWarningsForElement(store.getElementIDAtIndex(1)); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 2 [root] ⚠ → ⚠ `); clearErrorsForElement(store.getElementIDAtIndex(2)); // Should step to the (now) next one in the list. selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 2 [root] ⚠ `); // Should skip over the (now) cleared Fiber selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 2 [root] ⚠ → ⚠ `); }); it('should update correctly when errors/warnings are cleared for the currently selected fiber', () => { withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ✕ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] → ✕ `); clearWarningsForElement(store.getElementIDAtIndex(0)); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 0 [root] ✕ `); }); it('should update correctly when new errors/warnings are added', () => { withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ✕ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] → ✕ `); withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 2 [root] ⚠ → ✕ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 2 [root] ✕ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 2 [root] → ✕ `); }); it('should update correctly when all errors/warnings are cleared', () => { withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] ✕ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 1 [root] → ✕ `); clearAllErrors(); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] → `); selectPreviousErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] → `); }); it('should update correctly when elements are added/removed', () => { let errored = false; function ErrorOnce() { if (!errored) { errored = true; console.error('test-only:one-time-error'); } return null; } withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 0 [root] ✕ `); withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 0 [root] ✕ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 0 [root] ✕ `); }); it('should update correctly when elements are re-ordered', () => { function ErrorOnce() { const didErrorRef = React.useRef(false); if (!didErrorRef.current) { didErrorRef.current = true; console.error('test-only:one-time-error'); } return null; } withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); let renderer; utils.act(() => (renderer = TestRenderer.create())); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 0 [root] ✕ `); // Select a child selectNextErrorOrWarning(); utils.act(() => renderer.update()); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 0 [root] ✕ `); // Re-order the tree and ensure indices are updated. withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 0 [root] → `); // Select the next child and ensure the index doesn't break. selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 0 [root] `); // Re-order the tree and ensure indices are updated. withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); expect(state).toMatchInlineSnapshot(` ✕ 2, ⚠ 0 [root] → `); }); it('should update select and auto-expand parts components within hidden parts of the tree', () => { const Wrapper = ({children}) => children; store.collapseNodesByDefault = true; withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 0, ⚠ 2 [root] ▸ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 0, ⚠ 2 [root] ▾ ⚠ ▸ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 0, ⚠ 2 [root] ▾ ⚠ ▾ ⚠ `); }); it('should preserve errors for fibers even if they are filtered out of the tree initially', () => { const Wrapper = ({children}) => children; withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); store.componentFilters = [utils.createDisplayNameFilter('Child')]; utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => { store.componentFilters = []; }); expect(state).toMatchInlineSnapshot(` ✕ 0, ⚠ 2 [root] ▾ ⚠ ▾ ⚠ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 0, ⚠ 2 [root] ▾ ⚠ ▾ ⚠ `); }); describe('suspense', () => { // This verifies that we don't flush before the tree has been committed. it('should properly handle errors/warnings from components inside of delayed Suspense', async () => { const NeverResolves = React.lazy(() => new Promise(() => {})); withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); jest.runAllTimers(); expect(state).toMatchInlineSnapshot(` [root] [suspense-root] rects={null} `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] [suspense-root] rects={null} `); }); it('should properly handle errors/warnings from components that dont mount because of Suspense', async () => { async function fakeImport(result) { return {default: result}; } const LazyComponent = React.lazy(() => fakeImport(Child)); withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` [root] [suspense-root] rects={null} `); await Promise.resolve(); withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( , ), ), ); expect(state).toMatchInlineSnapshot(` ✕ 0, ⚠ 1 [root] ▾ [suspense-root] rects={null} `); }); it('should properly show errors/warnings from components in the Suspense fallback tree', async () => { async function fakeImport(result) { return {default: result}; } const LazyComponent = React.lazy(() => fakeImport(Child)); const Fallback = () => ; withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( }> , ), ), ); utils.act(() => TestRenderer.create()); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 0 [root] ▾ ✕ [suspense-root] rects={null} `); await Promise.resolve(); withErrorsOrWarningsIgnored(['test-only:'], () => utils.act(() => render( }> , ), ), ); expect(state).toMatchInlineSnapshot(` [root] ▾ [suspense-root] rects={null} `); }); }); describe('error boundaries', () => { it('should properly handle errors from components that dont mount because of an error', () => { class ErrorBoundary extends React.Component { state = {error: null}; static getDerivedStateFromError(error) { return {error}; } render() { if (this.state.error) { return null; } return this.props.children; } } class BadRender extends React.Component { render() { console.error('test-only: I am about to throw!'); throw new Error('test-only: Oops!'); } } withErrorsOrWarningsIgnored( ['test-only:', 'React will try to recreate this component tree'], () => { utils.act(() => render( , ), ); }, ); utils.act(() => TestRenderer.create()); expect(store).toMatchInlineSnapshot(` [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => unmount()); expect(state).toMatchInlineSnapshot(``); // Should be a noop selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(``); }); it('should properly handle warnings from components that dont mount because of an error', () => { class ErrorBoundary extends React.Component { state = {error: null}; static getDerivedStateFromError(error) { return {error}; } render() { if (this.state.error) { return null; } return this.props.children; } } class LogsWarning extends React.Component { render() { console.warn('test-only: I am about to throw!'); return ; } } class ThrowsError extends React.Component { render() { throw new Error('test-only: Oops!'); } } withErrorsOrWarningsIgnored( ['test-only:', 'React will try to recreate this component tree'], () => { utils.act(() => render( , ), ); }, ); utils.act(() => TestRenderer.create()); expect(store).toMatchInlineSnapshot(` [root] `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` [root] `); utils.act(() => unmount()); expect(state).toMatchInlineSnapshot(``); // Should be a noop selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(``); }); it('should properly handle errors/warnings from inside of an error boundary', () => { class ErrorBoundary extends React.Component { state = {error: null}; static getDerivedStateFromError(error) { return {error}; } render() { if (this.state.error) { return ; } return this.props.children; } } class BadRender extends React.Component { render() { console.error('test-only: I am about to throw!'); throw new Error('test-only: Oops!'); } } withErrorsOrWarningsIgnored( ['test-only:', 'React will try to recreate this component tree'], () => { utils.act(() => render( , ), ); }, ); utils.act(() => TestRenderer.create()); expect(store).toMatchInlineSnapshot(` ✕ 1, ⚠ 0 [root] ▾ ✕ `); selectNextErrorOrWarning(); expect(state).toMatchInlineSnapshot(` ✕ 1, ⚠ 0 [root] ▾ ✕ `); }); }); }); });