Ionic Angular tests with Spectator/Jest: Are sleeps necessary?

67 Views Asked by At

I am trying to write Unit Tests for my Angular App that uses Ionic Components. My HTML looks like this:

  <ion-select data-testid="ort-select" formControlName="place" name="place" id="place">
<ion-select-option *ngFor="let ort of orte" [value]="ort.id">{{ ort.name }}</ion-select-option>
</ion-select>

I have a test that looks like this:

await sleep(100); // await new Promise(r => setTimeout(r, 100))
expect(spectator.query(byTestId('ort-select'))).toHaveClass('hydrated');

spectator.click(byTestId('ort-select'));

await sleep(100);
spectator.click(byTextContent('Albsfelde', { selector: 'ion-alert button' }));
spectator.click(byTextContent('OK', { selector: 'ion-alert button' }));

expect(spectator.component.addressForm.value.place).toBe(1);

The test works, however the sleeps are necessary. Having to put sleeps into testing code seems to be an anti-pattern. From what I can guess the sleeps wait for the UI to be setup by Ionic and for an alert-dialog to show up that Ionic creates.

Is there a way to do that without sleeps?

0

There are 0 best solutions below