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
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type {FocusOverride} from 'sentry/components/searchQueryBuilder/types';
import {WildcardOperators} from 'sentry/components/searchSyntax/parser';

import {
replaceFreeTextTokens,
type ReplaceTokensWithTextOnPasteAction,
} from './useQueryBuilderState';
import {replaceFreeTextTokens} from './useQueryBuilderState';

describe('replaceFreeTextTokens', () => {
describe('when there are free text tokens', () => {
Expand All @@ -15,7 +12,6 @@ describe('replaceFreeTextTokens', () => {
query: string | undefined;
};
input: {
action: ReplaceTokensWithTextOnPasteAction;
currentQuery: string;
getFieldDefinition: () => null;
rawSearchReplacement: string[];
Expand All @@ -26,12 +22,6 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there are no tokens',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: '',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: '',
Expand All @@ -44,12 +34,6 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when the replace raw search keys is empty',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: '',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: [],
currentQuery: '',
Expand All @@ -62,12 +46,6 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when the replace raw search keys is an empty string',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: '',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: [''],
currentQuery: '',
Expand All @@ -80,12 +58,6 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there is no raw search replacement',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: '',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: [],
currentQuery: `browser.name:${WildcardOperators.CONTAINS}"firefox"`,
Expand All @@ -98,12 +70,6 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there are no free text tokens',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: '',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: `browser.name:${WildcardOperators.CONTAINS}"firefox"`,
Expand All @@ -116,15 +82,9 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there only valid action tokens',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'span.op:eq',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: '',
currentQuery: 'span.op:eq',
},
expected: {
query: undefined,
Expand All @@ -134,15 +94,9 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there only space free text tokens in the action',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'span.op:eq ',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: '',
currentQuery: 'span.op:eq ',
},
expected: {
query: undefined,
Expand All @@ -152,15 +106,9 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there is one free text token',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'test',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: '',
currentQuery: 'test',
},
expected: {
query: `span.description:${WildcardOperators.CONTAINS}test`,
Expand All @@ -170,15 +118,9 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there is one free text token that has a space',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'test test',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: '',
currentQuery: 'test test',
},
expected: {
query: `span.description:${WildcardOperators.CONTAINS}"test test"`,
Expand All @@ -188,15 +130,9 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there is already a token present',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'test',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: 'span.op:eq',
currentQuery: 'span.op:eq test',
},
expected: {
query: `span.op:eq span.description:${WildcardOperators.CONTAINS}test`,
Expand All @@ -206,52 +142,34 @@ describe('replaceFreeTextTokens', () => {
{
description: 'when there is already a replace token present',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'test2',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: `span.description:${WildcardOperators.CONTAINS}test`,
currentQuery: `span.description:${WildcardOperators.CONTAINS}test test2`,
},
expected: {
query: `span.description:${WildcardOperators.CONTAINS}[test,test2]`,
focusOverride: {itemKey: 'freeText:1'},
query: `span.description:${WildcardOperators.CONTAINS}test span.description:${WildcardOperators.CONTAINS}test2`,
focusOverride: {itemKey: 'freeText:2'},
},
},
{
description: 'when there is already a replace token present with a space',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'other value',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: `span.description:${WildcardOperators.CONTAINS}test`,
currentQuery: `span.description:${WildcardOperators.CONTAINS}test other value`,
},
expected: {
query: `span.description:${WildcardOperators.CONTAINS}[test,"other value"]`,
focusOverride: {itemKey: 'freeText:1'},
query: `span.description:${WildcardOperators.CONTAINS}test span.description:${WildcardOperators.CONTAINS}"other value"`,
focusOverride: {itemKey: 'freeText:2'},
},
},
{
description:
'when there is already a replace token present with a different operator',
input: {
action: {
type: 'REPLACE_TOKENS_WITH_TEXT_ON_PASTE',
text: 'other value',
tokens: [],
focusOverride: undefined,
},
getFieldDefinition: () => null,
rawSearchReplacement: ['span.description'],
currentQuery: `span.description:test`,
currentQuery: `span.description:test other value`,
},
expected: {
query: `span.description:test span.description:${WildcardOperators.CONTAINS}"other value"`,
Expand All @@ -262,10 +180,9 @@ describe('replaceFreeTextTokens', () => {

it.each(testCases)('$description', ({input, expected}) => {
const result = replaceFreeTextTokens(
input.action,
input.currentQuery,
input.getFieldDefinition,
input.rawSearchReplacement,
input.currentQuery
input.rawSearchReplacement
);

expect(result?.newQuery).toBe(expected.query);
Expand Down
Loading
Loading