mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 09:04:07 -08:00
262 lines
6.5 KiB
TypeScript
262 lines
6.5 KiB
TypeScript
import type { FormField } from '../interfaces';
|
|
import { prepareFormData } from '../utils';
|
|
|
|
describe('FormTrigger, prepareFormData', () => {
|
|
it('should return valid form data with given parameters', () => {
|
|
const formFields: FormField[] = [
|
|
{
|
|
fieldLabel: 'Name',
|
|
fieldType: 'text',
|
|
requiredField: true,
|
|
placeholder: 'Enter your name',
|
|
},
|
|
{
|
|
fieldLabel: 'Email',
|
|
fieldType: 'email',
|
|
requiredField: true,
|
|
placeholder: 'Enter your email',
|
|
},
|
|
{
|
|
fieldLabel: 'Gender',
|
|
fieldType: 'dropdown',
|
|
requiredField: false,
|
|
fieldOptions: { values: [{ option: 'Male' }, { option: 'Female' }] },
|
|
},
|
|
{
|
|
fieldLabel: 'Files',
|
|
fieldType: 'file',
|
|
requiredField: false,
|
|
acceptFileTypes: '.jpg,.png',
|
|
multipleFiles: true,
|
|
},
|
|
];
|
|
|
|
const query = { Name: 'John Doe', Email: 'john@example.com' };
|
|
|
|
const result = prepareFormData({
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: 'Thank you for your submission',
|
|
redirectUrl: 'https://example.com/thank-you',
|
|
formFields,
|
|
testRun: false,
|
|
query,
|
|
instanceId: 'test-instance',
|
|
useResponseData: true,
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
testRun: false,
|
|
validForm: true,
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: 'Thank you for your submission',
|
|
n8nWebsiteLink:
|
|
'https://n8n.io/?utm_source=n8n-internal&utm_medium=form-trigger&utm_campaign=test-instance',
|
|
formFields: [
|
|
{
|
|
id: 'field-0',
|
|
errorId: 'error-field-0',
|
|
label: 'Name',
|
|
inputRequired: 'form-required',
|
|
defaultValue: 'John Doe',
|
|
placeholder: 'Enter your name',
|
|
isInput: true,
|
|
type: 'text',
|
|
},
|
|
{
|
|
id: 'field-1',
|
|
errorId: 'error-field-1',
|
|
label: 'Email',
|
|
inputRequired: 'form-required',
|
|
defaultValue: 'john@example.com',
|
|
placeholder: 'Enter your email',
|
|
isInput: true,
|
|
type: 'email',
|
|
},
|
|
{
|
|
id: 'field-2',
|
|
errorId: 'error-field-2',
|
|
label: 'Gender',
|
|
inputRequired: '',
|
|
defaultValue: '',
|
|
placeholder: undefined,
|
|
isSelect: true,
|
|
selectOptions: ['Male', 'Female'],
|
|
},
|
|
{
|
|
id: 'field-3',
|
|
errorId: 'error-field-3',
|
|
label: 'Files',
|
|
inputRequired: '',
|
|
defaultValue: '',
|
|
placeholder: undefined,
|
|
isFileInput: true,
|
|
acceptFileTypes: '.jpg,.png',
|
|
multipleFiles: 'multiple',
|
|
},
|
|
],
|
|
useResponseData: true,
|
|
appendAttribution: true,
|
|
redirectUrl: 'https://example.com/thank-you',
|
|
});
|
|
});
|
|
|
|
it('should handle missing optional fields gracefully', () => {
|
|
const formFields: FormField[] = [
|
|
{
|
|
fieldLabel: 'Name',
|
|
fieldType: 'text',
|
|
requiredField: true,
|
|
placeholder: 'Enter your name',
|
|
},
|
|
];
|
|
|
|
const result = prepareFormData({
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: undefined,
|
|
redirectUrl: undefined,
|
|
formFields,
|
|
testRun: true,
|
|
query: {},
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
testRun: true,
|
|
validForm: true,
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: 'Your response has been recorded',
|
|
n8nWebsiteLink: 'https://n8n.io/?utm_source=n8n-internal&utm_medium=form-trigger',
|
|
formFields: [
|
|
{
|
|
id: 'field-0',
|
|
errorId: 'error-field-0',
|
|
label: 'Name',
|
|
inputRequired: 'form-required',
|
|
defaultValue: '',
|
|
placeholder: 'Enter your name',
|
|
isInput: true,
|
|
type: 'text',
|
|
},
|
|
],
|
|
useResponseData: undefined,
|
|
appendAttribution: true,
|
|
});
|
|
});
|
|
|
|
it('should set redirectUrl with http if protocol is missing', () => {
|
|
const formFields: FormField[] = [
|
|
{
|
|
fieldLabel: 'Name',
|
|
fieldType: 'text',
|
|
requiredField: true,
|
|
placeholder: 'Enter your name',
|
|
},
|
|
];
|
|
|
|
const query = { Name: 'John Doe' };
|
|
|
|
const result = prepareFormData({
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: undefined,
|
|
redirectUrl: 'example.com/thank-you',
|
|
formFields,
|
|
testRun: true,
|
|
query,
|
|
});
|
|
|
|
expect(result.redirectUrl).toBe('http://example.com/thank-you');
|
|
});
|
|
|
|
it('should return invalid form data when formFields are empty', () => {
|
|
const result = prepareFormData({
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: undefined,
|
|
redirectUrl: undefined,
|
|
formFields: [],
|
|
testRun: true,
|
|
query: {},
|
|
});
|
|
|
|
expect(result.validForm).toBe(false);
|
|
expect(result.formFields).toEqual([]);
|
|
});
|
|
|
|
it('should correctly handle multiselect fields', () => {
|
|
const formFields: FormField[] = [
|
|
{
|
|
fieldLabel: 'Favorite Colors',
|
|
fieldType: 'text',
|
|
requiredField: true,
|
|
multiselect: true,
|
|
fieldOptions: { values: [{ option: 'Red' }, { option: 'Blue' }, { option: 'Green' }] },
|
|
},
|
|
];
|
|
|
|
const query = { 'Favorite Colors': 'Red,Blue' };
|
|
|
|
const result = prepareFormData({
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: 'Thank you',
|
|
redirectUrl: 'example.com',
|
|
formFields,
|
|
testRun: false,
|
|
query,
|
|
});
|
|
|
|
expect(result.formFields[0].isMultiSelect).toBe(true);
|
|
expect(result.formFields[0].multiSelectOptions).toEqual([
|
|
{ id: 'option0_field-0', label: 'Red' },
|
|
{ id: 'option1_field-0', label: 'Blue' },
|
|
{ id: 'option2_field-0', label: 'Green' },
|
|
]);
|
|
});
|
|
it('should correctly handle multiselect fields with unique ids', () => {
|
|
const formFields: FormField[] = [
|
|
{
|
|
fieldLabel: 'Favorite Colors',
|
|
fieldType: 'text',
|
|
requiredField: true,
|
|
multiselect: true,
|
|
fieldOptions: { values: [{ option: 'Red' }, { option: 'Blue' }, { option: 'Green' }] },
|
|
},
|
|
{
|
|
fieldLabel: 'Favorite Colors',
|
|
fieldType: 'text',
|
|
requiredField: true,
|
|
multiselect: true,
|
|
fieldOptions: { values: [{ option: 'Red' }, { option: 'Blue' }, { option: 'Green' }] },
|
|
},
|
|
];
|
|
|
|
const query = { 'Favorite Colors': 'Red,Blue' };
|
|
|
|
const result = prepareFormData({
|
|
formTitle: 'Test Form',
|
|
formDescription: 'This is a test form',
|
|
formSubmittedText: 'Thank you',
|
|
redirectUrl: 'example.com',
|
|
formFields,
|
|
testRun: false,
|
|
query,
|
|
});
|
|
|
|
expect(result.formFields[0].isMultiSelect).toBe(true);
|
|
expect(result.formFields[0].multiSelectOptions).toEqual([
|
|
{ id: 'option0_field-0', label: 'Red' },
|
|
{ id: 'option1_field-0', label: 'Blue' },
|
|
{ id: 'option2_field-0', label: 'Green' },
|
|
]);
|
|
expect(result.formFields[1].multiSelectOptions).toEqual([
|
|
{ id: 'option0_field-1', label: 'Red' },
|
|
{ id: 'option1_field-1', label: 'Blue' },
|
|
{ id: 'option2_field-1', label: 'Green' },
|
|
]);
|
|
});
|
|
});
|