Skip to content

Commit

Permalink
fix(editor): Allow switch to Fixed for boolean and number parameter…
Browse files Browse the repository at this point in the history
…s with invalid expressions (#12948)
  • Loading branch information
CharlieKolb authored Jan 31, 2025
1 parent c60cc43 commit 118be24
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
85 changes: 85 additions & 0 deletions packages/editor-ui/src/components/ParameterInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { waitFor } from '@testing-library/vue';
import userEvent from '@testing-library/user-event';
import type { useNodeTypesStore } from '@/stores/nodeTypes.store';
import { cleanupAppModals, createAppModals } from '@/__tests__/utils';
import { createEventBus } from 'n8n-design-system';

let mockNdvState: Partial<ReturnType<typeof useNDVStore>>;
let mockNodeTypesState: Partial<ReturnType<typeof useNodeTypesStore>>;
Expand Down Expand Up @@ -233,4 +234,88 @@ describe('ParameterInput.vue', () => {

expect(emitted('update')).toBeUndefined();
});

test('should reset bool on eventBus:removeExpression', async () => {
const eventBus = createEventBus();
const { emitted } = renderComponent(ParameterInput, {
pinia: createTestingPinia(),
props: {
path: 'aSwitch',
parameter: {
displayName: 'A Switch',
name: 'aSwitch',
type: 'boolean',
default: true,
},
modelValue: '={{ }}', // note that this makes a syntax error
eventBus,
},
});

eventBus.emit('optionSelected', 'removeExpression');
expect(emitted('update')).toContainEqual([expect.objectContaining({ value: true })]);
});

test('should reset bool with undefined evaluation on eventBus:removeExpression', async () => {
const eventBus = createEventBus();
const { emitted } = renderComponent(ParameterInput, {
pinia: createTestingPinia(),
props: {
path: 'aSwitch',
parameter: {
displayName: 'A Switch',
name: 'aSwitch',
type: 'boolean',
default: true,
},
modelValue: undefined,
eventBus,
},
});

eventBus.emit('optionSelected', 'removeExpression');
expect(emitted('update')).toContainEqual([expect.objectContaining({ value: true })]);
});

test('should reset number on eventBus:removeExpression', async () => {
const eventBus = createEventBus();
const { emitted } = renderComponent(ParameterInput, {
pinia: createTestingPinia(),
props: {
path: 'aNum',
parameter: {
displayName: 'A Num',
name: 'aNum',
type: 'number',
default: 6,
},
modelValue: '={{ }}', // note that this makes a syntax error
eventBus,
},
});

eventBus.emit('optionSelected', 'removeExpression');
expect(emitted('update')).toContainEqual([expect.objectContaining({ value: 6 })]);
});

test('should reset string on eventBus:removeExpression', async () => {
const eventBus = createEventBus();
const { emitted } = renderComponent(ParameterInput, {
pinia: createTestingPinia(),
props: {
path: 'aStr',
parameter: {
displayName: 'A Str',
name: 'aStr',
type: 'string',
default: 'some default',
},
modelValue: '={{ }}', // note that this makes a syntax error
eventBus,
},
});

eventBus.emit('optionSelected', 'removeExpression');
expect(emitted('update')).toContainEqual([expect.objectContaining({ value: '{{ }}' })]);
});
});
7 changes: 6 additions & 1 deletion packages/editor-ui/src/components/ParameterInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,16 @@ async function optionSelected(command: string) {
if (isResourceLocatorParameter.value && isResourceLocatorValue(props.modelValue)) {
valueChanged({ __rl: true, value, mode: props.modelValue.mode });
} else {
let newValue = typeof value !== 'undefined' ? value : null;
let newValue: NodeParameterValueType | {} = typeof value !== 'undefined' ? value : null;
if (props.parameter.type === 'string') {
// Strip the '=' from the beginning
newValue = modelValueString.value ? modelValueString.value.toString().substring(1) : null;
} else if (newValue === null) {
// Invalid expressions land here
if (['number', 'boolean'].includes(props.parameter.type)) {
newValue = props.parameter.default;
}
}
valueChanged(newValue);
}
Expand Down

0 comments on commit 118be24

Please sign in to comment.