From cb41db37cb384717bb483edf793973a5c7e2f3b2 Mon Sep 17 00:00:00 2001 From: Stefan Hengl Date: Thu, 27 Jun 2024 12:50:12 +0200 Subject: [PATCH] search: fix regexp toggle if regexp is default (#63489) I noticed that the regexp toggle doesn't work anymore if `"search.defaultPatternType": "regexp"`. This is related to a recent change #63410. We also append `patternType:keyword` in that case which I don't think we want, because we have an UI element to indicate that keyword search is active. The question I don't know how to answer is: What should happen if `regexp` is the default and the user toggles keyword search off. Should we go back to `regexp` or to `standard`? Test plan: - new unit test - manual testing with default pattern type set to "keyword" and "regexp". --- .../search-ui/input/toggles/Toggles.test.tsx | 33 +++++++++++++++++-- .../src/search-ui/input/toggles/Toggles.tsx | 8 ++++- .../__snapshots__/Toggles.test.tsx.snap | 25 ++++++++++++++ .../web/src/stores/navbarSearchQueryState.ts | 6 +++- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/client/branded/src/search-ui/input/toggles/Toggles.test.tsx b/client/branded/src/search-ui/input/toggles/Toggles.test.tsx index e5dce30e507..031f6c8edfd 100644 --- a/client/branded/src/search-ui/input/toggles/Toggles.test.tsx +++ b/client/branded/src/search-ui/input/toggles/Toggles.test.tsx @@ -1,5 +1,5 @@ -import { screen } from '@testing-library/react' -import { describe, expect, test } from 'vitest' +import { screen, fireEvent } from '@testing-library/react' +import { describe, expect, test, vi } from 'vitest' import { SearchPatternType } from '@sourcegraph/shared/src/graphql-operations' import { SearchMode } from '@sourcegraph/shared/src/search' @@ -83,5 +83,34 @@ describe('Toggles', () => { ) expect(screen.getAllByRole('checkbox', { name: 'Regular expression toggle' })).toMatchSnapshot() }) + + test('Regex toggles off even if defaultPatternType is regexp', () => { + const setPatternType = vi.fn() + + renderWithBrandedContext( + undefined} + searchMode={SearchMode.Precise} + setSearchMode={() => undefined} + telemetryService={NOOP_TELEMETRY_SERVICE} + telemetryRecorder={noOpTelemetryRecorder} + /> + ) + + // Initially, the regexp toggle should be checked + + expect(screen.getByRole('checkbox', { name: 'Regular expression toggle' })).toMatchSnapshot() + + // Toggle the regexp off + fireEvent.click(screen.getByRole('checkbox', { name: 'Regular expression toggle' })) + + // Verify that setPatternType was called with patternType keyword + expect(setPatternType).toHaveBeenCalledWith(SearchPatternType.keyword) + }) }) }) diff --git a/client/branded/src/search-ui/input/toggles/Toggles.tsx b/client/branded/src/search-ui/input/toggles/Toggles.tsx index dde6eee7afc..01d929f66b2 100644 --- a/client/branded/src/search-ui/input/toggles/Toggles.tsx +++ b/client/branded/src/search-ui/input/toggles/Toggles.tsx @@ -77,7 +77,13 @@ export const Toggles: React.FunctionComponent { - const newPatternType = patternType !== SearchPatternType.regexp ? SearchPatternType.regexp : defaultPatternType + const newPatternType = + patternType !== SearchPatternType.regexp + ? SearchPatternType.regexp + : // Handle the case where the user has regexp configured as the default pattern type. + defaultPatternType === SearchPatternType.regexp + ? SearchPatternType.keyword + : defaultPatternType setPatternType(newPatternType) submitOnToggle({ newPatternType }) diff --git a/client/branded/src/search-ui/input/toggles/__snapshots__/Toggles.test.tsx.snap b/client/branded/src/search-ui/input/toggles/__snapshots__/Toggles.test.tsx.snap index 574ba9f76f5..ab1a3c2cc1d 100644 --- a/client/branded/src/search-ui/input/toggles/__snapshots__/Toggles.test.tsx.snap +++ b/client/branded/src/search-ui/input/toggles/__snapshots__/Toggles.test.tsx.snap @@ -1,5 +1,30 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`Toggles > Query input toggle state > Regex toggles off even if defaultPatternType is regexp 1`] = ` + +`; + exports[`Toggles > Query input toggle state > case toggle for case subexpressions 1`] = ` Array [