|
| 1 | +import {Component, model, provideZonelessChangeDetection, signal} from '@angular/core'; |
| 2 | +import {TestBed} from '@angular/core/testing'; |
| 3 | +import {form} from '../src/api/structure'; |
| 4 | +import {Control} from '../src/controls/control'; |
| 5 | +import {FormValueControl} from '../public_api'; |
| 6 | + |
| 7 | +fdescribe('control directive', () => { |
| 8 | + beforeEach(() => { |
| 9 | + TestBed.configureTestingModule({ |
| 10 | + providers: [provideZonelessChangeDetection()], |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + it('synchronizes a basic form with a custom control', () => { |
| 15 | + @Component({ |
| 16 | + imports: [Control], |
| 17 | + template: ` |
| 18 | + <input [control]="f"> |
| 19 | + `, |
| 20 | + }) |
| 21 | + class TestCmp { |
| 22 | + f = form<string>(signal('test')); |
| 23 | + } |
| 24 | + |
| 25 | + const fix = act(() => TestBed.createComponent(TestCmp)); |
| 26 | + const input = fix.nativeElement.firstChild as HTMLInputElement; |
| 27 | + const cmp = fix.componentInstance as TestCmp; |
| 28 | + |
| 29 | + // Initial state |
| 30 | + expect(input.value).toBe('test'); |
| 31 | + |
| 32 | + // Model -> View |
| 33 | + act(() => cmp.f().value.set('testing')); |
| 34 | + expect(input.value).toBe('testing'); |
| 35 | + |
| 36 | + // View -> Model |
| 37 | + act(() => { |
| 38 | + input.value = 'typing'; |
| 39 | + input.dispatchEvent(new Event('input')); |
| 40 | + }); |
| 41 | + expect(cmp.f().value()).toBe('typing'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('synchronizes with a checkbox control', () => { |
| 45 | + @Component({ |
| 46 | + imports: [Control], |
| 47 | + template: `<input type="checkbox" [control]="f">`, |
| 48 | + }) |
| 49 | + class TestCmp { |
| 50 | + f = form(signal(false)); |
| 51 | + } |
| 52 | + |
| 53 | + const fix = act(() => TestBed.createComponent(TestCmp)); |
| 54 | + const input = fix.nativeElement.firstChild as HTMLInputElement; |
| 55 | + const cmp = fix.componentInstance as TestCmp; |
| 56 | + |
| 57 | + // Initial state |
| 58 | + expect(input.checked).toBe(false); |
| 59 | + |
| 60 | + // Model -> View |
| 61 | + act(() => cmp.f().value.set(true)); |
| 62 | + expect(input.checked).toBe(true); |
| 63 | + |
| 64 | + // View -> Model |
| 65 | + act(() => { |
| 66 | + input.checked = false; |
| 67 | + input.dispatchEvent(new Event('input')); |
| 68 | + }); |
| 69 | + expect(cmp.f().value()).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it('synchronizes with a radio group', () => { |
| 73 | + const {cmp, expectStates, inputA, inputB, inputC} = setupRadioGroup(); |
| 74 | + |
| 75 | + // All the inputs should have the same name. |
| 76 | + expect(inputA.name).toBe('test'); |
| 77 | + expect(inputB.name).toBe('test'); |
| 78 | + expect(inputC.name).toBe('test'); |
| 79 | + |
| 80 | + // Model -> View |
| 81 | + act(() => cmp.f().value.set('c')); |
| 82 | + expect(inputA.checked).toBeFalse(); |
| 83 | + expect(inputB.checked).toBeFalse(); |
| 84 | + expect(inputC.checked).toBeTrue(); |
| 85 | + |
| 86 | + // View -> Model |
| 87 | + act(() => { |
| 88 | + inputB.click(); |
| 89 | + expect(inputB.checked).toBeTrue(); |
| 90 | + }); |
| 91 | + expect(cmp.f().value()).toBe('c'); |
| 92 | + }); |
| 93 | + |
| 94 | + fit('synchronizes with a custom control', () => { |
| 95 | + @Component({ |
| 96 | + selector: 'my-input', |
| 97 | + template: '', |
| 98 | + }) |
| 99 | + class CustomInput implements FormValueControl<string> { |
| 100 | + value = model.required<string>(); |
| 101 | + |
| 102 | + ngOnInit(): void { |
| 103 | + // console.log('CustomInput.ngOnInit'); |
| 104 | + this.value(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Component({ |
| 109 | + imports: [Control, CustomInput], |
| 110 | + template: `<my-input [control]="f" />`, |
| 111 | + }) |
| 112 | + class TestCmp { |
| 113 | + f = form<string>(signal('test')); |
| 114 | + } |
| 115 | + |
| 116 | + const fix = act(() => TestBed.createComponent(TestCmp)); |
| 117 | + const input = fix.nativeElement.firstChild as HTMLInputElement; |
| 118 | + const cmp = fix.componentInstance as TestCmp; |
| 119 | + |
| 120 | + // Initial state |
| 121 | + expect(input.value).toBe('test'); |
| 122 | + |
| 123 | + // Model -> View |
| 124 | + act(() => cmp.f().value.set('testing')); |
| 125 | + expect(input.value).toBe('testing'); |
| 126 | + |
| 127 | + // View -> Model |
| 128 | + act(() => { |
| 129 | + input.value = 'typing'; |
| 130 | + input.dispatchEvent(new Event('input')); |
| 131 | + }); |
| 132 | + expect(cmp.f().value()).toBe('typing'); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +function setupRadioGroup() { |
| 137 | + @Component({ |
| 138 | + imports: [Control], |
| 139 | + template: ` |
| 140 | + <form> |
| 141 | + <input type="radio" value="a" [control]="f"> |
| 142 | + <input type="radio" value="b" [control]="f"> |
| 143 | + <input type="radio" value="c" [control]="f"> |
| 144 | + </form> |
| 145 | + `, |
| 146 | + }) |
| 147 | + class TestCmp { |
| 148 | + f = form(signal('a'), { |
| 149 | + name: 'test', |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + const fix = act(() => TestBed.createComponent(TestCmp)); |
| 154 | + const formEl = (fix.nativeElement as HTMLElement).firstChild as HTMLFormElement; |
| 155 | + const inputs = Array.from(formEl.children) as HTMLInputElement[]; |
| 156 | + |
| 157 | + // A fix for Domino issues with <form> around <input>. |
| 158 | + for (const input of inputs) { |
| 159 | + Object.defineProperty(input, 'form', {get: () => formEl}); |
| 160 | + } |
| 161 | + |
| 162 | + const [inputA, inputB, inputC] = inputs; |
| 163 | + const cmp = fix.componentInstance as TestCmp; |
| 164 | + |
| 165 | + function expectStates(a: boolean, b: boolean, c: boolean): void { |
| 166 | + expect(inputA.checked).toBe(a); |
| 167 | + expect(inputB.checked).toBe(b); |
| 168 | + expect(inputC.checked).toBe(c); |
| 169 | + } |
| 170 | + |
| 171 | + return {cmp, expectStates, inputA, inputB, inputC}; |
| 172 | +} |
| 173 | + |
| 174 | +function act<T>(fn: () => T): T { |
| 175 | + try { |
| 176 | + return fn(); |
| 177 | + } finally { |
| 178 | + TestBed.tick(); |
| 179 | + } |
| 180 | +} |
0 commit comments