Skip to content

Commit

Permalink
fix(cdk): optional chaining for tuiControlValue subscription (#9865)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Zezin <[email protected]>
  • Loading branch information
dequubi and Alexey Zezin authored Dec 3, 2024
1 parent eb71369 commit e77b631
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 44 deletions.
12 changes: 4 additions & 8 deletions projects/cdk/observables/control-value.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,11 +8,8 @@ import {startWith} from 'rxjs/operators';
export function tuiControlValue<T>(
control: AbstractControl | AbstractControlDirective,
): Observable<T> {
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),
);
}
37 changes: 1 addition & 36 deletions projects/cdk/observables/test/control-value.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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('');
}));
});
});

0 comments on commit e77b631

Please sign in to comment.