lion/packages/ui/components/form-integrations/test/form-group-methods.test.js
gerjanvangeest d2de984f0b
Feat/input file (#1881)
* feat(input-file): create input-file component

* chore: improvements after review

* chore: update after review

* chore: update translations

* chore: - fixed demo with form submit, submit was not prevented
       - fixed checking allowed file extensions
       - fixed clicking on select file button in drag and drop area

* chore: since the input-file does not upload files itself but enables user to select files, I replaced "upload" and "upload" with "select" and "selected" where applicable

* chore: - removed unused properties allowedFileTypes and allowedFileExtensions from lion-input-file
       - cleaned up docs

* chore: - changed type Array.<type> to Array<type>
       - removed redundant type definition

* fix: - FocusMixin: moved registering events for from connectedCallback to firstUpdated since _focusableNode is sometimes not available yet
     - SlotMixin: changed updated to update in since slots were rendered too late (related to previous fix in FocusMixin.js)

* fix: renamed lion-uploaded-file-list.js to lion-selected-file-list.js

* fix: fixed test for lion-selected-file-list

* fix: fixed typ

* wip

* fix: - fixed issue with multiple file selection where element would not select valid files after invalid ones
     - added getMessage method to FileValidation that returns empty string to prevent message being shown that error message must be configured
     - fixed tests

* chore: replaced `uploadOnFormSubmit` with `uploadOnSelect` and flipped the default value to false. When `uploadOnSelect` is set to true, the file will be uploaded as soon as it is selected.

* fix: - replaced `uploadOnFormSubmit` with `uploadOnSelect` and flipped the default value to false. When `uploadOnSelect` is set to true, the file will be uploaded as soon as it is selected.
     - fixed issue where a valid file was not selected and added to the file list if it was preceded by an invalid file

* chore: removed redundant README.md

* fix: fixed failing test

* chore: added missing type annotation

* chore: annotated event param as optional

---------

Co-authored-by: Danny Moerkerke <danny.moerkerke@ing.com>
Co-authored-by: Thijs Louisse <Thijs.Louisse@ing.com>
2023-06-06 11:30:43 +02:00

179 lines
5.1 KiB
JavaScript

import { elementUpdated, expect, fixture } from '@open-wc/testing';
import { html } from 'lit';
import { getAllFieldsAndFormGroups } from './helpers/helpers.js';
import './helpers/umbrella-form.js';
/**
* @typedef {import('../../form-core/src/LionField.js').LionField} LionField
* @typedef {import('../../input-file/types/input-file.js').InputFile} InputFile
* @typedef {import('../../button/src/LionButton.js').LionButton} LionButton
* @typedef {import('./helpers/umbrella-form.js').UmbrellaForm} UmbrellaForm
*/
const file = /** @type {InputFile} */ (
new File(['foo'], 'foo.txt', {
type: 'text/plain',
})
);
const fullyPrefilledSerializedValue = {
fullName: { firstName: 'Lorem', lastName: 'Ipsum' },
date: '2000-12-12',
datepicker: '2020-12-12',
bio: 'Lorem',
money: '12313.12',
iban: '123456',
email: 'a@b.com',
checkers: ['foo', 'bar'],
dinosaurs: 'brontosaurus',
favoriteFruit: 'Banana',
favoriteMovie: 'Rocky',
favoriteColor: 'hotpink',
file: [],
lyrics: '1',
notifications: {
checked: true,
value: 'Lorem',
},
range: 2.3,
rsvp: 'Lorem',
terms: ['agreed'],
comments: 'Lorem',
};
const fullyChangedSerializedValue = {
fullName: { firstName: 'LoremChanged', lastName: 'IpsumChanged' },
date: '1999-12-12',
datepicker: '1986-12-12',
bio: 'LoremChanged',
money: '9912313.12',
iban: '99123456',
email: 'aChanged@b.com',
checkers: ['foo'],
dinosaurs: '',
favoriteFruit: '',
favoriteMovie: '',
favoriteColor: '',
file: [file],
lyrics: '2',
notifications: {
checked: false,
value: 'Lorem',
},
range: 3.3,
rsvp: 'LoremChanged',
terms: [],
comments: 'LoremChanged',
};
describe(`Submitting/Resetting/Clearing Form`, async () => {
it('pressing submit button of a form should make submitted true for all fields', async () => {
const el = /** @type {UmbrellaForm} */ (await fixture(html`<umbrella-form></umbrella-form>`));
await el.updateComplete;
await el.waitForAllChildrenUpdates();
const formEl = el._lionFormNode;
const allElements = getAllFieldsAndFormGroups(formEl);
allElements.forEach((/** @type {LionField} */ field) => {
// TODO: prefer submitted 'false' over 'undefined'
expect(Boolean(field.submitted)).to.be.false;
});
/** @type {LionButton} */ (formEl.querySelector('#submit_button')).click();
await elementUpdated(formEl);
await el.updateComplete;
allElements.forEach((/** @type {LionField} */ field) => {
expect(field.submitted).to.be.true;
});
});
it('calling resetGroup() should reset all metadata (interaction states and initial values)', async () => {
const el = /** @type {UmbrellaForm} */ (
await fixture(
html`<umbrella-form .serializedValue="${fullyPrefilledSerializedValue}"></umbrella-form>`,
)
);
await el.waitForAllChildrenUpdates();
await el.updateComplete;
const formEl = el._lionFormNode;
/** @type {LionButton} */ (formEl.querySelector('#submit_button')).click();
await elementUpdated(formEl);
await formEl.updateComplete;
const allElements = getAllFieldsAndFormGroups(formEl);
allElements.forEach((/** @type {LionField} */ field) => {
// eslint-disable-next-line no-param-reassign
field.touched = true;
// eslint-disable-next-line no-param-reassign
field.dirty = true;
});
formEl.serializedValue = fullyChangedSerializedValue;
allElements.forEach((/** @type {LionField} */ field) => {
expect(field.submitted).to.be.true;
expect(field.touched).to.be.true;
expect(field.dirty).to.be.true;
});
/** @type {LionButton} */ (formEl.querySelector('#reset_button')).click();
await elementUpdated(formEl);
await formEl.updateComplete;
expect(formEl.submitted).to.be.false;
allElements.forEach((/** @type {LionField} */ field) => {
expect(field.submitted).to.be.false;
expect(field.touched).to.be.false;
expect(field.dirty).to.be.false;
});
// TODO: investigate why this doesn't work
// expect(formEl.serializedValue).to.eql(fullyPrefilledSerializedValue);
});
// Wait till ListboxMixin properly clears
it('calling clearGroup() should clear all fields', async () => {
const el = /** @type {UmbrellaForm} */ (
await fixture(
html`<umbrella-form .serializedValue="${fullyPrefilledSerializedValue}"></umbrella-form>`,
)
);
await el.waitForAllChildrenUpdates();
await el.updateComplete;
const formEl = el._lionFormNode;
formEl.clearGroup();
await elementUpdated(formEl);
await formEl.updateComplete;
expect(formEl.serializedValue).to.eql({
fullName: { firstName: '', lastName: '' },
date: '',
datepicker: '',
bio: '',
money: '',
iban: '',
email: '',
checkers: [],
dinosaurs: '',
favoriteFruit: '',
favoriteMovie: '',
favoriteColor: '',
file: [],
lyrics: '',
notifications: {
checked: false,
value: 'Lorem',
},
range: '',
rsvp: '',
tel: '',
'tel-dropdown': '',
terms: [],
comments: '',
});
});
});