Skip to content
Merged
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
52 changes: 43 additions & 9 deletions static/app/components/searchQueryBuilder/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -980,31 +980,43 @@ describe('SearchQueryBuilder', () => {
it('can add a new token by clicking a key suggestion', async () => {
const mockOnChange = jest.fn();
render(<SearchQueryBuilder {...defaultProps} onChange={mockOnChange} />, {
organization: {features: ['search-query-builder-input-flow-changes']},
organization: {
features: [
'search-query-builder-input-flow-changes',
'search-query-builder-wildcard-operators',
'search-query-builder-default-to-contains',
],
},
});

await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
await userEvent.click(screen.getByRole('option', {name: 'browser.name'}));

// New token should be added with the correct key and default value
expect(screen.getByRole('row', {name: 'browser.name:""'})).toBeInTheDocument();
expect(
screen.getByRole('row', {name: `browser.name:${WildcardOperators.CONTAINS}""`})
).toBeInTheDocument();
// onChange should not be called until exiting edit mode
expect(mockOnChange).not.toHaveBeenCalled();

// Should have focus on the operator option
const operatorOption = await screen.findByRole('option', {name: 'is'});
const operatorOption = await screen.findByRole('option', {name: 'contains'});
expect(operatorOption).toHaveFocus();
await userEvent.click(operatorOption);

await userEvent.click(await screen.findByRole('option', {name: 'Firefox'}));

// New token should have a value
expect(screen.getByRole('row', {name: 'browser.name:Firefox'})).toBeInTheDocument();
expect(
screen.getByRole('row', {
name: `browser.name:${WildcardOperators.CONTAINS}Firefox`,
})
).toBeInTheDocument();

// Now we call onChange
expect(mockOnChange).toHaveBeenCalledTimes(1);
expect(mockOnChange).toHaveBeenCalledWith(
'browser.name:Firefox',
`browser.name:${WildcardOperators.CONTAINS}Firefox`,
expect.anything()
);
});
Expand Down Expand Up @@ -1127,15 +1139,25 @@ describe('SearchQueryBuilder', () => {
});

it('converts text to filter when typing <filter>:', async () => {
render(<SearchQueryBuilder {...defaultProps} />);
render(<SearchQueryBuilder {...defaultProps} />, {
organization: {
features: [
'search-query-builder-input-flow-changes',
'search-query-builder-wildcard-operators',
'search-query-builder-default-to-contains',
],
},
});
await userEvent.click(getLastInput());

await userEvent.type(
screen.getByRole('combobox', {name: 'Add a search term'}),
'browser.name:'
);

const browserNameFilter = await screen.findByRole('row', {name: 'browser.name:""'});
const browserNameFilter = await screen.findByRole('row', {
name: `browser.name:${WildcardOperators.CONTAINS}""`,
});
expect(browserNameFilter).toBeInTheDocument();
});

Expand All @@ -1155,16 +1177,28 @@ describe('SearchQueryBuilder', () => {
});

it('selects [Filtered] from dropdown', async () => {
render(<SearchQueryBuilder {...defaultProps} />);
render(<SearchQueryBuilder {...defaultProps} />, {
organization: {
features: [
'search-query-builder-input-flow-changes',
'search-query-builder-wildcard-operators',
'search-query-builder-default-to-contains',
],
},
});
await userEvent.click(getLastInput());

await userEvent.type(
screen.getByRole('combobox', {name: 'Add a search term'}),
'message:'
);
await userEvent.keyboard('{enter}');
await userEvent.click(screen.getByRole('option', {name: '[Filtered]'}));

expect(
await screen.findByRole('row', {name: 'message:"[Filtered]"'})
await screen.findByRole('row', {
name: `message:${WildcardOperators.CONTAINS}"[Filtered]"`,
})
).toBeInTheDocument();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export function FilterKeyCombobox({token, onCommit, item}: KeyComboboxProps) {
getFieldDefinition(getKeyName(token.key))
);

const hasWildcardOperators =
organization.features.includes('search-query-builder-wildcard-operators') &&
organization.features.includes('search-query-builder-default-to-contains');

const handleSelectKey = useCallback(
(keyName: string) => {
const newFieldDef = getFieldDefinition(keyName);
Expand Down Expand Up @@ -108,7 +112,7 @@ export function FilterKeyCombobox({token, onCommit, item}: KeyComboboxProps) {
dispatch({
type: 'REPLACE_TOKENS_WITH_TEXT_ON_SELECT',
tokens: [token],
text: getInitialFilterText(keyName, newFieldDef),
text: getInitialFilterText(keyName, newFieldDef, hasWildcardOperators),
focusOverride: {
itemKey: item.key.toString(),
part: 'value',
Expand All @@ -122,6 +126,7 @@ export function FilterKeyCombobox({token, onCommit, item}: KeyComboboxProps) {
currentInputValueRef,
dispatch,
getFieldDefinition,
hasWildcardOperators,
item.key,
onCommit,
organization,
Expand Down
17 changes: 12 additions & 5 deletions static/app/components/searchQueryBuilder/tokens/freeText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ function replaceFocusedWordWithFilter(
value: string,
cursorPosition: number,
key: string,
getFieldDefinition: FieldDefinitionGetter
getFieldDefinition: FieldDefinitionGetter,
hasWildcardOperators: boolean
) {
return replaceFocusedWord(
value,
cursorPosition,
getInitialFilterText(key, getFieldDefinition(key))
getInitialFilterText(key, getFieldDefinition(key), hasWildcardOperators)
);
}

Expand Down Expand Up @@ -261,6 +262,9 @@ function SearchQueryBuilderInputInternal({
const hasInputChangeFlows = organization.features.includes(
'search-query-builder-input-flow-changes'
);
const hasWildcardOperators =
organization.features.includes('search-query-builder-wildcard-operators') &&
organization.features.includes('search-query-builder-default-to-contains');

const updateSelectionIndex = useCallback(() => {
setSelectionIndex(inputRef.current?.selectionStart ?? 0);
Expand Down Expand Up @@ -471,7 +475,8 @@ function SearchQueryBuilderInputInternal({
inputValue,
selectionIndex,
value,
getFieldDefinition
getFieldDefinition,
hasWildcardOperators
),
focusOverride: calculateNextFocusForFilter(
state,
Expand Down Expand Up @@ -574,7 +579,8 @@ function SearchQueryBuilderInputInternal({
inputValue,
selectionIndex,
filterValue,
getFieldDefinition
getFieldDefinition,
hasWildcardOperators
),
focusOverride: calculateNextFocusForFilter(
state,
Expand Down Expand Up @@ -617,7 +623,8 @@ function SearchQueryBuilderInputInternal({
inputValue,
selectionIndex,
filterKey,
getFieldDefinition
getFieldDefinition,
hasWildcardOperators
),
focusOverride: calculateNextFocusForFilter(
state,
Expand Down
18 changes: 15 additions & 3 deletions static/app/components/searchQueryBuilder/tokens/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type {
SelectOptionOrSectionWithKey,
SelectSectionWithKey,
} from 'sentry/components/core/compactSelect/types';
import type {ParseResultToken} from 'sentry/components/searchSyntax/parser';
import {areWildcardOperatorsAllowed} from 'sentry/components/searchQueryBuilder/tokens/filter/utils';
import {
WildcardOperators,
type ParseResultToken,
} from 'sentry/components/searchSyntax/parser';
import {defined} from 'sentry/utils';
import {FieldKind, FieldValueType, type FieldDefinition} from 'sentry/utils/fields';

Expand Down Expand Up @@ -113,21 +117,29 @@ function getInitialValueType(fieldDefinition: FieldDefinition | null) {

export function getInitialFilterText(
key: string,
fieldDefinition: FieldDefinition | null
fieldDefinition: FieldDefinition | null,
hasWildcardOperators: boolean
) {
const defaultValue = getDefaultFilterValue({fieldDefinition});

const keyText = getInitialFilterKeyText(key, fieldDefinition);
const valueType = getInitialValueType(fieldDefinition);

const allowContainsOperator =
hasWildcardOperators && areWildcardOperatorsAllowed(fieldDefinition);

switch (valueType) {
case FieldValueType.INTEGER:
case FieldValueType.NUMBER:
case FieldValueType.DURATION:
case FieldValueType.SIZE:
case FieldValueType.PERCENTAGE:
return `${keyText}:>${defaultValue}`;
case FieldValueType.STRING:
case FieldValueType.STRING: {
return allowContainsOperator
? `${keyText}:${WildcardOperators.CONTAINS}${defaultValue}`
: `${keyText}:${defaultValue}`;
}
default:
return `${keyText}:${defaultValue}`;
}
Expand Down
Loading