Files
supabase/apps/studio/components/ui/CodeEditor/CodeEditor.utils.test.ts
Joshen Lim 8d59e69da4 Add support for running only selected query in QueryEditor (#49651)
## Context

As per PR title - this behaviour currently exists in the SQL Editor so
just bringing it over to the Explorer, applies to both QueryTab and
QueryCell since they use the same QueryEditor component

"Run" button also updates to "Run selected" for clarity when a specific
portion of the code editor is selected

<img width="1387" height="958" alt="image"
src="https://github.com/user-attachments/assets/7e652951-3c07-4f8d-851b-bb9219d0c1f2"
/>


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Run only the selected SQL when text is highlighted in the query
editor.
  * Run the full query when no text is selected.
  * Updated the run button label and tooltip to reflect the action.

* **Bug Fixes**
  * Improved query execution for empty or collapsed selections.
  * Corrected selection state when reopening the query editor.

* **Tests**
* Added coverage for selected-text, full-query, and editor reopening
scenarios.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-28 12:49:35 +08:00

52 lines
1.5 KiB
TypeScript

import type { editor } from 'monaco-editor'
import { describe, expect, it } from 'vitest'
import { getEditorValueOrSelection } from './CodeEditor.utils'
const buildEditor = ({
value,
selectedValue,
hasSelection,
}: {
value: string
selectedValue?: string
hasSelection: boolean
}): editor.IStandaloneCodeEditor => {
return {
getValue: () => value,
getSelection: () => (hasSelection ? {} : null),
getModel: () => ({ getValueInRange: () => selectedValue }),
} as unknown as editor.IStandaloneCodeEditor
}
describe('getEditorValueOrSelection', () => {
it('returns the selected text when there is a non-empty selection', () => {
const editorInstance = buildEditor({
value: 'select * from a;\nselect * from b;',
selectedValue: 'select * from b;',
hasSelection: true,
})
expect(getEditorValueOrSelection(editorInstance)).toBe('select * from b;')
})
it('falls back to the full value when there is no selection', () => {
const editorInstance = buildEditor({
value: 'select * from a;',
hasSelection: false,
})
expect(getEditorValueOrSelection(editorInstance)).toBe('select * from a;')
})
it('falls back to the full value when the selection is collapsed (empty range)', () => {
const editorInstance = buildEditor({
value: 'select * from a;',
selectedValue: '',
hasSelection: true,
})
expect(getEditorValueOrSelection(editorInstance)).toBe('select * from a;')
})
})