rob-revisited/ide/ui/js/symfony-forms-preview.test.mjs

111 lines
10 KiB
JavaScript

// The HTML of the Forms window's preview, per form theme (symfony-forms-preview.js).
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/symfony-forms-preview.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { formHtml, themeStylesheets, standalonePage } from './symfony-forms-preview.js';
import { blockPrefix, humanize, rendererOf, constraintMessage, listenerName, phpOf, optionsOf, CORE, ENTITY_TYPE } from './symfony-forms-model.js';
const V = 'Symfony\\Component\\Validator\\Constraints\\';
const model = {
class: 'App\\Form\\UserProfileType',
fields: [
{ name: 'firstName', type: CORE + 'TextType', options: { help: 'As on your passport', attr: { placeholder: 'Ada' } } },
{ name: 'email', type: CORE + 'EmailType', options: { constraints: [{ '@new': V + 'Email', args: { message: 'Not an e-mail.' } }] } },
{ name: 'bio', type: CORE + 'TextareaType', options: { required: false } },
{ name: 'size', type: CORE + 'ChoiceType', options: { choices: { Small: 's', Large: 'l' }, expanded: true } },
{ name: 'country', type: CORE + 'CountryType', options: { placeholder: 'Choose' } },
{ name: 'status', type: CORE + 'EnumType', options: { class: { '@class': 'App\\Enum\\Status' } } },
{ name: 'team', type: ENTITY_TYPE, options: { class: { '@class': 'App\\Entity\\Team' }, choice_label: 'name', multiple: true } },
{ name: 'salary', type: CORE + 'MoneyType', options: { currency: 'EUR' } },
{ name: 'born', type: CORE + 'DateType', options: { widget: 'single_text' } },
{ name: 'newsletter', type: CORE + 'CheckboxType', options: { required: false } },
{ name: 'password', type: CORE + 'RepeatedType', options: { type: { '@class': CORE + 'PasswordType' }, first_options: { label: 'Password' }, second_options: { label: 'Repeat' } } },
{ name: 'address', type: 'App\\Form\\AddressType', options: {} },
{ name: 'links', type: CORE + 'CollectionType', options: { entry_type: { '@class': CORE + 'UrlType' }, allow_add: true } },
{ name: 'token', type: CORE + 'HiddenType', options: {} },
{ name: 'save', type: CORE + 'SubmitType', options: { label: 'Save it' } },
],
defaults: {},
};
const subforms = { 'App\\Form\\AddressType': { class: 'App\\Form\\AddressType', fields: [{ name: 'city', type: CORE + 'TextType', options: {} }] } };
const enums = { 'App\\Enum\\Status': ['Active', 'Banned'] };
test('names: block prefixes, labels, listener methods, renderers', () => {
assert.equal(blockPrefix('App\\Form\\UserProfileType'), 'user_profile');
assert.equal(blockPrefix('App\\Form\\HTMLPageType'), 'html_page');
assert.equal(humanize('firstName'), 'First name');
assert.equal(humanize('created_at'), 'Created at');
assert.equal(listenerName('PRE_SET_DATA'), 'onPreSetData');
assert.equal(rendererOf(['form/fields.html.twig', 'bootstrap_5_layout.html.twig']), 'bootstrap_5');
assert.equal(rendererOf('tailwind_2_layout.html.twig'), 'tailwind_2');
assert.equal(rendererOf([]), 'div');
assert.equal(constraintMessage({ '@new': V + 'Length', args: { min: 3 } }), 'This value is too short. It should have 3 characters or more.');
assert.equal(phpOf({ '@new': V + 'Length', args: { min: 3, max: 9 } }), 'new Length(min: 3, max: 9)');
assert.ok(optionsOf(CORE + 'CollectionType').includes('allow_add'));
assert.ok(optionsOf('App\\Form\\AddressType').includes('by_reference'));
});
test('form_div_layout: labels, names, ids, help, csrf, sub-forms, collections', () => {
const html = formHtml(model, { theme: 'form_div_layout.html.twig', subforms, enums });
assert.match(html, /^<form name="user_profile" method="post">\n<div id="user_profile">/);
assert.ok(html.includes('<div><label for="user_profile_firstName" class="required">First name</label><input type="text" id="user_profile_firstName" name="user_profile[firstName]" required="required" aria-describedby="user_profile_firstName_help" placeholder="Ada"><div id="user_profile_firstName_help" class="help-text">As on your passport</div></div>'));
assert.ok(html.includes('<label for="user_profile_bio">Bio</label><textarea id="user_profile_bio" name="user_profile[bio]"></textarea>'), 'not required');
assert.ok(html.includes('<input type="radio" id="user_profile_size_0" name="user_profile[size]" required="required" value="s"><label for="user_profile_size_0" class="required">Small</label>'));
assert.ok(html.includes('<option value="" disabled selected>Choose</option><option value="France">France</option>'));
assert.ok(html.includes('<option value="active">Active</option><option value="banned">Banned</option>'));
assert.ok(html.includes('<select id="user_profile_team" name="user_profile[team][]" required="required" multiple="multiple"><option value="1">Team 1 (name)</option>'));
assert.ok(html.includes('€ <input type="text" id="user_profile_salary"'));
assert.ok(html.includes('<input type="date" id="user_profile_born"'));
assert.ok(html.includes('<input type="checkbox" id="user_profile_newsletter" name="user_profile[newsletter]" value="1"><label for="user_profile_newsletter">Newsletter</label>'));
assert.ok(html.includes('<label for="user_profile_password_first" class="required">Password</label><input type="password" id="user_profile_password_first" name="user_profile[password][first]"'));
assert.ok(html.includes('<label for="user_profile_password_second" class="required">Repeat</label>'));
assert.ok(html.includes('<div><label class="required">Address</label><div id="user_profile_address">\n<div><label for="user_profile_address_city" class="required">City</label>'));
assert.ok(html.includes('data-prototype="&lt;div&gt;&lt;label for=&quot;user_profile_links___name__&quot;'), 'the prototype');
assert.ok(html.includes('<input type="text" id="user_profile_links_0" name="user_profile[links][0]" required="required" inputmode="url">'));
assert.ok(html.includes('<input type="hidden" id="user_profile_token" name="user_profile[token]">'));
assert.ok(html.includes('<div><button type="submit" id="user_profile_save" name="user_profile[save]">Save it</button></div>'));
assert.ok(html.includes('<input type="hidden" id="user_profile__token" name="user_profile[_token]" value="csrf-token">'));
assert.ok(!html.includes('<ul>'), 'no errors unless asked');
assert.ok(!formHtml({ ...model, defaults: { csrf_protection: false } }).includes('[_token]'));
});
test('form_div_layout with errors: the list before the widget', () => {
const html = formHtml(model, { theme: 'div', errors: true, subforms, enums });
assert.ok(html.includes('<label for="user_profile_email" class="required">Email</label><ul><li>Not an e-mail.</li></ul><input type="email"'));
assert.ok(html.includes('<ul><li>This value should not be blank.</li></ul><input type="text" id="user_profile_firstName"'));
assert.ok(!html.includes('<label for="user_profile_bio">Bio</label><ul>'), 'an optional field has nothing to say');
});
test('bootstrap_5_layout: its classes, input groups, fieldsets, is-invalid and invalid-feedback', () => {
const html = formHtml(model, { theme: ['bootstrap_5_layout.html.twig'], subforms, enums });
assert.ok(html.includes('<div class="mb-3"><label for="user_profile_firstName" class="form-label required">First name</label><input type="text" id="user_profile_firstName" name="user_profile[firstName]" required="required" aria-describedby="user_profile_firstName_help" placeholder="Ada" class="form-control"><div id="user_profile_firstName_help" class="form-text mb-0 help-text">As on your passport</div></div>'));
assert.ok(html.includes('<fieldset class="mb-3"><legend class="col-form-label required">Size</legend><div id="user_profile_size"><div class="form-check"><input type="radio"'));
assert.ok(html.includes('class="form-select"'));
assert.ok(html.includes('<div class="input-group"><span class="input-group-text">€</span><input type="text" id="user_profile_salary"'));
assert.ok(html.includes('<div class="mb-3"><div class="form-check"><input type="checkbox" id="user_profile_newsletter" name="user_profile[newsletter]" value="1" class="form-check-input"><label for="user_profile_newsletter" class="form-check-label">Newsletter</label></div></div>'));
assert.ok(html.includes('<button type="submit" id="user_profile_save" name="user_profile[save]" class="btn-primary btn">Save it</button>'));
const errors = formHtml(model, { theme: 'bootstrap_5', errors: true, subforms, enums });
assert.ok(errors.includes('class="form-control is-invalid"><div class="invalid-feedback d-block">Not an e-mail.</div></div>'));
assert.ok(errors.includes('<div class="input-group has-validation"><span class="input-group-text">€</span>'));
});
test('tailwind_2_layout: its utility classes and red errors', () => {
const html = formHtml(model, { theme: 'tailwind_2_layout.html.twig', errors: true, subforms, enums });
assert.ok(html.includes('<div class="mb-6"><label for="user_profile_email" class="block text-gray-800 required">Email</label><input type="email" id="user_profile_email" name="user_profile[email]" required="required" class="mt-1 w-full border-2 border-red-700"><ul><li class="text-red-700">Not an e-mail.</li></ul></div>'));
assert.ok(html.includes('<p id="user_profile_firstName_help" class="mt-1 text-gray-600">As on your passport</p>'));
});
test('form_table_layout: a table, a row per field', () => {
const html = formHtml(model, { theme: 'form_table_layout.html.twig', subforms, enums });
assert.ok(html.includes('<table id="user_profile">'));
assert.ok(html.includes('<tr><td><label for="user_profile_firstName" class="required">First name</label></td><td><input type="text"'));
assert.ok(html.includes('<tr><td></td><td><button type="submit"'));
});
test('a file field makes a multipart form; stylesheets and the page around it', () => {
assert.match(formHtml({ class: 'App\\Form\\UploadType', fields: [{ name: 'doc', type: CORE + 'FileType', options: {} }] }), /enctype="multipart\/form-data"/);
assert.deepEqual(themeStylesheets('bootstrap_5_layout.html.twig', 'assets/vendor/bootstrap/dist/css/bootstrap.min.css'), ['/assets/vendor/bootstrap/dist/css/bootstrap.min.css']);
assert.match(themeStylesheets('bootstrap_5')[0], /^https:\/\/cdn\.jsdelivr\.net\/npm\/bootstrap@5\.3\.3\//);
assert.deepEqual(themeStylesheets('form_div_layout.html.twig'), []);
assert.match(standalonePage('<form></form>', 'T', ['/x.css']), /<link rel="stylesheet" href="\/x\.css"><\/head><body><main[^>]*><form><\/form>/);
});