From e77b6315f34494b9cb7aed41fff8df064ba12bb7 Mon Sep 17 00:00:00 2001 From: Alexey <47535336+dequubi@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:40:25 +0300 Subject: [PATCH] fix(cdk): optional chaining for tuiControlValue subscription (#9865) Co-authored-by: Alexey Zezin --- projects/cdk/observables/control-value.ts | 12 ++---- .../observables/test/control-value.spec.ts | 37 +------------------ 2 files changed, 5 insertions(+), 44 deletions(-) diff --git a/projects/cdk/observables/control-value.ts b/projects/cdk/observables/control-value.ts index 4dc605ba8c43..9017ff5d331a 100644 --- a/projects/cdk/observables/control-value.ts +++ b/projects/cdk/observables/control-value.ts @@ -1,5 +1,4 @@ import {AbstractControl, AbstractControlDirective} from '@angular/forms'; -import {TuiValueChangesException} from '@taiga-ui/cdk/exceptions'; import {Observable} from 'rxjs'; import {startWith} from 'rxjs/operators'; @@ -9,11 +8,8 @@ import {startWith} from 'rxjs/operators'; export function tuiControlValue( control: AbstractControl | AbstractControlDirective, ): Observable { - return new Observable(subscriber => { - if (!control.valueChanges) { - throw new TuiValueChangesException(); - } - - return control.valueChanges.pipe(startWith(control.value)).subscribe(subscriber); - }); + return new Observable( + subscriber => + control?.valueChanges?.pipe(startWith(control.value)).subscribe(subscriber), + ); } diff --git a/projects/cdk/observables/test/control-value.spec.ts b/projects/cdk/observables/test/control-value.spec.ts index 5b5da1f22278..75cda1d3293e 100644 --- a/projects/cdk/observables/test/control-value.spec.ts +++ b/projects/cdk/observables/test/control-value.spec.ts @@ -1,7 +1,6 @@ import {fakeAsync} from '@angular/core/testing'; -import {AbstractControl, FormControl} from '@angular/forms'; +import {FormControl} from '@angular/forms'; import {tuiControlValue} from '@taiga-ui/cdk'; -import {tuiSwitchNgDevMode} from '@taiga-ui/testing'; import {skip} from 'rxjs/operators'; describe('tuiControlValue', () => { @@ -30,38 +29,4 @@ describe('tuiControlValue', () => { expect(actual).toBe('test'); })); - - describe('dev mode', () => { - beforeEach(() => tuiSwitchNgDevMode(true)); - - it('throws an error if there is no valueChanges', fakeAsync(() => { - let actual = ''; - - tuiControlValue({} as AbstractControl).subscribe({ - next: () => {}, - error: (err: unknown) => { - actual = (err as Error).message; - }, - }); - - expect(actual).toBe('Control does not have valueChanges'); - })); - - afterEach(() => tuiSwitchNgDevMode(false)); - }); - - describe('production mode', () => { - it('throws an error if there is no valueChanges', fakeAsync(() => { - let actual = ''; - - tuiControlValue({} as AbstractControl).subscribe({ - next: () => {}, - error: (err: unknown) => { - actual = (err as Error).message; - }, - }); - - expect(actual).toBe(''); - })); - }); });