LoginSignup
1
0

More than 3 years have passed since last update.

Jasmine, fixture.debugElement.nativeElement.querySelectorで要素が取得できないよ!!!

Last updated at Posted at 2020-03-05

Jasmine 3.3

Angular Jasmineでハマったポイント
もしかしたらng-ifとかで、画面の表示をガッツリ切り替えてないですか?
(ローディング等)

とりあえずfixture.debugElement.nativeElementを表示してみて、そこに目的の要素がなければquerySelectorで何しようと取れません。

必要あれば、ngifをクリアできる様にcomponentのパラメータをいじりましょう

test.component.html
<app-loading *ngIf='isLoading'></app-loading>
<div *ngIf='!isLoading'>
  <!--
    ここにほしい要素
  -->
</div>

蛇足ですが、fixture.detectChanges();を忘れずに

蛇足2
もしテンプレートの要素を取得したい場合、
下記二つは同じ(inputの要素をnameでとる場合)

test.component.spec.ts
let element = fixture.debugElement.nativeElement.querySelector('input[name=approveAmount]');
let element = fixture.debugElement.query(By.css('[name=approveAmount]')).nativeElement as HTMLInputElement;

//あとは入力して、dispatchすれば反映されるよ!
element.value = something;
element.dispatchEvent(new Event('input'))
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0