Skip to content

Commit

Permalink
feat(ng-core): Rename WrappedControlSuperclass.outerToInner and sim…
Browse files Browse the repository at this point in the history
…ilar to `.outerToInnerValues` and similar, to avoid confusion with `.outerToInnerErrors`

BREAKING CHANGE: Subclasses that override one of these methods must make the same name change.
  • Loading branch information
ersimont committed Jun 6, 2022
1 parent edea7d4 commit b65e4e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
26 changes: 13 additions & 13 deletions projects/ng-core/src/lib/wrapped-control-superclass.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('WrappedControlSuperclass', () => {
lastName: new FormControl(),
});

protected override outerToInner(outer: FullName | null): FullName {
protected override outerToInnerValue(outer: FullName | null): FullName {
// `outer` can come in as `null` during initialization when the user binds with `ngModel`
return outer ?? new FullName();
}
Expand Down Expand Up @@ -124,13 +124,13 @@ describe('WrappedControlSuperclass', () => {
> {
control = new FormControl();

protected override setUpOuterToInner$(
protected override setUpOuterToInnerValue$(
value$: Observable<number>,
): Observable<string> {
return value$.pipe(map((outer) => String(outer / 2)));
}

protected override setUpInnerToOuter$(
protected override setUpInnerToOuterValue$(
value$: Observable<string>,
): Observable<number> {
return value$.pipe(
Expand Down Expand Up @@ -172,15 +172,15 @@ describe('WrappedControlSuperclass', () => {
});
});

it('gracefully handles an error in .innerToOuter()', () => {
it('gracefully handles an error in .innerToOuterValue()', () => {
@Component({
selector: `sl-error-in`,
template: `<input [formControl]="control" />`,
providers: [provideValueAccessor(ErrorInComponent)],
})
class ErrorInComponent extends WrappedControlSuperclass<number> {
control = new FormControl();
override outerToInner = jasmine.createSpy();
override outerToInnerValue = jasmine.createSpy();
}

@Component({
Expand All @@ -205,26 +205,26 @@ describe('WrappedControlSuperclass', () => {
).nativeElement;

const error = new Error();
control.outerToInner.and.throwError(error);
control.outerToInnerValue.and.throwError(error);
ctx.assignInputs({ value: 'wont show' });
expectSingleCallAndReset(handleError, error);
expect(input.value).toBe('');

control.outerToInner.and.returnValue('restored');
control.outerToInnerValue.and.returnValue('restored');
ctx.assignInputs({ value: 'will show' });
expect(input.value).toBe('restored');
});
});

it('gracefully handles an error in .outerToInner()', () => {
it('gracefully handles an error in .outerToInnerValue()', () => {
@Component({
selector: `sl-error-out`,
template: `<input [formControl]="control" />`,
providers: [provideValueAccessor(ErrorOutComponent)],
})
class ErrorOutComponent extends WrappedControlSuperclass<number> {
control = new FormControl();
override innerToOuter = jasmine.createSpy();
override innerToOuterValue = jasmine.createSpy();
}

@Component({
Expand All @@ -250,12 +250,12 @@ describe('WrappedControlSuperclass', () => {
).nativeElement;

const error = new Error();
control.innerToOuter.and.throwError(error);
control.innerToOuterValue.and.throwError(error);
setValue(input, 'wont show');
expectSingleCallAndReset(handleError, error);
expect(wrapper.value).toBe('initial value');

control.innerToOuter.and.returnValue('restored');
control.innerToOuterValue.and.returnValue('restored');
setValue(input, 'will show');
expect(wrapper.value).toBe('restored');
});
Expand Down Expand Up @@ -408,11 +408,11 @@ describe('WrappedControlSuperclass tests using an old style fixture', () => {
class DateComponent extends WrappedControlSuperclass<Date, string> {
control = new FormControl();

protected override innerToOuter(value: string): Date {
protected override innerToOuterValue(value: string): Date {
return new Date(value + 'Z');
}

protected override outerToInner(value: Date): string {
protected override outerToInnerValue(value: Date): string {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- happens during initialization
if (value === null) {
return '';
Expand Down
34 changes: 17 additions & 17 deletions projects/ng-core/src/lib/wrapped-control-superclass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { FormComponentSuperclass } from './form-component-superclass';
* lastName: new FormControl(),
* });
*
* protected outerToInner(outer: FullName | null): FullName {
* protected outerToInnerValue(outer: FullName | null): FullName {
* // `outer` can come in as `null` during initialization when the user binds with `ngModel`
* return outer || new FullName();
* }
Expand All @@ -69,11 +69,11 @@ import { FormComponentSuperclass } from './form-component-superclass';
* providers: [provideValueAccessor(DateComponent)],
* })
* class DateComponent extends WrappedFormControlSuperclass<Date, string> {
* protected innerToOuter(inner: string): Date {
* protected innerToOuterValue(inner: string): Date {
* return new Date(inner + "Z");
* }
*
* protected outerToInner(outer: Date): string {
* protected outerToInnerValue(outer: Date): string {
* if (outer === null) {
* return ""; // happens during initialization
* }
Expand Down Expand Up @@ -101,7 +101,7 @@ export abstract class WrappedControlSuperclass<OuterType, InnerType = OuterType>
this.#injector = injector;
this.#errorHandler = injector.get(ErrorHandler);
this.subscribeTo(
this.setUpOuterToInner$(this.#incomingValues$),
this.setUpOuterToInnerValue$(this.#incomingValues$),
(inner) => {
this.control.setValue(inner, { emitEvent: false });
},
Expand All @@ -111,7 +111,7 @@ export abstract class WrappedControlSuperclass<OuterType, InnerType = OuterType>
ngOnInit(): void {
this.#bindValidation();
this.subscribeTo(
this.setUpInnerToOuter$(this.control.valueChanges),
this.setUpInnerToOuterValue$(this.control.valueChanges),
(outer) => {
this.emitOutgoingValue(outer);
},
Expand Down Expand Up @@ -143,31 +143,31 @@ export abstract class WrappedControlSuperclass<OuterType, InnerType = OuterType>
*
* In this example, incoming values are debounced before being passed through to the inner form control
* ```ts
* setUpOuterToInner$(outer$: Observable<OuterType>): Observable<InnerType> {
* setUpOuterToInnerValue$(outer$: Observable<OuterType>): Observable<InnerType> {
* return outer$.pipe(
* debounce(300),
* map((outer) => doExpensiveTransformToInnerValue(outer)),
* );
* }
* ```
*
* For a simple transformation, see {@linkcode #outerToInner} instead.
* For a simple transformation, see {@linkcode #outerToInnerValue} instead.
*/
protected setUpOuterToInner$(
protected setUpOuterToInnerValue$(
outer$: Observable<OuterType>,
): Observable<InnerType> {
return outer$.pipe(
map((outer) => this.outerToInner(outer)),
map((outer) => this.outerToInnerValue(outer)),
this.#handleError(),
);
}

/**
* Override this to modify a value coming from the outside to the format needed within this component.
*
* For more complex needs, see {@linkcode #setUpOuterToInner$} instead.
* For more complex needs, see {@linkcode #setUpOuterToInnerValue$} instead.
*/
protected outerToInner(outer: OuterType): InnerType {
protected outerToInnerValue(outer: OuterType): InnerType {
return outer as unknown as InnerType;
}

Expand All @@ -176,30 +176,30 @@ export abstract class WrappedControlSuperclass<OuterType, InnerType = OuterType>
*
* In this example, illegal values are not emitted
* ```ts
* setUpInnerToOuter$(inner$: Observable<InnerType>): Observable<OuterType> {
* setUpInnerToOuterValue$(inner$: Observable<InnerType>): Observable<OuterType> {
* return inner$.pipe(
* filter((inner) => isLegalValue(outer)),
* );
* }
* ```
*
* For a simple transformation, see {@linkcode #innerToOuter} instead.
* For a simple transformation, see {@linkcode #innerToOuterValue} instead.
*/
protected setUpInnerToOuter$(
protected setUpInnerToOuterValue$(
inner$: Observable<InnerType>,
): Observable<OuterType> {
return inner$.pipe(
map((inner) => this.innerToOuter(inner)),
map((inner) => this.innerToOuterValue(inner)),
this.#handleError(),
);
}

/**
* Override this to modify a value coming from within this component to the format expected on the outside.
*
* For more complex needs, see {@linkcode #setUpInnerToOuter$} instead.
* For more complex needs, see {@linkcode #setUpInnerToOuterValue$} instead.
*/
protected innerToOuter(inner: InnerType): OuterType {
protected innerToOuterValue(inner: InnerType): OuterType {
return inner as unknown as OuterType;
}

Expand Down

0 comments on commit b65e4e2

Please sign in to comment.