-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(quick-filters): improve filter visibility and auto-open behavior #9253
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
Open
ahmadshaheer
wants to merge
8
commits into
main
Choose a base branch
from
feat/quick-filters-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a62aae2
feat(quick-filters): improve filter visibility and auto-open behavior
ahmadshaheer bd24a37
chore: remove the unnecessary parentheses
ahmadshaheer 75f280b
chore: write tests
ahmadshaheer 9a2fe42
chore: overall improvements
ahmadshaheer cecb810
chore: remove the applied filters count from quick filters
ahmadshaheer 9d188ff
chore: run prettier on Checkbox.styles.scss
ahmadshaheer 6ade32b
test(quick-filters): consolidate the tests
ahmadshaheer e00625b
chore: memoize isSomeFilterPresentForCurrentAttribute
ahmadshaheer 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
216 changes: 216 additions & 0 deletions
216
frontend/src/components/QuickFilters/FilterRenderers/Checkbox/Checkbox.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,216 @@ | ||
import { FiltersType, QuickFiltersSource } from 'components/QuickFilters/types'; | ||
import { useGetAggregateValues } from 'hooks/queryBuilder/useGetAggregateValues'; | ||
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder'; | ||
import { useGetQueryKeyValueSuggestions } from 'hooks/querySuggestions/useGetQueryKeyValueSuggestions'; | ||
import { quickFiltersAttributeValuesResponse } from 'mocks-server/__mockdata__/customQuickFilters'; | ||
import { rest, server } from 'mocks-server/server'; | ||
import { UseQueryResult } from 'react-query'; | ||
import { render, screen, userEvent, waitFor } from 'tests/test-utils'; | ||
import { SuccessResponse } from 'types/api'; | ||
import { IAttributeValuesResponse } from 'types/api/queryBuilder/getAttributesValues'; | ||
import { DataTypes } from 'types/api/queryBuilder/queryAutocompleteResponse'; | ||
import { DataSource } from 'types/common/queryBuilder'; | ||
|
||
import CheckboxFilter from './Checkbox'; | ||
|
||
// Mock the query builder hook | ||
jest.mock('hooks/queryBuilder/useQueryBuilder'); | ||
const mockUseQueryBuilder = jest.mocked(useQueryBuilder); | ||
|
||
// Mock the aggregate values hook | ||
jest.mock('hooks/queryBuilder/useGetAggregateValues'); | ||
|
||
const mockUseGetAggregateValues = jest.mocked(useGetAggregateValues); | ||
|
||
// Mock the key value suggestions hook | ||
jest.mock('hooks/querySuggestions/useGetQueryKeyValueSuggestions'); | ||
|
||
const mockUseGetQueryKeyValueSuggestions = jest.mocked( | ||
useGetQueryKeyValueSuggestions, | ||
); | ||
|
||
interface MockFilterConfig { | ||
title: string; | ||
attributeKey: { | ||
key: string; | ||
dataType: DataTypes; | ||
type: string; | ||
}; | ||
dataSource: DataSource; | ||
defaultOpen: boolean; | ||
type: FiltersType; | ||
} | ||
|
||
const createMockFilter = ( | ||
overrides: Partial<MockFilterConfig> = {}, | ||
): MockFilterConfig => ({ | ||
// eslint-disable-next-line sonarjs/no-duplicate-string | ||
title: 'Service Name', | ||
attributeKey: { | ||
key: 'service.name', | ||
dataType: DataTypes.String, | ||
type: 'resource', | ||
}, | ||
dataSource: DataSource.LOGS, | ||
defaultOpen: false, | ||
type: FiltersType.CHECKBOX, | ||
...overrides, | ||
}); | ||
|
||
const createMockQueryBuilderData = (hasActiveFilters = false): any => ({ | ||
lastUsedQuery: 0, | ||
currentQuery: { | ||
builder: { | ||
queryData: [ | ||
{ | ||
filters: { | ||
items: hasActiveFilters | ||
? [ | ||
{ | ||
key: { | ||
key: 'service.name', | ||
dataType: DataTypes.String, | ||
type: 'resource', | ||
}, | ||
op: 'in', | ||
value: ['otel-demo', 'sample-flask'], | ||
}, | ||
] | ||
: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
redirectWithQueryBuilderData: jest.fn(), | ||
}); | ||
|
||
describe('CheckboxFilter - User Flows', () => { | ||
beforeEach(() => { | ||
// Reset all mocks | ||
jest.clearAllMocks(); | ||
|
||
// Default mock implementations using the same structure as existing tests | ||
mockUseGetAggregateValues.mockReturnValue({ | ||
data: { | ||
payload: { | ||
stringAttributeValues: [ | ||
'mq-kafka', | ||
'otel-demo', | ||
'otlp-python', | ||
'sample-flask', | ||
], | ||
}, | ||
}, | ||
isLoading: false, | ||
} as UseQueryResult<SuccessResponse<IAttributeValuesResponse>>); | ||
|
||
mockUseGetQueryKeyValueSuggestions.mockReturnValue({ | ||
data: null, | ||
isLoading: false, | ||
} as any); | ||
|
||
// Setup MSW server for API calls | ||
server.use( | ||
rest.get('*/api/v3/autocomplete/attribute_values', (_req, res, ctx) => | ||
res(ctx.status(200), ctx.json(quickFiltersAttributeValuesResponse)), | ||
), | ||
); | ||
}); | ||
|
||
it('should auto-open filter and prioritize checked items when user opens page with active filters', async () => { | ||
// Mock query builder with active filters | ||
mockUseQueryBuilder.mockReturnValue(createMockQueryBuilderData(true) as any); | ||
|
||
const mockFilter = createMockFilter({ defaultOpen: false }); | ||
|
||
render( | ||
<CheckboxFilter | ||
filter={mockFilter} | ||
source={QuickFiltersSource.LOGS_EXPLORER} | ||
/>, | ||
); | ||
|
||
// User should see the filter is automatically opened (not collapsed) | ||
expect(screen.getByText('Service Name')).toBeInTheDocument(); | ||
await waitFor(() => { | ||
// eslint-disable-next-line sonarjs/no-duplicate-string | ||
expect(screen.getByPlaceholderText('Filter values')).toBeInTheDocument(); | ||
}); | ||
|
||
// User should see checked items at the top | ||
await waitFor(() => { | ||
const checkboxes = screen.getAllByRole('checkbox'); | ||
expect(checkboxes[0]).toBeChecked(); // otel-demo should be first and checked | ||
expect(checkboxes[1]).toBeChecked(); // sample-flask should be second and checked | ||
expect(checkboxes[2]).not.toBeChecked(); // mq-kafka should be unchecked | ||
expect(checkboxes[3]).not.toBeChecked(); // otlp-python should be unchecked | ||
}); | ||
}); | ||
|
||
it('should show visual separator when user expands filter with mixed checked/unchecked items', async () => { | ||
// Mock query builder with active filters | ||
mockUseQueryBuilder.mockReturnValue(createMockQueryBuilderData(true) as any); | ||
|
||
const mockFilter = createMockFilter({ defaultOpen: false }); | ||
|
||
render( | ||
<CheckboxFilter | ||
filter={mockFilter} | ||
source={QuickFiltersSource.LOGS_EXPLORER} | ||
/>, | ||
); | ||
|
||
// Wait for filter to load | ||
await waitFor(() => { | ||
expect(screen.getByPlaceholderText('Filter values')).toBeInTheDocument(); | ||
}); | ||
|
||
// User should see visual separator between checked and unchecked items | ||
expect(screen.getByTestId('filter-separator')).toBeInTheDocument(); | ||
|
||
// User should see checked items above separator, unchecked below | ||
const checkboxes = screen.getAllByRole('checkbox'); | ||
expect(checkboxes[0]).toBeChecked(); // otel-demo - checked, above separator | ||
expect(checkboxes[1]).toBeChecked(); // sample-flask - checked, above separator | ||
expect(checkboxes[2]).not.toBeChecked(); // mq-kafka - unchecked, below separator | ||
expect(checkboxes[3]).not.toBeChecked(); // otlp-python - unchecked, below separator | ||
}); | ||
|
||
it('should respect user preference when user manually toggles filter over auto-open behavior', async () => { | ||
const user = userEvent.setup({ pointerEventsCheck: 0 }); | ||
|
||
// Mock query builder with active filters | ||
mockUseQueryBuilder.mockReturnValue(createMockQueryBuilderData(true) as any); | ||
|
||
const mockFilter = createMockFilter({ defaultOpen: false }); | ||
|
||
render( | ||
<CheckboxFilter | ||
filter={mockFilter} | ||
source={QuickFiltersSource.LOGS_EXPLORER} | ||
/>, | ||
); | ||
|
||
// Initially auto-opened due to active filters | ||
await waitFor(() => { | ||
expect(screen.getByPlaceholderText('Filter values')).toBeInTheDocument(); | ||
}); | ||
|
||
// User manually closes the filter | ||
await user.click(screen.getByText('Service Name')); | ||
|
||
// User should see filter is now closed (respecting user preference) | ||
expect( | ||
screen.queryByPlaceholderText('Filter values'), | ||
).not.toBeInTheDocument(); | ||
|
||
// User manually opens the filter again | ||
await user.click(screen.getByText('Service Name')); | ||
|
||
// User should see filter is now open (respecting user preference) | ||
await waitFor(() => { | ||
expect(screen.getByPlaceholderText('Filter values')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.