Skip to content

test(helper): Add waitUntilAllAsyncImagesAreLoaded #11

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ language: node_js
node_js:
# we recommend testing addons with the same minimum supported node version as Ember CLI
# so that your addon works for all apps
- "6"
- "12"

sudo: false
dist: trusty

addons:
chrome: stable

cache:
directories:
- $HOME/.npm
cache: npm

env:
global:
Expand Down Expand Up @@ -47,7 +44,6 @@ jobs:

before_install:
- npm config set spin false
- npm install -g npm@4
- npm --version

script:
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ Extracted from [smoke-and-mirrors](https://github.com/runspired/smoke-and-mirror
title=attrs.imageTitle
}}
```

### Test Helper

Sometimes you want to wait for all async images on a page to load during tests. `waitUntilAllAsyncImagesAreLoaded` can be use to wait for all image to finish loading during a test. For instance you use visual regression testing and need to ensure all async images are loaded before taking a snapshot.

```js
import { visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { module, test } from 'qunit';
import { takeVisualRegressionSnapshot } from 'my-great-visual-regression-testing-lib';
import { waitUntilAllAsyncImagesAreLoaded } from 'ember-async-image/test-support';

module('Acceptance | dashboard', function (hooks) {
setupApplicationTest(hooks);

test('no visual regressions', async function(assert) {
visit('/page-with-async-images');
await waitUntilAllAsyncImagesAreLoaded();
await takeVisualRegressionSnapshot();
});
});
```
3 changes: 3 additions & 0 deletions addon-test-support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import waitUntilAllAsyncImagesAreLoaded from './wait-until-all-async-images-are-loaded';

export { waitUntilAllAsyncImagesAreLoaded };
13 changes: 13 additions & 0 deletions addon-test-support/wait-until-all-async-images-are-loaded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { findAll, waitUntil } from '@ember/test-helpers';

export default function waitUntilAllAsyncImagesAreLoaded({ emptyAllowed = false, failureAllowed = false } = {}) {
return waitUntil(function allAsyncImagesLoaded() {
return findAll('.async-image').every(function isLoaded(element) {
return (
element.classList.contains('is-loaded') ||
(failureAllowed && element.classList.contains('is-failed')) ||
(emptyAllowed && element.classList.contains('is-empty'))
);
});
});
}
Binary file added tests/dummy/public/small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions tests/integration/components/async-image-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, findAll } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { waitUntilAllAsyncImagesAreLoaded } from 'ember-async-image/test-support';

module('Integration | Component | async image', function(hooks) {
setupRenderingTest(hooks);
Expand All @@ -15,4 +16,29 @@ module('Integration | Component | async image', function(hooks) {

assert.equal(findAll('*')[0].firstElementChild.tagName, 'IMG');
});

test('waitUntilAllAsyncImagesAreLoaded', async function(assert) {
await render(hbs`{{async-image src="/small.png"}}`);
let element = findAll('*')[0].firstElementChild;

assert.ok(element.classList.contains('is-loading'), `classes: ${element.classList.toString()}`);
await waitUntilAllAsyncImagesAreLoaded();
assert.ok(element.classList.contains('is-loaded'), `classes: ${element.classList.toString()}`);
});

test('waitUntilAllAsyncImagesAreLoaded - empty', async function(assert) {
await render(hbs`{{async-image src=""}}`);
let element = findAll('*')[0].firstElementChild;

await waitUntilAllAsyncImagesAreLoaded({ emptyAllowed: true });
assert.ok(element.classList.contains('is-empty'), `classes: ${element.classList.toString()}`);
});

test('waitUntilAllAsyncImagesAreLoaded - failed', async function(assert) {
await render(hbs`{{async-image src="/nonexistent.png"}}`);
let element = findAll('*')[0].firstElementChild;

await waitUntilAllAsyncImagesAreLoaded({ failureAllowed: true });
assert.ok(element.classList.contains('is-failed'), `classes: ${element.classList.toString()}`);
});
});