|
9 | 9 | import {ApplicationRef, Injector, resource, signal} from '@angular/core'; |
10 | 10 | import {TestBed} from '@angular/core/testing'; |
11 | 11 | import { |
| 12 | + disabled, |
12 | 13 | form, |
13 | 14 | required, |
14 | 15 | requiredError, |
15 | 16 | submit, |
16 | 17 | validateAsync, |
17 | 18 | ValidationError, |
| 19 | + hidden, |
| 20 | + readonly, |
18 | 21 | } from '../../public_api'; |
19 | 22 |
|
20 | 23 | describe('submit', () => { |
@@ -361,6 +364,81 @@ describe('submit', () => { |
361 | 364 | expect(f.first().errors()).toEqual([]); |
362 | 365 | expect(f.last().errors()).toEqual([{kind: 'submit', fieldTree: f.last}]); |
363 | 366 | }); |
| 367 | + |
| 368 | + it('does not mark disabled fields as touched', async () => { |
| 369 | + const data = signal({first: '', last: ''}); |
| 370 | + const f = form( |
| 371 | + data, |
| 372 | + (name) => { |
| 373 | + // Disable first name when last name is empty. |
| 374 | + disabled(name.first, ({valueOf}) => valueOf(name.last) === ''); |
| 375 | + }, |
| 376 | + {injector: TestBed.inject(Injector)}, |
| 377 | + ); |
| 378 | + |
| 379 | + expect(f.first().disabled()).toBe(true); |
| 380 | + expect(f.first().touched()).toBe(false); |
| 381 | + expect(f.last().touched()).toBe(false); |
| 382 | + |
| 383 | + await submit(f, async () => []); |
| 384 | + expect(f.first().touched()).toBe(false); |
| 385 | + expect(f.last().touched()).toBe(true); |
| 386 | + |
| 387 | + // Set last name to make first name enabled. |
| 388 | + f.last().value.set('Doe'); |
| 389 | + expect(f.first().disabled()).toBe(false); |
| 390 | + expect(f.first().touched()).toBe(false); |
| 391 | + }); |
| 392 | + |
| 393 | + it('does not mark hidden fields as touched', async () => { |
| 394 | + const data = signal({first: '', last: ''}); |
| 395 | + const f = form( |
| 396 | + data, |
| 397 | + (name) => { |
| 398 | + // Hide first name when last name is empty. |
| 399 | + hidden(name.first, ({valueOf}) => valueOf(name.last) === ''); |
| 400 | + }, |
| 401 | + {injector: TestBed.inject(Injector)}, |
| 402 | + ); |
| 403 | + |
| 404 | + expect(f.first().hidden()).toBe(true); |
| 405 | + expect(f.first().touched()).toBe(false); |
| 406 | + expect(f.last().touched()).toBe(false); |
| 407 | + |
| 408 | + await submit(f, async () => []); |
| 409 | + expect(f.first().touched()).toBe(false); |
| 410 | + expect(f.last().touched()).toBe(true); |
| 411 | + |
| 412 | + // Set last name to make first name visible. |
| 413 | + f.last().value.set('Doe'); |
| 414 | + expect(f.first().hidden()).toBe(false); |
| 415 | + expect(f.first().touched()).toBe(false); |
| 416 | + }); |
| 417 | + |
| 418 | + it('does not mark readonly fields as touched', async () => { |
| 419 | + const data = signal({first: '', last: ''}); |
| 420 | + const f = form( |
| 421 | + data, |
| 422 | + (name) => { |
| 423 | + // Make first name readonly when last name is empty. |
| 424 | + readonly(name.first, ({valueOf}) => valueOf(name.last) === ''); |
| 425 | + }, |
| 426 | + {injector: TestBed.inject(Injector)}, |
| 427 | + ); |
| 428 | + |
| 429 | + expect(f.first().readonly()).toBe(true); |
| 430 | + expect(f.first().touched()).toBe(false); |
| 431 | + expect(f.last().touched()).toBe(false); |
| 432 | + |
| 433 | + await submit(f, async () => []); |
| 434 | + expect(f.first().touched()).toBe(false); |
| 435 | + expect(f.last().touched()).toBe(true); |
| 436 | + |
| 437 | + // Set last name to make first name enabled. |
| 438 | + f.last().value.set('Doe'); |
| 439 | + expect(f.first().readonly()).toBe(false); |
| 440 | + expect(f.first().touched()).toBe(false); |
| 441 | + }); |
364 | 442 | }); |
365 | 443 |
|
366 | 444 | /** |
|
0 commit comments