Skip to content

Commit

Permalink
only render empty string as [empty]
Browse files Browse the repository at this point in the history
  • Loading branch information
ShireenMissi committed Jan 8, 2025
1 parent 5b925bc commit 9264ef8
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 13 deletions.
103 changes: 103 additions & 0 deletions packages/editor-ui/src/composables/useExpressionEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 7 additions & 13 deletions packages/editor-ui/src/composables/useExpressionEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 9264ef8

Please sign in to comment.