Skip to content

Commit

Permalink
fix(ng-core): Fix a timing issue when combining nasModel and `Wrapp…
Browse files Browse the repository at this point in the history
…edControlSuperclass` so that it properly receives its initial value
  • Loading branch information
ersimont committed Nov 4, 2021
1 parent 337eaa3 commit 6e07405
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
@@ -0,0 +1,30 @@
import { Component } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RootStore } from '@s-libs/app-state';
import { NasModelModule } from '@s-libs/ng-app-state';
import { ComponentContext } from '@s-libs/ng-dev';
import { WrappedControlComponent } from './wrapped-control.component';

describe('WrappedControlComponent', () => {
// There is some kind of tricky timing issue when using NasModel and WrappedControlSuperclass that required moving a subscription from `ngOnInit()` to `constructor()` to fix.
it('catches the first incoming value from a nasModel', () => {
@Component({
template: `
<s-wrapped-control [nasModel]="store('value')"></s-wrapped-control>
`,
})
class WrapperComponent {
store = new RootStore({ value: 'initial value' });
}

const ctx = new ComponentContext(WrapperComponent, {
declarations: [WrappedControlComponent],
imports: [FormsModule, NasModelModule, ReactiveFormsModule],
});
ctx.run(() => {
const el: HTMLElement = ctx.fixture.nativeElement;
const input = el.querySelector('input')!;
expect(input.value).toBe('initial value');
});
});
});
6 changes: 5 additions & 1 deletion projects/ng-core/src/lib/wrapped-control-superclass.ts
Expand Up @@ -77,10 +77,14 @@ export abstract class WrappedControlSuperclass<OuterType, InnerType = OuterType>

private incomingValues$ = new Subject<OuterType>();

ngOnInit(): void {
constructor(injector: Injector) {
super(injector);
this.subscribeTo(this.setUpOuterToInner$(this.incomingValues$), (inner) => {
this.control.setValue(inner, { emitEvent: false });
});
}

ngOnInit(): void {
this.subscribeTo(
this.setUpInnerToOuter$(this.control.valueChanges),
(outer) => {
Expand Down

0 comments on commit 6e07405

Please sign in to comment.