角度单元测试选择 onChange 监视空值

Whi*_*her 2 unit-testing angular

我想使用间谍测试选择 onChange 事件,但出现错误

Expected spy onSelect to have been called with [ 'en' ] but actual calls were [ '' ].
Run Code Online (Sandbox Code Playgroud)

成分

@Component({
  selector: 'tnos-languages',
  templateUrl: './languages.component.html',
  styleUrls: ['./languages.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class LanguagesComponent {
  @Input() languages: Language[];
  @Output()
  change: EventEmitter<string> = new EventEmitter<string>();
  constructor() { }
  onSelect(locale: string) {
    this.change.emit(locale);
  }
}
Run Code Online (Sandbox Code Playgroud)

模板

<select #selectLanguage (change)="onSelect(selectLanguage.value)">
  <option  *ngFor="let language of languages" [value]="language.locale">
    {{language.label}}
  </option>
</select>
Run Code Online (Sandbox Code Playgroud)

测试

const languages: Language[] = [{
  id: 1,
  label: 'Italiano',
  locale: 'it',
  default: false
}, {
  id: 2,
  label: 'English',
  locale: 'en',
  default: true
}];

fdescribe('LanguagesComponent', () => {
  let component: LanguagesComponent;
  let debugEl: DebugElement;
  let element: HTMLElement;
  let fixture: ComponentFixture<LanguagesComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [BrowserModule, FormsModule],
      declarations: [LanguagesComponent]
    })
      .compileComponents();
    fixture = TestBed.overrideComponent(LanguagesComponent, {
      set: {
        selector: 'languages',
        templateUrl: './languages.component.html',
        changeDetection: ChangeDetectionStrategy.Default
      }
    })
      .createComponent(LanguagesComponent);

    fixture.detectChanges();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(LanguagesComponent);
    component = fixture.componentInstance;
    debugEl = fixture.debugElement;
    element = debugEl.nativeElement;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should show `Italiano English` as option', async(() => {
    component.languages = languages;
    fixture.detectChanges();
    fixture.whenStable().then(() => {
      const options = debugEl.queryAll(By.css('select option'));
      expect(options[0].nativeElement.text).toBe('Italiano');
      expect(options[1].nativeElement.text).toBe('English');
    });
  }));

  it('should show `Italiano English` as option', async(() => {
    component.languages = languages;
    fixture.whenStable().then(() => {
      spyOn(component, 'onSelect');
      const select = debugEl.query(By.css('select')).nativeElement;
      select.dispatchEvent(new Event('change'));
      fixture.detectChanges();
      expect(component.onSelect).toHaveBeenCalled();
      //expect(component.onSelect).toHaveBeenCalledWith('en');
    });
  }));

  it('should emit on change', fakeAsync(() => {
    component.languages = languages;
    fixture.whenStable().then(() => {
      spyOn(component.change, 'emit');
      const select = debugEl.query(By.css('select')).nativeElement;
      select.dispatchEvent(new Event('change'));
      fixture.detectChanges();
      expect(component.change.emit).toHaveBeenCalled();
      expect(component.change.emit).toHaveBeenCalledWith('en');
    });
  }));
});
Run Code Online (Sandbox Code Playgroud)

你能帮我吗 ?

解决

作为@Ayala的回复,但他/她只忘记了fixture.detectChanges();

it('should show `Italiano English` as option', async(() => {
    component.languages = languages;
    fixture.detectChanges();
    fixture.whenStable().then(() => {
      spyOn(component, 'onSelect');
      const select = debugEl.query(By.css('select')).nativeElement;
      select.value = select.options[1].value;
      select.dispatchEvent(new Event('change'));
      fixture.detectChanges();
      expect(component.onSelect).toHaveBeenCalled();
      expect(component.onSelect).toHaveBeenCalledWith('en');
    });
  }));
Run Code Online (Sandbox Code Playgroud)

更新

我认为这是摆脱异步和稳定的方法

it('should emit on change', () => {
    component.languages = languages;
    fixture.detectChanges();
    spyOn(component.change, 'emit');
    const select = debugEl.query(By.css('select')).nativeElement;
    select.value = select.options[1].value;
    select.dispatchEvent(new Event('change'));
    fixture.detectChanges();
    expect(component.change.emit).toHaveBeenCalled();
    expect(component.change.emit).toHaveBeenCalledWith('en');
  });
Run Code Online (Sandbox Code Playgroud)

Aya*_*ala 6

在调度“更改”事件之前,您似乎没有更改选择值。

如果您尝试选择“en”选项,请尝试执行以下操作:

it('should show `Italiano English` as option', async(() => {
    component.languages = languages;
    fixture.whenStable().then(() => {
      spyOn(component, 'onSelect');
      const select = debugEl.query(By.css('select')).nativeElement;
      select.value = select.options[1].value;  // <-- select a new value
      select.dispatchEvent(new Event('change'));
      fixture.detectChanges();
      expect(component.onSelect).toHaveBeenCalled();
      expect(component.onSelect).toHaveBeenCalledWith('en');
    });
}));
Run Code Online (Sandbox Code Playgroud)