Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use promise instead of waitForCondition for events #3204

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert, expect } from '@open-wc/testing';
import type { SbbCardElement } from '@sbb-esta/lyne-elements/card.js';
import { fixture } from '@sbb-esta/lyne-elements/core/testing/private.js';
import { EventSpy, waitForCondition } from '@sbb-esta/lyne-elements/core/testing.js';
import { EventSpy } from '@sbb-esta/lyne-elements/core/testing.js';
import { html } from 'lit/static-html.js';

import type { ITripItem, Notice, PtSituation } from '../core/timetable/timetable-properties.js';
Expand Down Expand Up @@ -32,7 +32,7 @@ describe(`sbb-timetable-row`, () => {
const changeSpy = new EventSpy('click');

card.click();
await waitForCondition(() => changeSpy.events.length === 1);
await changeSpy.calledOnce();
expect(changeSpy.count).to.be.equal(1);
});
});
Expand Down
18 changes: 9 additions & 9 deletions src/elements/accordion/accordion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, expect } from '@open-wc/testing';
import { html } from 'lit/static-html.js';

import { fixture } from '../core/testing/private.js';
import { waitForCondition, waitForLitRender, EventSpy } from '../core/testing.js';
import { EventSpy, waitForLitRender } from '../core/testing.js';
import {
SbbExpansionPanelElement,
type SbbExpansionPanelHeaderElement,
Expand Down Expand Up @@ -139,21 +139,21 @@ describe(`sbb-accordion`, () => {
}

headerTwo.click();
await waitForCondition(() => willOpenEventSpy.events.length === 1);
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
expect(panelOne.expanded).to.be.equal(false);
expect(panelTwo.expanded).to.be.equal(true);
expect(panelThree.expanded).to.be.equal(false);

headerOne.click();
await waitForCondition(() => willOpenEventSpy.events.length === 2);
await willOpenEventSpy.calledTimes(2);
expect(willOpenEventSpy.count).to.be.equal(2);
expect(panelOne.expanded).to.be.equal(true);
expect(panelTwo.expanded).to.be.equal(false);
expect(panelThree.expanded).to.be.equal(false);

headerThree.click();
await waitForCondition(() => willOpenEventSpy.events.length === 3);
await willOpenEventSpy.calledTimes(3);
expect(willOpenEventSpy.count).to.be.equal(3);
expect(panelOne.expanded).to.be.equal(false);
expect(panelTwo.expanded).to.be.equal(false);
Expand Down Expand Up @@ -182,21 +182,21 @@ describe(`sbb-accordion`, () => {
}

headerTwo.click();
await waitForCondition(() => willOpenEventSpy.events.length === 1);
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
expect(panelOne.expanded).to.be.equal(false);
expect(panelTwo.expanded).to.be.equal(true);
expect(panelThree.expanded).to.be.equal(false);

headerOne.click();
await waitForCondition(() => willOpenEventSpy.events.length === 2);
await willOpenEventSpy.calledTimes(2);
expect(willOpenEventSpy.count).to.be.equal(2);
expect(panelOne.expanded).to.be.equal(true);
expect(panelTwo.expanded).to.be.equal(true);
expect(panelThree.expanded).to.be.equal(false);

headerThree.click();
await waitForCondition(() => willOpenEventSpy.events.length === 3);
await willOpenEventSpy.calledTimes(3);
expect(willOpenEventSpy.count).to.be.equal(3);
expect(panelOne.expanded).to.be.equal(true);
expect(panelTwo.expanded).to.be.equal(true);
Expand Down Expand Up @@ -224,12 +224,12 @@ describe(`sbb-accordion`, () => {
const willOpenEventSpy = new EventSpy(SbbExpansionPanelElement.events.willOpen);

headerTwo.click();
await waitForCondition(() => willOpenEventSpy.events.length === 1);
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
expect(panelTwo.expanded).to.be.equal(true);

headerThree.click();
await waitForCondition(() => willOpenEventSpy.events.length === 2);
await willOpenEventSpy.calledTimes(2);
expect(willOpenEventSpy.count).to.be.equal(2);
expect(panelThree.expanded).to.be.equal(true);

Expand Down
8 changes: 4 additions & 4 deletions src/elements/alert/alert/alert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, expect } from '@open-wc/testing';
import { html } from 'lit/static-html.js';

import { fixture } from '../../core/testing/private.js';
import { waitForCondition, EventSpy } from '../../core/testing.js';
import { EventSpy } from '../../core/testing.js';

import { SbbAlertElement } from './alert.js';

Expand All @@ -25,17 +25,17 @@ describe(`sbb-alert`, () => {
html`<sbb-alert title-content="disruption">Interruption</sbb-alert>`,
);

await waitForCondition(() => willOpenSpy.events.length === 1);
await willOpenSpy.calledOnce();
expect(willOpenSpy.count).to.be.equal(1);
await waitForCondition(() => didOpenSpy.events.length === 1);
await didOpenSpy.calledOnce();
expect(didOpenSpy.count).to.be.equal(1);

alert.requestDismissal();
expect(dismissalSpy.count).to.be.equal(1);

alert.close();

await waitForCondition(() => didCloseSpy.events.length === 1);
await didCloseSpy.calledOnce();
expect(willCloseSpy.count).to.be.equal(1);
expect(didCloseSpy.count).to.be.equal(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, expect } from '@open-wc/testing';
import { html } from 'lit/static-html.js';

import { fixture } from '../../core/testing/private.js';
import { EventSpy, waitForCondition, waitForLitRender } from '../../core/testing.js';
import { EventSpy, waitForLitRender } from '../../core/testing.js';

import { SbbAutocompleteGridButtonElement } from './autocomplete-grid-button.js';

Expand All @@ -24,7 +24,7 @@ describe(`sbb-autocomplete-grid-button`, () => {
const clickSpy = new EventSpy('click');

element.click();
await waitForCondition(() => clickSpy.events.length === 1);
await clickSpy.calledOnce();
expect(clickSpy.count).to.be.equal(1);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { html } from 'lit/static-html.js';

import { isSafari } from '../../core/dom.js';
import { fixture, tabKey } from '../../core/testing/private.js';
import { describeIf, EventSpy, waitForCondition, waitForLitRender } from '../../core/testing.js';
import { describeIf, EventSpy, waitForLitRender } from '../../core/testing.js';
import { SbbFormFieldElement } from '../../form-field.js';
import type { SbbAutocompleteGridButtonElement } from '../autocomplete-grid-button.js';
import { SbbAutocompleteGridOptionElement } from '../autocomplete-grid-option.js';
Expand Down Expand Up @@ -100,47 +100,47 @@ describe(`sbb-autocomplete-grid`, () => {
const didCloseEventSpy = new EventSpy(SbbAutocompleteGridElement.events.didClose);

input.click();
await waitForCondition(() => willOpenEventSpy.events.length === 1);
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);

await waitForCondition(() => didOpenEventSpy.events.length === 1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);
expect(input).to.have.attribute('aria-expanded', 'true');

await sendKeys({ press: 'Escape' });
await waitForCondition(() => willCloseEventSpy.events.length === 1);
await willCloseEventSpy.calledOnce();
expect(willCloseEventSpy.count).to.be.equal(1);
await waitForCondition(() => didCloseEventSpy.events.length === 1);
await didCloseEventSpy.calledOnce();
expect(didCloseEventSpy.count).to.be.equal(1);
expect(input).to.have.attribute('aria-expanded', 'false');

await sendKeys({ press: 'ArrowDown' });
await waitForCondition(() => willOpenEventSpy.events.length === 2);
await willOpenEventSpy.calledTimes(2);
expect(willOpenEventSpy.count).to.be.equal(2);
await waitForCondition(() => didOpenEventSpy.events.length === 2);
await didOpenEventSpy.calledTimes(2);
expect(didOpenEventSpy.count).to.be.equal(2);
expect(input).to.have.attribute('aria-expanded', 'true');

await sendKeys({ press: tabKey });
await waitForCondition(() => willCloseEventSpy.events.length === 2);
await willCloseEventSpy.calledTimes(2);
expect(willCloseEventSpy.count).to.be.equal(2);
await waitForCondition(() => didCloseEventSpy.events.length === 2);
await didCloseEventSpy.calledTimes(2);
expect(didCloseEventSpy.count).to.be.equal(2);
expect(input).to.have.attribute('aria-expanded', 'false');

input.click();
await waitForCondition(() => willOpenEventSpy.events.length === 3);
await willOpenEventSpy.calledTimes(3);
expect(willOpenEventSpy.count).to.be.equal(3);
await waitForCondition(() => didOpenEventSpy.events.length === 3);
await didOpenEventSpy.calledTimes(3);
expect(didOpenEventSpy.count).to.be.equal(3);
expect(input).to.have.attribute('aria-expanded', 'true');

// Simulate backdrop click
sendMouse({ type: 'click', position: [formField.offsetWidth + 25, 25] });
await sendMouse({ type: 'click', position: [formField.offsetWidth + 25, 25] });

await waitForCondition(() => willCloseEventSpy.events.length === 3);
await willCloseEventSpy.calledTimes(3);
expect(willCloseEventSpy.count).to.be.equal(3);
await waitForCondition(() => didCloseEventSpy.events.length === 3);
await didCloseEventSpy.calledTimes(3);
expect(didCloseEventSpy.count).to.be.equal(3);
expect(input).to.have.attribute('aria-expanded', 'false');
});
Expand All @@ -153,9 +153,9 @@ describe(`sbb-autocomplete-grid`, () => {
);

input.focus();
await waitForCondition(() => willOpenEventSpy.events.length === 1);
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
await waitForCondition(() => didOpenEventSpy.events.length === 1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);

await sendKeys({ press: 'ArrowDown' });
Expand All @@ -173,16 +173,16 @@ describe(`sbb-autocomplete-grid`, () => {
const clickSpy = new EventSpy('click');

input.focus();
await waitForCondition(() => willOpenEventSpy.events.length === 1);
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
await waitForCondition(() => didOpenEventSpy.events.length === 1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);

const buttonOne = element.querySelector('#button-1') as SbbAutocompleteGridButtonElement;
buttonOne.click();
await waitForLitRender(element);

await waitForCondition(() => clickSpy.events.length === 1);
await clickSpy.calledOnce();
expect(clickSpy.count).to.be.equal(1);
expect(
(clickSpy.firstEvent!.target as SbbAutocompleteGridButtonElement).option!.textContent,
Expand All @@ -201,7 +201,7 @@ describe(`sbb-autocomplete-grid`, () => {
const buttonThree = element.querySelector('#button-3');
input.focus();

await waitForCondition(() => didOpenEventSpy.events.length === 1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);

await sendKeys({ press: 'ArrowDown' });
Expand Down Expand Up @@ -246,7 +246,7 @@ describe(`sbb-autocomplete-grid`, () => {
const optTwo = element.querySelector('#option-2');
input.focus();

await waitForCondition(() => didOpenEventSpy.events.length === 1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);

await sendKeys({ press: 'ArrowDown' });
Expand All @@ -259,7 +259,7 @@ describe(`sbb-autocomplete-grid`, () => {
expect(input).to.have.attribute('aria-activedescendant', 'option-2');

await sendKeys({ press: 'Enter' });
await waitForCondition(() => didCloseEventSpy.events.length === 1);
await didCloseEventSpy.calledOnce();
expect(didCloseEventSpy.count).to.be.equal(1);

expect(optTwo).not.to.have.attribute('data-active');
Expand All @@ -277,7 +277,7 @@ describe(`sbb-autocomplete-grid`, () => {
const buttonTwo = element.querySelector('#button-2');
input.focus();

await waitForCondition(() => didOpenEventSpy.events.length === 1);
await didOpenEventSpy.calledOnce();
expect(didOpenEventSpy.count).to.be.equal(1);

await sendKeys({ press: 'ArrowDown' });
Expand All @@ -289,7 +289,7 @@ describe(`sbb-autocomplete-grid`, () => {
expect(buttonOne).to.have.attribute('data-focus-visible');
expect(input).to.have.attribute('aria-activedescendant', 'button-1');
await sendKeys({ press: 'Enter' });
await waitForCondition(() => clickSpy.events.length === 1);
await clickSpy.calledOnce();
expect(clickSpy.count).to.be.equal(1);

await sendKeys({ press: 'ArrowDown' });
Expand All @@ -300,7 +300,7 @@ describe(`sbb-autocomplete-grid`, () => {
expect(buttonTwo).to.have.attribute('data-focus-visible');
expect(input).to.have.attribute('aria-activedescendant', 'button-2');
await sendKeys({ press: 'Enter' });
await waitForCondition(() => clickSpy.events.length === 2);
await clickSpy.calledTimes(2);
expect(clickSpy.count).to.be.equal(2);
});

Expand Down Expand Up @@ -344,7 +344,7 @@ describe(`sbb-autocomplete-grid`, () => {
);
element.open();

await waitForCondition(() => willOpenEventSpy.events.length === 1);
await willOpenEventSpy.calledOnce();
expect(willOpenEventSpy.count).to.be.equal(1);
await waitForLitRender(element);

Expand All @@ -356,15 +356,15 @@ describe(`sbb-autocomplete-grid`, () => {
const willCloseEventSpy = new EventSpy(SbbAutocompleteGridElement.events.willClose);

element.open();
await waitForCondition(() => didOpenEventSpy.events.length === 1);
await didOpenEventSpy.calledOnce();
await waitForLitRender(element);

element.addEventListener(SbbAutocompleteGridElement.events.willClose, (ev) =>
ev.preventDefault(),
);
element.close();

await waitForCondition(() => willCloseEventSpy.events.length === 1);
await willCloseEventSpy.calledOnce();
await waitForLitRender(element);

expect(element).to.have.attribute('data-state', 'opened');
Expand Down
Loading
Loading