kopia lustrzana https://github.com/wagtail/wagtail
CleanController - Add identity action
This allows the comparison to run and always return truepull/12554/head
rodzic
c010a1e5eb
commit
8751ac27eb
|
@ -207,6 +207,56 @@ describe('CleanController', () => {
|
|||
|
||||
expect(event.preventDefault).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should allow the compare as identity to ensure that the values are always considered equal', async () => {
|
||||
expect(events['w-clean:applied']).toHaveLength(0);
|
||||
|
||||
const input = document.getElementById('slug');
|
||||
input.setAttribute('data-w-clean-compare-as-param', 'identity');
|
||||
|
||||
input.value = 'title-alpha';
|
||||
|
||||
const event = new CustomEvent('custom:event', {
|
||||
detail: { value: 'title beta' },
|
||||
});
|
||||
|
||||
event.preventDefault = jest.fn();
|
||||
|
||||
input.dispatchEvent(event);
|
||||
|
||||
await new Promise(process.nextTick);
|
||||
|
||||
expect(event.preventDefault).not.toHaveBeenCalled();
|
||||
expect(events['w-clean:applied']).toHaveLength(1);
|
||||
expect(events['w-clean:applied']).toHaveProperty('0.detail', {
|
||||
action: 'identity',
|
||||
cleanValue: 'title-alpha',
|
||||
sourceValue: 'title-alpha',
|
||||
});
|
||||
|
||||
// now use the compare from the event detail
|
||||
input.removeAttribute('data-w-clean-compare-as-param');
|
||||
|
||||
input.value = 'title-delta';
|
||||
|
||||
const event2 = new CustomEvent('custom:event', {
|
||||
detail: { value: 'title whatever', compareAs: 'identity' },
|
||||
});
|
||||
|
||||
event2.preventDefault = jest.fn();
|
||||
|
||||
input.dispatchEvent(event2);
|
||||
|
||||
await new Promise(process.nextTick);
|
||||
|
||||
expect(event2.preventDefault).not.toHaveBeenCalled();
|
||||
expect(events['w-clean:applied']).toHaveLength(2);
|
||||
expect(events['w-clean:applied']).toHaveProperty('1.detail', {
|
||||
action: 'identity',
|
||||
cleanValue: 'title-delta',
|
||||
sourceValue: 'title-delta',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('slugify', () => {
|
||||
|
|
|
@ -3,6 +3,7 @@ import { slugify } from '../utils/slugify';
|
|||
import { urlify } from '../utils/urlify';
|
||||
|
||||
enum Actions {
|
||||
Identity = 'identity',
|
||||
Slugify = 'slugify',
|
||||
Urlify = 'urlify',
|
||||
}
|
||||
|
@ -97,6 +98,18 @@ export class CleanController extends Controller<HTMLInputElement> {
|
|||
return new Set(values.map((value: string) => `${value}`)).size === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element's value as is, without any modifications.
|
||||
* Useful for identity fields or when no cleaning is required but the event
|
||||
* is needed or comparison is required to always pass.
|
||||
*/
|
||||
identity() {
|
||||
const action = Actions.Identity;
|
||||
const value = this.element.value;
|
||||
this.applyUpdate(action, value, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the value before being processed by an action method.
|
||||
*/
|
||||
|
|
Ładowanie…
Reference in New Issue