Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/react-devtools-shared/src/__tests__/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ function shouldIgnoreConsoleErrorOrWarn(args) {
return false;
}

const maybeError = args[1];
if (
maybeError !== null &&
typeof maybeError === 'object' &&
maybeError.message === 'Simulated error coming from DevTools'
) {
// Error from forcing an error boundary.
return true;
}

return global._ignoredErrorOrWarningMessages.some(errorOrWarningMessage => {
return firstArg.indexOf(errorOrWarningMessage) !== -1;
});
Expand Down
2 changes: 0 additions & 2 deletions packages/react-devtools-shared/src/__tests__/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3283,8 +3283,6 @@ describe('Store', () => {
<Suspense name="Outer" rects={null}>
`);

console.log('...........................');

await actAsync(() => {
resolve('loaded');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@ import {
describe('Store component filters', () => {
let React;
let Types;
let agent;
let bridge: FrontendBridge;
let store: Store;
let utils;
let actAsync;

beforeAll(() => {
// JSDDOM doesn't implement getClientRects so we're just faking one for testing purposes
Element.prototype.getClientRects = function (this: Element) {
const textContent = this.textContent;
return [
new DOMRect(1, 2, textContent.length, textContent.split('\n').length),
];
};
});

beforeEach(() => {
agent = global.agent;
bridge = global.bridge;
store = global.store;
store.collapseNodesByDefault = false;
Expand Down Expand Up @@ -156,9 +168,9 @@ describe('Store component filters', () => {
<div>
▾ <Suspense>
<div>
[suspense-root] rects={[]}
<Suspense name="Unknown" rects={[]}>
<Suspense name="Unknown" rects={[]}>
[suspense-root] rects={[{x:1,y:2,width:7,height:1}, {x:1,y:2,width:6,height:1}]}
<Suspense name="Unknown" rects={[{x:1,y:2,width:7,height:1}]}>
<Suspense name="Unknown" rects={[{x:1,y:2,width:6,height:1}]}>
`);

await actAsync(
Expand All @@ -174,9 +186,9 @@ describe('Store component filters', () => {
<div>
▾ <Suspense>
<div>
[suspense-root] rects={[]}
<Suspense name="Unknown" rects={[]}>
<Suspense name="Unknown" rects={[]}>
[suspense-root] rects={[{x:1,y:2,width:7,height:1}, {x:1,y:2,width:6,height:1}]}
<Suspense name="Unknown" rects={[{x:1,y:2,width:7,height:1}]}>
<Suspense name="Unknown" rects={[{x:1,y:2,width:6,height:1}]}>
`);

await actAsync(
Expand All @@ -192,9 +204,9 @@ describe('Store component filters', () => {
<div>
▾ <Suspense>
<div>
[suspense-root] rects={[]}
<Suspense name="Unknown" rects={[]}>
<Suspense name="Unknown" rects={[]}>
[suspense-root] rects={[{x:1,y:2,width:7,height:1}, {x:1,y:2,width:6,height:1}]}
<Suspense name="Unknown" rects={[{x:1,y:2,width:7,height:1}]}>
<Suspense name="Unknown" rects={[{x:1,y:2,width:6,height:1}]}>
`);
});

Expand Down Expand Up @@ -740,4 +752,179 @@ describe('Store component filters', () => {
`);
});
});

// @reactVersion >= 16.6
it('resets forced error and fallback states when filters are changed', async () => {
store.componentFilters = [];
class ErrorBoundary extends React.Component {
state = {hasError: false};

static getDerivedStateFromError() {
return {hasError: true};
}

render() {
if (this.state.hasError) {
return <div key="did-error" />;
}
return this.props.children;
}
}

function App() {
return (
<>
<React.Suspense fallback={<div key="loading" />}>
<div key="suspense-content" />
</React.Suspense>
<ErrorBoundary>
<div key="error-content" />
</ErrorBoundary>
</>
);
}

await actAsync(async () => {
render(<App />);
});
const rendererID = utils.getRendererID();
await actAsync(() => {
agent.overrideSuspense({
id: store.getElementIDAtIndex(2),
rendererID,
forceFallback: true,
});
agent.overrideError({
id: store.getElementIDAtIndex(4),
rendererID,
forceError: true,
});
});

expect(store).toMatchInlineSnapshot(`
[root]
▾ <App>
▾ <Suspense>
<div key="loading">
▾ <ErrorBoundary>
<div key="did-error">
[suspense-root] rects={[{x:1,y:2,width:0,height:1}, {x:1,y:2,width:0,height:1}, {x:1,y:2,width:0,height:1}]}
<Suspense name="App" rects={[{x:1,y:2,width:0,height:1}]}>
`);

await actAsync(() => {
store.componentFilters = [
utils.createElementTypeFilter(Types.ElementTypeFunction, true),
];
});

expect(store).toMatchInlineSnapshot(`
[root]
▾ <Suspense>
<div key="suspense-content">
▾ <ErrorBoundary>
<div key="error-content">
[suspense-root] rects={[{x:1,y:2,width:0,height:1}, {x:1,y:2,width:0,height:1}]}
<Suspense name="Unknown" rects={[{x:1,y:2,width:0,height:1}]}>
`);
});

// @reactVersion >= 19.2
it('can filter by Activity slices', async () => {
const Activity = React.Activity;
const immediate = Promise.resolve(<div>Immediate</div>);

function Root({children}) {
return (
<Activity name="/" mode="visible">
<React.Suspense fallback="Loading...">
<h1>Root</h1>
<main>{children}</main>
</React.Suspense>
</Activity>
);
}

function Layout({children}) {
return (
<Activity name="/blog" mode="visible">
<h2>Blog</h2>
<section>{children}</section>
</Activity>
);
}

function Page() {
return <React.Suspense fallback="Loading...">{immediate}</React.Suspense>;
}

await actAsync(async () =>
render(
<Root>
<Layout>
<Page />
</Layout>
</Root>,
),
);

expect(store).toMatchInlineSnapshot(`
[root]
▾ <Root>
▾ <Activity name="/">
▾ <Suspense>
<h1>
▾ <main>
▾ <Layout>
▾ <Activity name="/blog">
<h2>
▾ <section>
▾ <Page>
▾ <Suspense>
<div>
[suspense-root] rects={[{x:1,y:2,width:4,height:1}, {x:1,y:2,width:13,height:1}]}
<Suspense name="Root" rects={[{x:1,y:2,width:4,height:1}, {x:1,y:2,width:13,height:1}]}>
<Suspense name="Page" rects={[{x:1,y:2,width:9,height:1}]}>
`);

await actAsync(
async () =>
(store.componentFilters = [
utils.createActivitySliceFilter(store.getElementIDAtIndex(1)),
]),
);

expect(store).toMatchInlineSnapshot(`
[root]
▾ <Activity name="/">
▾ <Suspense>
<h1>
▾ <main>
▾ <Layout>
<Activity name="/blog">
[suspense-root] rects={[{x:1,y:2,width:4,height:1}, {x:1,y:2,width:13,height:1}]}
<Suspense name="Unknown" rects={[{x:1,y:2,width:4,height:1}, {x:1,y:2,width:13,height:1}]}>
Copy link
Collaborator Author

@eps1lon eps1lon Oct 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name heuristic breaks once the Owner is filtered. Need to think about how to fix this. Only problematic when the Activity and Suspense share the same owner.

`);

await actAsync(async () => (store.componentFilters = []));

expect(store).toMatchInlineSnapshot(`
[root]
▾ <Root>
▾ <Activity name="/">
▾ <Suspense>
<h1>
▾ <main>
▾ <Layout>
▾ <Activity name="/blog">
<h2>
▾ <section>
▾ <Page>
▾ <Suspense>
<div>
[suspense-root] rects={[{x:1,y:2,width:4,height:1}, {x:1,y:2,width:13,height:1}]}
<Suspense name="Root" rects={[{x:1,y:2,width:4,height:1}, {x:1,y:2,width:13,height:1}]}>
<Suspense name="Page" rects={[{x:1,y:2,width:9,height:1}]}>
`);
});
});
13 changes: 13 additions & 0 deletions packages/react-devtools-shared/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@ export function createLocationFilter(
};
}

export function createActivitySliceFilter(
activityID: Element['id'],
isEnabled: boolean = true,
) {
const Types = require('react-devtools-shared/src/frontend/types');
return {
type: Types.ComponentFilterActivitySlice,
isEnabled,
isValid: true,
activityID: activityID,
};
}

export function getRendererID(): number {
if (global.agent == null) {
throw Error('Agent unavailable.');
Expand Down
Loading
Loading