-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Autocomplete random variables #4695
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
sanjaikumar-bruno
wants to merge
12
commits into
usebruno:main
Choose a base branch
from
sanjaikumar-bruno:add-tests-for-autocomplete-random-variables
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 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e129753
Feature: adding dynamic variable support (#3609)
rsxc f73229f
Refactor and enhance mock data and variable interpolation
sanjai0py 5aa21d9
Added tests
sanjai0py cfb38ef
Refactor moved get hints function to a util and removed unnecessary c…
sanjai0py ca04cb4
Refactor: remove unused mock data functions and suggestions
sanjai0py 334ac1f
Refactor: enhance CodeMirror mock and update autocomplete tests
sanjai0py b190e20
Add @testing-library/dom and related dependencies to package-lock.json
sanjai0py b31d16e
Refactor: update testing-library dependencies and clean up unused pac…
sanjai0py f76128a
pushing latest package-lock.json
sanjai0py 3fb0184
refactor: highlight valid randomVariables in green; fix autocomplete …
sanjai0py 720b55b
Updates CodeEditor paths and excludes test files in rsbuild configura…
sanjai0py ed8d33d
refactor: update eslint config to specify codemirror mock file
sanjai0py 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-env"], | ||
"presets": ["@babel/preset-env", "@babel/preset-react"], | ||
"plugins": [["styled-components", { "ssr": true }]] | ||
} |
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
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,11 @@ | ||
jest.mock('nanoid', () => { | ||
return { | ||
nanoid: () => {} | ||
}; | ||
}); | ||
|
||
jest.mock('strip-json-comments', () => { | ||
return { | ||
stripJsonComments: (str) => str | ||
}; | ||
}); |
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
43 changes: 43 additions & 0 deletions
43
packages/bruno-app/src/components/CodeEditor/__mocks__/codemirror.js
sanjaikumar-bruno marked this conversation as resolved.
Show resolved
Hide resolved
|
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,43 @@ | ||
const CodeMirror = jest.fn((node, options) => { | ||
const editor = { | ||
options, | ||
_currentValue: '', | ||
inputReadHandler: null, | ||
getCursor: jest.fn(() => ({ line: 0, ch: editor._currentValue?.length || 0 })), | ||
getRange: jest.fn((from, to) => editor._currentValue?.slice(0, to.ch) || ''), | ||
getValue: jest.fn(() => editor._currentValue), | ||
setValue: jest.fn(function (val) { | ||
editor._currentValue = val; | ||
}), | ||
getLine: jest.fn(() => editor._currentValue || ''), | ||
setOption: jest.fn(), | ||
refresh: jest.fn(), | ||
off: jest.fn(), | ||
showHint: jest.fn(), | ||
on: jest.fn(function (event, handler) { | ||
if (event === 'inputRead') { | ||
this.inputReadHandler = handler; | ||
} | ||
}), | ||
}; | ||
return editor; | ||
}); | ||
|
||
CodeMirror.commands = { | ||
autocomplete: jest.fn() | ||
}; | ||
|
||
CodeMirror.hint = {}; | ||
|
||
CodeMirror.registerHelper = jest.fn((type, name, value) => { | ||
if (!CodeMirror[type]) { | ||
CodeMirror[type] = {}; | ||
} | ||
|
||
CodeMirror[type][name] = value; | ||
}); | ||
|
||
CodeMirror.fromTextArea = jest.fn(); | ||
CodeMirror.defineMode = jest.fn(); | ||
|
||
module.exports = CodeMirror; |
97 changes: 97 additions & 0 deletions
97
packages/bruno-app/src/components/CodeEditor/__tests__/CodeEditor.autocomplete.test.js
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,97 @@ | ||
import React from 'react'; | ||
import { render, act } from '@testing-library/react'; | ||
import CodeEditor from '../../CodeEditor'; | ||
import { ThemeProvider } from 'styled-components'; | ||
|
||
jest.mock('codemirror'); | ||
|
||
const MOCK_THEME = { | ||
codemirror: { | ||
bg: "#1e1e1e", | ||
border: "#333", | ||
}, | ||
textLink: "#007acc", | ||
}; | ||
|
||
const setupEditorState = (editor, { value, cursorPosition }) => { | ||
editor._currentValue = value; | ||
editor.getCursor.mockReturnValue({ line: 0, ch: cursorPosition }); | ||
editor.getRange.mockImplementation((from, to) => { | ||
if (from.line === 0 && from.ch === 0 && to.line === 0 && to.ch === cursorPosition) { | ||
return value; | ||
} | ||
return editor._currentValue.slice(from.ch, to.ch); | ||
}); | ||
}; | ||
|
||
const setupEditorWithRef = () => { | ||
const ref = React.createRef(); | ||
const { rerender } = render( | ||
<ThemeProvider theme={MOCK_THEME}> | ||
<CodeEditor ref={ref} /> | ||
</ThemeProvider> | ||
); | ||
return { ref, rerender }; | ||
}; | ||
|
||
describe('CodeEditor Autocomplete', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('shows hint suggestions when typing {{$f', () => { | ||
// Setup | ||
const { ref } = setupEditorWithRef(); | ||
|
||
const editorInstance = ref.current; | ||
expect(editorInstance).toBeTruthy(); | ||
|
||
const editor = editorInstance.editor; | ||
expect(editor).toBeTruthy(); | ||
|
||
// Configure editor state | ||
setupEditorState(editor, { | ||
value: '{{$r', | ||
cursorPosition: 4 | ||
}); | ||
|
||
// Trigger autocomplete | ||
const inputReadHandler = editor.inputReadHandler; | ||
expect(typeof inputReadHandler).toBe('function'); | ||
|
||
act(() => { | ||
inputReadHandler(editor, { text: ['a'], origin: '+input' }); | ||
}); | ||
|
||
// Assertions | ||
expect(editor.showHint).toHaveBeenCalled(); | ||
const call = editor.showHint.mock.calls[0][0]; | ||
expect(typeof call.hint).toBe('function'); | ||
|
||
const hints = call.hint(); | ||
expect(Array.isArray(hints.list)).toBe(true); | ||
expect(hints.list.some((s) => s.startsWith('$'))).toBe(true); | ||
sanjaikumar-bruno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it('does not show hints for regular text input', () => { | ||
// Setup | ||
const { ref } = setupEditorWithRef(); | ||
const editor = ref.current.editor; | ||
|
||
// Configure editor state | ||
setupEditorState(editor, { | ||
value: 'regular text', | ||
cursorPosition: 11 | ||
}); | ||
|
||
// Trigger input | ||
const inputReadHandler = editor.inputReadHandler; | ||
|
||
act(() => { | ||
inputReadHandler(editor, { text: ['x'], origin: '+input' }); | ||
}); | ||
|
||
// Assert no hints shown for regular text | ||
expect(editor.showHint).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
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
25 changes: 25 additions & 0 deletions
25
packages/bruno-app/src/utils/codemirror/mock-data-hints.js
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,25 @@ | ||
import { mockDataFunctions } from '@usebruno/common'; | ||
|
||
const MOCK_FUNCTION_SUGGESTIONS = Object.keys(mockDataFunctions).map(key => `$${key}`); | ||
|
||
export const getMockDataHints = (cm) => { | ||
const cursor = cm.getCursor(); | ||
const currentString = cm.getRange({ line: cursor.line, ch: 0 }, cursor); | ||
|
||
const match = currentString.match(/\{\{\$(\w*)$/); | ||
if (!match) return null; | ||
|
||
const wordMatch = match[1]; | ||
if (!wordMatch) return null; | ||
|
||
const suggestions = MOCK_FUNCTION_SUGGESTIONS.filter(name => name.startsWith(`$${wordMatch}`)); | ||
if (!suggestions.length) return null; | ||
|
||
const startPos = { line: cursor.line, ch: currentString.lastIndexOf('{{$') + 2 }; // +2 accounts for `{{` | ||
|
||
return { | ||
list: suggestions, | ||
from: startPos, | ||
to: cm.getCursor(), | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { mockDataFunctions } from './utils/faker-functions'; | ||
export { default as interpolate } from './interpolate'; |
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
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.