From a024d8e26a4faf266f428859de165ad86649cd07 Mon Sep 17 00:00:00 2001 From: Carlo <1778532+cdcme@users.noreply.github.com> Date: Fri, 19 Jun 2026 14:37:06 -0400 Subject: [PATCH] Fix policy and report pages showing previously viewed content (#47767) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Related issue:** Resolves #43310 Read-only policy and report detail pages rendered their displayed fields (name, description, resolution, platforms, query) from the editing context (`PolicyContext`/`QueryContext`), which only updates in react-query's `onSuccess`. On a cached revisit, `isLoading` is `false` (no spinner) and the freshly-loaded entity is available immediately, but the context still held the previously-viewed entity's values for a frame — briefly showing the wrong policy/report. This change makes both detail pages render directly from the fresh `useQuery` result (`storedPolicy`/`storedQuery`) and drops their coupling to the editing context, matching the existing `HostDetailsPage`/`SoftwareTitleDetailsPage` pattern. `QueryDetailsPage` (Reports) had the identical latent bug and is fixed here too. # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`. ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually New regression tests (`PolicyDetailsPage.tests.tsx`, `QueryDetailsPage.tests.tsx`) seed the context with stale values and assert the page renders the freshly-loaded entity instead. Verified live against a running Fleet instance with Playwright: navigating between cached detail pages no longer flashes the previous entity's content. [6f7d6d22134dc1fd968f8473c552ee49.webm](https://github.com/user-attachments/assets/c8ef5f3f-278c-4e13-adc1-689b3eff4e59) ## Summary by CodeRabbit ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Resolved an issue where policy and query details pages could briefly display previously viewed policy/report information when switching between items. * **Tests** * Added regression coverage to ensure the UI renders freshly loaded policy/query name, description, and query/report details, and does not show stale values. --- changes/43310-policy-report-details-flash | 1 + .../PolicyDetailsPage.tests.tsx | 138 +++++++++++++++++- .../PolicyDetailsPage/PolicyDetailsPage.tsx | 60 +------- .../QueryDetailsPage.tests.tsx | 136 +++++++++++++++++ .../QueryDetailsPage/QueryDetailsPage.tsx | 56 ++----- .../components/QueryReport/QueryReport.tsx | 9 +- 6 files changed, 298 insertions(+), 102 deletions(-) create mode 100644 changes/43310-policy-report-details-flash diff --git a/changes/43310-policy-report-details-flash b/changes/43310-policy-report-details-flash new file mode 100644 index 0000000000..3c687ed9c1 --- /dev/null +++ b/changes/43310-policy-report-details-flash @@ -0,0 +1 @@ +- Fixed the policy and report details pages briefly showing the previously-viewed policy/report's content when navigating between them. diff --git a/frontend/pages/policies/details/PolicyDetailsPage/PolicyDetailsPage.tests.tsx b/frontend/pages/policies/details/PolicyDetailsPage/PolicyDetailsPage.tests.tsx index 514d1a62e7..ef51a3deb9 100644 --- a/frontend/pages/policies/details/PolicyDetailsPage/PolicyDetailsPage.tests.tsx +++ b/frontend/pages/policies/details/PolicyDetailsPage/PolicyDetailsPage.tests.tsx @@ -1,13 +1,51 @@ +import React from "react"; +import { screen } from "@testing-library/react"; +import { http, HttpResponse } from "msw"; + import { IPolicy } from "interfaces/policy"; import { ILabelPolicy } from "interfaces/label"; +import { + createCustomRenderer, + baseUrl, + createMockRouter, +} from "test/test-utils"; +import mockServer from "test/mock-server"; +import createMockUser from "__mocks__/userMock"; +import createMockConfig from "__mocks__/configMock"; -import { getLabelModalData } from "./PolicyDetailsPage"; +import PolicyDetailsPage, { getLabelModalData } from "./PolicyDetailsPage"; // Stub SoftwareIcon to avoid asset resolution when importing the page module. jest.mock("pages/SoftwarePage/components/icons/SoftwareIcon", () => { return () => null; }); +// Avoid depending on react-router's browserHistory inside BackButton. +jest.mock("components/BackButton", () => ({ + __esModule: true, + default: ({ text }: { text: string }) => ( + + ), +})); + +// Surface the modal's `query` prop as plain text (the real modal renders it in +// an Ace editor that isn't reliably assertable in jsdom). +jest.mock("components/modals/ShowQueryModal", () => ({ + __esModule: true, + default: ({ query }: { query?: string }) => ( +