-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Discover][Metrics] Persist flyout state across navigation #251637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
lucaslopezf
wants to merge
21
commits into
elastic:main
from
lucaslopezf:235834-persist-view-flyout-details-across-navigation
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b00d860
Discover(metrics): Persist flyout state across navigation
lucaslopezf 989f1bb
Add isTabSelected to ChartSectionProps
lucaslopezf f734283
minor fix
lucaslopezf 9eb3a15
Changes from node scripts/eslint_all_files --no-cache --fix
kibanamachine 140e6a3
linting issues
lucaslopezf a4ef03d
Ignore currentPage restorable state and enhance code
lucaslopezf 9143c27
linting issues
lucaslopezf d7e74b8
clean up
lucaslopezf dfaad75
linting issues
lucaslopezf 1aa94ef
linting issues
lucaslopezf e1c72fe
rename metricData constant
lucaslopezf 62f92d0
clean up flyoutstate to avoid inconsistencies
lucaslopezf 495667d
Add ownFocus=false
lucaslopezf 4f05c62
react-reverse-portal workaround
lucaslopezf 8caee65
Wrap MetricFlyout to avoid react-reverse-portal error
lucaslopezf 5bc1efd
set ownFocus default behavior
lucaslopezf 0cbea55
Add ownFocus workaround
lucaslopezf 46d0c48
Merge branch 'main' into 235834-persist-view-flyout-details-across-na…
lucaslopezf cb6e7b3
linting issues
lucaslopezf f5217f9
Merge branch 'main' into 235834-persist-view-flyout-details-across-na…
lucaslopezf b384fee
Merge branch 'main' into 235834-persist-view-flyout-details-across-na…
lucaslopezf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...hared/kbn-unified-chart-section-viewer/src/components/flyout/metrics_flyout_body.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import '@testing-library/jest-dom'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { ES_FIELD_TYPES } from '@kbn/field-types'; | ||
| import type { MetricField } from '../../types'; | ||
| import { MetricFlyoutBody } from './metrics_flyout_body'; | ||
| import { useMetricsExperienceState } from '../observability/metrics/context/metrics_experience_state_provider'; | ||
|
|
||
| jest.mock('../observability/metrics/context/metrics_experience_state_provider', () => ({ | ||
| useMetricsExperienceState: jest.fn(), | ||
| })); | ||
|
|
||
| const mockUseMetricsExperienceState = useMetricsExperienceState as jest.Mock; | ||
|
|
||
| describe('MetricFlyoutBody', () => { | ||
| const createMockMetric = (overrides: Partial<MetricField> = {}): MetricField => ({ | ||
| name: 'system.cpu.user.pct', | ||
| index: 'metrics-*', | ||
| type: ES_FIELD_TYPES.FLOAT, | ||
| uniqueKey: 'metrics-*::system.cpu.user.pct', | ||
| dimensions: [], | ||
| ...overrides, | ||
| }); | ||
|
|
||
| const mockOnFlyoutTabChange = jest.fn(); | ||
|
|
||
| const createDefaultFlyoutState = (overrides = {}) => ({ | ||
| gridPosition: 0, | ||
| metricUniqueKey: 'metrics-*::system.cpu.user.pct', | ||
| esqlQuery: 'FROM metrics-*', | ||
| selectedTabId: undefined, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockUseMetricsExperienceState.mockReturnValue({ | ||
| flyoutState: createDefaultFlyoutState(), | ||
| onFlyoutTabChange: mockOnFlyoutTabChange, | ||
| }); | ||
| }); | ||
|
|
||
| it('renders both tabs', () => { | ||
| const metric = createMockMetric(); | ||
| render(<MetricFlyoutBody metric={metric} esqlQuery="FROM metrics-*" />); | ||
|
|
||
| expect(screen.getByRole('tab', { name: 'Overview' })).toBeInTheDocument(); | ||
| expect(screen.getByRole('tab', { name: 'ES|QL Query' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('defaults to Overview tab when selectedTabId is undefined', () => { | ||
| const metric = createMockMetric(); | ||
| render(<MetricFlyoutBody metric={metric} esqlQuery="FROM metrics-*" />); | ||
|
|
||
| const overviewTab = screen.getByRole('tab', { name: 'Overview' }); | ||
| const esqlTab = screen.getByRole('tab', { name: 'ES|QL Query' }); | ||
|
|
||
| expect(overviewTab).toHaveAttribute('aria-selected', 'true'); | ||
| expect(esqlTab).toHaveAttribute('aria-selected', 'false'); | ||
| }); | ||
|
|
||
| it('selects the tab from flyoutState.selectedTabId', () => { | ||
| mockUseMetricsExperienceState.mockReturnValue({ | ||
| flyoutState: createDefaultFlyoutState({ selectedTabId: 'esql-query' }), | ||
| onFlyoutTabChange: mockOnFlyoutTabChange, | ||
| }); | ||
|
|
||
| const metric = createMockMetric(); | ||
| render(<MetricFlyoutBody metric={metric} esqlQuery="FROM metrics-*" />); | ||
|
|
||
| const overviewTab = screen.getByRole('tab', { name: 'Overview' }); | ||
| const esqlTab = screen.getByRole('tab', { name: 'ES|QL Query' }); | ||
|
|
||
| expect(overviewTab).toHaveAttribute('aria-selected', 'false'); | ||
| expect(esqlTab).toHaveAttribute('aria-selected', 'true'); | ||
| }); | ||
|
|
||
| it('calls onFlyoutTabChange when clicking a tab', async () => { | ||
| const user = userEvent.setup(); | ||
| const metric = createMockMetric(); | ||
|
|
||
| render(<MetricFlyoutBody metric={metric} esqlQuery="FROM metrics-*" />); | ||
|
|
||
| const esqlTab = screen.getByRole('tab', { name: 'ES|QL Query' }); | ||
| await user.click(esqlTab); | ||
|
|
||
| expect(mockOnFlyoutTabChange).toHaveBeenCalledWith('esql-query'); | ||
| }); | ||
|
|
||
| it('calls onFlyoutTabChange with overview when clicking Overview tab', async () => { | ||
| const user = userEvent.setup(); | ||
|
|
||
| mockUseMetricsExperienceState.mockReturnValue({ | ||
| flyoutState: createDefaultFlyoutState({ selectedTabId: 'esql-query' }), | ||
| onFlyoutTabChange: mockOnFlyoutTabChange, | ||
| }); | ||
|
|
||
| const metric = createMockMetric(); | ||
| render(<MetricFlyoutBody metric={metric} esqlQuery="FROM metrics-*" />); | ||
|
|
||
| const overviewTab = screen.getByRole('tab', { name: 'Overview' }); | ||
| await user.click(overviewTab); | ||
|
|
||
| expect(mockOnFlyoutTabChange).toHaveBeenCalledWith('overview'); | ||
| }); | ||
|
|
||
| it('handles undefined flyoutState gracefully', () => { | ||
| mockUseMetricsExperienceState.mockReturnValue({ | ||
| flyoutState: undefined, | ||
| onFlyoutTabChange: mockOnFlyoutTabChange, | ||
| }); | ||
|
|
||
| const metric = createMockMetric(); | ||
| render(<MetricFlyoutBody metric={metric} esqlQuery="FROM metrics-*" />); | ||
|
|
||
| const overviewTab = screen.getByRole('tab', { name: 'Overview' }); | ||
| expect(overviewTab).toHaveAttribute('aria-selected', 'true'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, using the index is fine, but we should be aware that user-defined strings could become quite long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion Kate!
QQ, what would be the valid unique combinations? I know index + metricName is one, but do we have another/s one? Because once
metrics_infois ready, if we get more info we could improve it