0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-25 05:02:57 +01:00

Chooser modal - update spinner logic on error

- When errors occur on chooser modal content, find the local progress buttons that are in loading state and update the value
- Stimulus will automatically resolve all DOM changes based on the dynamic value update
- Add unit tests for functionality
- Fixes #10498
- Regression from #9910 & #10062
This commit is contained in:
LB Johnston 2023-06-02 08:32:44 +10:00 committed by Matt Westcott
parent 35de8a98b7
commit 3ce2b5f007
3 changed files with 72 additions and 3 deletions

View File

@ -69,7 +69,7 @@ export class ProgressController extends Controller<HTMLButtonElement> {
window.cancelSpinner = () => {
const attr = `data-${identifier}-loading-value`;
document.querySelectorAll(`[${attr}="true"]`).forEach((element) => {
document.querySelectorAll(`[${attr}~="true"]`).forEach((element) => {
element.removeAttribute(attr);
});
};

View File

@ -25,8 +25,13 @@ const validateCreationForm = (form) => {
}
});
if (hasErrors) {
// eslint-disable-next-line no-undef
setTimeout(cancelSpinner, 500);
setTimeout(() => {
// clear any loading state on progress buttons
const attr = 'data-w-progress-loading-value';
form.querySelectorAll(`[${attr}~="true"]`).forEach((element) => {
element.removeAttribute(attr);
});
}, 500);
}
return !hasErrors;
};

View File

@ -0,0 +1,64 @@
import { validateCreationForm } from './chooserModal';
jest.useFakeTimers();
describe('chooserModal', () => {
describe('validateCreationForm', () => {
let form;
beforeEach(() => {
document.body.innerHTML = `
<form id="form">
<div data-field>
<div data-field-errors></div>
<input id="input" required type="text" />
</div>
<button id="button" type="submit" data-controller="w-progress" data-w-progress-loading-value="true">Update</button>
</form>
`;
form = document.getElementById('form');
});
afterEach(() => {
jest.runAllTimers();
});
it('should update the aria attribute on invalid fields', () => {
const input = document.getElementById('input');
expect(input.getAttribute('aria-invalid')).toBeFalsy();
validateCreationForm(form);
expect(input.getAttribute('aria-invalid')).toBeTruthy();
});
it('should append an error message', () => {
expect(form.querySelector('.error-message')).toBeFalsy();
validateCreationForm(form);
expect(form.querySelector('.error-message').innerHTML).toEqual(
'This field is required.',
);
});
it('should clear any in progress buttons', () => {
const button = document.getElementById('button');
validateCreationForm(form);
expect({ ...button.dataset }).toEqual({
controller: 'w-progress',
wProgressLoadingValue: 'true',
});
jest.runAllTimers();
expect({ ...button.dataset }).toEqual({
controller: 'w-progress',
});
});
});
});