Skip to content
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ it('should work when printing', async() => {
});
```

### Accessibility Testing with aXe
### Accessibility Testing with axe
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I was here...


Elements can be processed by the [aXe accessibility validator](https://github.com/dequelabs/axe-core), which will automatically fail the test if any violations are detected.
Elements can be processed by the [axe accessibility validator](https://github.com/dequelabs/axe-core), which will automatically fail the test if any violations are detected.

```javascript
it('should be accessible', async() => {
Expand Down Expand Up @@ -302,6 +302,20 @@ it('should wait for updates', async() => {
});
```

If that change causes a different set of nested components to be rendered, and those nested components take some time to finish *their* rendering, it's possible that awaiting `updateComplete` isn't enough.

In this case, `waitForElem` can be used to recursively wait in a similar way to the original `fixture` call:

```javascript
import { fixture, waitForElem } from '@brightspace-ui/testing';

it('should wait for recursive re-rendering', async() => {
const elem = await fixture(html`<my-elem></my-elem>`);
elem.errorState = true;
await waitForElem(elem);
});
```

#### Waiting for `setTimeout` or `requestAnimationFrame`

To wait a fixed amount of time (analogous to `setTimeout`), use `aTimeout`. To wait until the moment before browser repaints the screen (analogous to `requestAnimationFrame`), use `nextFrame`.
Expand Down
12 changes: 6 additions & 6 deletions src/browser/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ function getComposedChildren(node) {
* @param {*} elem
* @param {boolean} awaitLoadingComplete
*/
async function waitForElem(elem, awaitLoadingComplete = true) {
export async function waitForElem(elem, awaitLoadingComplete = true) {

if (!elem) return;

const doWait = async() => {

const update = elem.updateComplete;
if (typeof update === 'object' && Promise.resolve(update) === update) {
await update;
if (awaitLoadingComplete && typeof elem.getLoadingComplete === 'function') {
await elem.getLoadingComplete();
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also flipped the order here. We first wait for getLoadingComplete and then we wait for updateComplete. The reasoning here is that almost always getLoadingComplete resolving is going to trigger some kind of state change, so we wait for updateComplete after that.

await nextFrame();
}

if (awaitLoadingComplete && typeof elem.getLoadingComplete === 'function') {
await elem.getLoadingComplete();
const update = elem.updateComplete;
if (typeof update === 'object' && Promise.resolve(update) === update) {
await update;
await nextFrame();
}

Expand Down
2 changes: 1 addition & 1 deletion src/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import './axe.js';

export { assert, aTimeout, defineCE, expect, html, nextFrame, oneDefaultPreventedEvent, oneEvent, waitUntil } from '@open-wc/testing';
export { clickAt, clickElem, clickElemAt, dragDropElems, focusElem, hoverAt, hoverElem, hoverElemAt, sendKeys, sendKeysElem, setViewport } from './commands.js';
export { fixture } from './fixture.js';
export { fixture, waitForElem } from './fixture.js';
export { runConstructor } from './constructor.js';
32 changes: 31 additions & 1 deletion test/browser/fixture.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { defineCE, expect, fixture, html, waitUntil } from '../../src/browser/index.js';
import { defineCE, expect, fixture, html, waitForElem, waitUntil } from '../../src/browser/index.js';
import { LitElement, nothing } from 'lit';
import { restore, stub } from 'sinon';
import { focusElem } from '../../src/browser/commands.js';
Expand Down Expand Up @@ -64,6 +64,24 @@ const removedElem = defineCE(
}
);

const fastOrSlowElem = defineCE(
class extends LitElement {
static properties = {
slow: { type: Boolean }
};
constructor() {
super();
this.slow = false;
}
render() {
if (this.slow) {
return unsafeHTML(`<${slowElem} id="slow">slow</${slowElem}>`);
}
return html`<p>fast</p>`;
}
}
);

describe('fixture', () => {

afterEach(() => restore());
Expand Down Expand Up @@ -332,6 +350,18 @@ describe('fixture', () => {
expect(elem.querySelector(removedElem)).to.be.null;
});

it('should wait for elements that are updated to be slow', async() => {
const elem = await fixture(`<${fastOrSlowElem}></${fastOrSlowElem}>`);
expect(elem.shadowRoot.querySelector('p').textContent).to.equal('fast');
elem.slow = true;
const waitPromise = waitForElem(elem);
await waitUntil(() => resolves.has('slow'));
timeouts.push(setTimeout(() => resolves.get('slow')(), 50));
expect(elem.shadowRoot.querySelector(slowElem).finished).to.be.false;
await waitPromise;
expect(elem.shadowRoot.querySelector(slowElem).finished).to.be.true;
Comment thread
bearfriend marked this conversation as resolved.
});

});

describe('waitForFonts', () => {
Expand Down