fix(editor): Render empty string instead of [empty] (#12448)

This commit is contained in:
Shireen Missi 2025-01-08 09:30:35 +00:00 committed by GitHub
parent 803960e092
commit 2c72047d0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 110 additions and 13 deletions

View file

@ -132,6 +132,109 @@ describe('useExpressionEditor', () => {
});
});
test('render [empty] when expression evaluates to an empty string', async () => {
mockResolveExpression().mockReturnValueOnce('');
const {
expressionEditor: { segments },
} = await renderExpressionEditor({
editorValue: "{{ '' }}",
extensions: [n8nLang()],
});
await waitFor(() => {
expect(toValue(segments.all)).toEqual([
{
error: null,
from: 0,
kind: 'resolvable',
resolvable: "{{ '' }}",
resolved: '[empty]',
state: 'valid',
to: 8,
},
]);
expect(toValue(segments.resolvable)).toEqual([
{
error: null,
from: 0,
kind: 'resolvable',
resolvable: "{{ '' }}",
resolved: '[empty]',
state: 'valid',
to: 8,
},
]);
expect(toValue(segments.plaintext)).toEqual([]);
});
});
test('does not render [empty] when expression evaluates to an empty string within a string', async () => {
mockResolveExpression().mockReturnValueOnce('');
const {
expressionEditor: { segments },
} = await renderExpressionEditor({
editorValue: "before {{ '' }} after",
extensions: [n8nLang()],
});
await waitFor(() => {
expect(toValue(segments.all)).toEqual([
{
from: 0,
kind: 'plaintext',
plaintext: 'before ',
to: 7,
},
{
error: null,
from: 7,
kind: 'resolvable',
resolvable: "{{ '' }}",
resolved: '',
state: 'valid',
to: 15,
},
{
from: 15,
kind: 'plaintext',
plaintext: ' after',
to: 21,
},
]);
expect(toValue(segments.resolvable)).toEqual([
{
error: null,
from: 7,
kind: 'resolvable',
resolvable: "{{ '' }}",
resolved: '',
state: 'valid',
to: 15,
},
]);
expect(toValue(segments.plaintext)).toEqual([
{
from: 0,
kind: 'plaintext',
plaintext: 'before ',
to: 7,
},
{
from: 15,
kind: 'plaintext',
plaintext: ' after',
to: 21,
},
]);
});
});
describe('readEditorValue()', () => {
test('should return the full editor value (unresolved)', async () => {
mockResolveExpression().mockReturnValueOnce(15);

View file

@ -22,11 +22,7 @@ import { useWorkflowHelpers } from '@/composables/useWorkflowHelpers';
import { highlighter } from '@/plugins/codemirror/resolvableHighlighter';
import { closeCursorInfoBox } from '@/plugins/codemirror/tooltips/InfoBoxTooltip';
import type { Html, Plaintext, RawSegment, Resolvable, Segment } from '@/types/expressions';
import {
getExpressionErrorMessage,
getResolvableState,
isEmptyExpression,
} from '@/utils/expressions';
import { getExpressionErrorMessage, getResolvableState } from '@/utils/expressions';
import { closeCompletion, completionStatus } from '@codemirror/autocomplete';
import {
Compartment,
@ -129,6 +125,12 @@ export const useExpressionEditor = ({
return acc;
}, []);
if (
segments.value.length === 1 &&
segments.value[0]?.kind === 'resolvable' &&
segments.value[0]?.resolved === ''
)
segments.value[0].resolved = i18n.baseText('expressionModalInput.empty');
};
function readEditorValue(): string {
@ -314,14 +316,6 @@ export const useExpressionEditor = ({
result.fullError = error;
}
if (result.resolved === '') {
result.resolved = i18n.baseText('expressionModalInput.empty');
}
if (result.resolved === undefined && isEmptyExpression(resolvable)) {
result.resolved = i18n.baseText('expressionModalInput.empty');
}
if (result.resolved === undefined) {
result.resolved = isUncalledExpressionExtension(resolvable)
? i18n.baseText('expressionEditor.uncalledFunction')