diff --git a/frontend/__mocks__/queryMock.ts b/frontend/__mocks__/queryMock.ts index d1cdc4d849..40b205161d 100644 --- a/frontend/__mocks__/queryMock.ts +++ b/frontend/__mocks__/queryMock.ts @@ -1,4 +1,5 @@ import { ISchedulableQuery } from "interfaces/schedulable_query"; +import { IQueryStats } from "interfaces/query_stats"; const DEFAULT_QUERY_MOCK: ISchedulableQuery = { created_at: "2022-11-03T17:22:14Z", @@ -36,4 +37,31 @@ const createMockQuery = ( return { ...DEFAULT_QUERY_MOCK, ...overrides }; }; +const DEFAULT_QUERY_STATS_MOCK: IQueryStats = { + scheduled_query_name: "test-query", + scheduled_query_id: 1, + query_name: "Test Query", + discard_data: false, + last_fetched: "2025-01-01T00:00:00Z", + automations_enabled: false, + description: "A test query", + pack_name: "test-pack", + pack_id: 1, + average_memory: 100, + denylisted: false, + executions: 10, + interval: 3600, + last_executed: "2025-01-01T00:00:00Z", + output_size: 1024, + system_time: 50, + user_time: 100, + wall_time: 150, +}; + +export const createMockQueryStats = ( + overrides?: Partial +): IQueryStats => { + return { ...DEFAULT_QUERY_STATS_MOCK, ...overrides }; +}; + export default createMockQuery; diff --git a/frontend/pages/hosts/details/HostDetailsPage/HostDetailsPage.tsx b/frontend/pages/hosts/details/HostDetailsPage/HostDetailsPage.tsx index 59b71f16d1..4520766885 100644 --- a/frontend/pages/hosts/details/HostDetailsPage/HostDetailsPage.tsx +++ b/frontend/pages/hosts/details/HostDetailsPage/HostDetailsPage.tsx @@ -65,6 +65,7 @@ import { isIPadOrIPhone, isLinuxLike, isWindows, + isChrome, } from "interfaces/platform"; import Spinner from "components/Spinner"; @@ -1099,6 +1100,10 @@ const HostDetailsPage = ({ const isIosOrIpadosHost = isIPadOrIPhone(host.platform); const isAndroidHost = isAndroid(host.platform); const isWindowsHost = isWindows(host.platform); + const isChromeHost = isChrome(host.platform); + + const isSupportedHostQueriesPlatform = + !isIosOrIpadosHost && !isAndroidHost && !isChromeHost; const canResendProfiles = (isMacOSHost || isWindowsHost) && @@ -1311,7 +1316,10 @@ const HostDetailsPage = ({ queryReportsDisabled={ config?.server_settings?.query_reports_disabled } - canAddQuery={isAnyMaintainerAdminObserverPlus} + canAddQuery={ + isAnyMaintainerAdminObserverPlus && + isSupportedHostQueriesPlatform + } onClickAddQuery={onClickAddQuery} /> { + it("renders the queries table and add query button for supported platform with queries", () => { + const schedule = [ + createMockQueryStats({ query_name: "Query 1", scheduled_query_id: 1 }), + createMockQueryStats({ query_name: "Query 2", scheduled_query_id: 2 }), + ]; + + render( + + ); + + expect(screen.getByText("Queries")).toBeInTheDocument(); + expect(screen.getByText("Add query")).toBeInTheDocument(); + // Use getAllByText due to tooltip duplicates + expect(screen.getAllByText("Query 1").length).toBeGreaterThan(0); + expect(screen.getAllByText("Query 2").length).toBeGreaterThan(0); + }); + + it("renders 'Queries not supported for this host' message and hides add query button for unsupported host platform", () => { + render( + + ); + + expect(screen.getByText("Queries")).toBeInTheDocument(); + expect( + screen.getByText("Queries not supported for this host") + ).toBeInTheDocument(); + expect( + screen.getByText(/Interested in collecting data from your Chromebooks/) + ).toBeInTheDocument(); + expect(screen.queryByText("Add query")).not.toBeInTheDocument(); + }); + + it("renders empty state and add query button for supported platform with no queries", () => { + render( + + ); + + expect(screen.getByText("Queries")).toBeInTheDocument(); + expect(screen.getByText("Add query")).toBeInTheDocument(); + expect(screen.getByText("No queries")).toBeInTheDocument(); + expect( + screen.getByText("Add a query to view custom vitals.") + ).toBeInTheDocument(); + }); +});