|
| 1 | +import { |
| 2 | + afterEach, describe, expect, it, |
| 3 | +} from '@jest/globals'; |
| 4 | +import { data } from '@js/core/element_data'; |
| 5 | +import $ from '@js/core/renderer'; |
| 6 | +import Validator from '@ts/ui/validator'; |
| 7 | + |
| 8 | +import type { EditorProperties } from '../editor'; |
| 9 | +import Editor from '../editor'; |
| 10 | + |
| 11 | +const VALIDATION_TARGET = 'dx-validation-target'; |
| 12 | + |
| 13 | +interface ValidationRequestArgs { |
| 14 | + value: unknown; |
| 15 | + editor: unknown; |
| 16 | +} |
| 17 | + |
| 18 | +const disposables: { dispose: () => void }[] = []; |
| 19 | + |
| 20 | +const createEditor = (options: Partial<EditorProperties> = {}): Editor => { |
| 21 | + const element = $('<div>').appendTo(document.body).get(0) as HTMLElement; |
| 22 | + // @ts-expect-error DOMComponent constructor is not typed for direct instantiation |
| 23 | + const instance: Editor = new Editor(element, options); |
| 24 | + |
| 25 | + disposables.push(instance); |
| 26 | + |
| 27 | + return instance; |
| 28 | +}; |
| 29 | + |
| 30 | +describe('Editor initialization', () => { |
| 31 | + afterEach(() => { |
| 32 | + disposables.forEach((instance) => instance.dispose()); |
| 33 | + disposables.length = 0; |
| 34 | + document.body.innerHTML = ''; |
| 35 | + }); |
| 36 | + |
| 37 | + it('lets a validator attach while the editor initialization is deferred by beginUpdate', () => { |
| 38 | + const instance = createEditor({ |
| 39 | + onInitializing(this: Editor): void { |
| 40 | + this.beginUpdate(); |
| 41 | + }, |
| 42 | + } as Partial<EditorProperties>); |
| 43 | + |
| 44 | + expect(() => { |
| 45 | + // @ts-expect-error DOMComponent constructor is not typed for direct instantiation |
| 46 | + const validator: Validator = new Validator(instance.$element().get(0), {}); |
| 47 | + |
| 48 | + disposables.push(validator); |
| 49 | + }).not.toThrow(); |
| 50 | + |
| 51 | + instance.endUpdate(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('marks the element as a validation target with the validation state ready', () => { |
| 55 | + const instance = createEditor(); |
| 56 | + const element = instance.$element().get(0) as HTMLElement; |
| 57 | + |
| 58 | + expect(data(element, VALIDATION_TARGET)).toBe(instance); |
| 59 | + expect(instance.validationRequest).toBeDefined(); |
| 60 | + expect(instance.showValidationMessageTimeout).toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('fires validationRequest when the value changes', () => { |
| 64 | + const instance = createEditor({ value: 'initial' }); |
| 65 | + const calls: ValidationRequestArgs[] = []; |
| 66 | + |
| 67 | + instance.validationRequest.add((args: ValidationRequestArgs) => { |
| 68 | + calls.push(args); |
| 69 | + }); |
| 70 | + instance.option('value', 'changed'); |
| 71 | + |
| 72 | + expect(calls).toHaveLength(1); |
| 73 | + expect(calls[0].value).toBe('changed'); |
| 74 | + expect(calls[0].editor).toBe(instance); |
| 75 | + }); |
| 76 | +}); |
0 commit comments