Change Detection in Cypress Angular Component Testing

huangapple go评论62阅读模式
英文:

Change Detection in Cypress Angular Component Testing

问题

I am using cypress angular component testing to try to test whether or not a button is disabled.

I am testing the following code:

this.subsink.sink = this.manageService.validCompetitorListener
.subscribe((validCompetitor:ValidCompetitor)=>{
  this.validCompetitor = validCompetitor;
  //this.cdref.detectChanges();
})

I took out the cdref.detectChanges because when I run the e2e test, this produces an error. validCompetitorListener is an rxjs BehaviorSubject

The template code is :

<button
    data-cy="save-btn"
    class="btn btn-success"
    [disabled]="validCompetitor.invalid"
    (click)="save()"
  >Save</button>

The following Test works:

it.only('should save data to system', () => {
  cy.wrap(true).then(() => {
    manageService.updateAthlete(true, new Athlete(getMockAthlete()));
    manageService.updateRegister(true,{belt_id:5});
  });
  cy.get('#info').click();
  cy.get('[data-cy="save-btn"]').click();
  
});

BUT the only reason I use the cy.get('#info').click() statement is to get change detection to work so my component, since changeDetection caused an error when I ran the e2e test. If this was a standard Angular unit test I would include fixture.detectChanges. Is there an equivalent in cypress component testing?

英文:

I am using cypress angular component testing to try to test weather or not a button is disabled.

I am testing the following code:

this.subsink.sink = this.manageService.validCompetitorListener
.subscribe((validCompetitor:ValidCompetitor)=>{
  this.validCompetitor = validCompetitor;
  //this.cdref.detectChanges();
})

I took out the cdref.detectChanges, because when I run the e2e test, this produces an error. validCompetitorListener is an rxjs BehaviorSubject

The template code is :

<button
    data-cy="save-btn"
    class="btn btn-success"
    [disabled]="validCompetitor.invalid"
    (click)="save()"
  >Save</button>

The following Test works:

it.only('should save data to system', () => {
  cy.wrap(true).then(() => {
    manageService.updateAthlete(true, new Athlete(getMockAthlete()));
    manageService.updateRegister(true,{belt_id:5});
  });
  cy.get('#info').click();
  cy.get('[data-cy="save-btn"]').click();
  
});

BUT the only reason I use the cy.get('#info').click() statement is to get change detection to work so my component, since changeDetection caused an error when I ran the e2e test. If this was a standard Angular unit test I would include fixture.detectChanges. Is there an equivelent in cypress component testing?

答案1

得分: 1

我相信你可以通过以下方式来处理 fixture

let myFixture: ComponentFixture<Component>;

beforeEach(() => {
   cy.mount(Component)
    .then(({ fixture }) => {
        myFixture = fixture
    })
});

// should be able to do myFixture.detectChanges() now.

如果您有其他需要翻译的部分,请告诉我。

英文:

I believe you can do the following to get a handle on the fixture.

let myFixture: ComponentFixture&lt;Component&gt;;

beforeEach(() =&gt; {
   cy.mount(Component)
    .then(({ fixture }) =&gt; {
        myFixture = fixture
    })
});

// should be able to do myFixture.detectChanges() now.

huangapple
  • 本文由 发表于 2023年5月10日 23:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定