Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwickern/596bce2e941e6f9e179b97a527d91af9 to your computer and use it in GitHub Desktop.
Save dwickern/596bce2e941e6f9e179b97a527d91af9 to your computer and use it in GitHub Desktop.
Example with both attempts
import Ember from 'ember';
export default Ember.Component.extend({
imageLoaded: 'image loading',
imageWidth: 'width loading',
// purely to make sure image loads each time and
// is not cached for testing
volatileSrc: Ember.computed('src', function(){
return this.get('src') + '?cache=' + new Date().toISOString();
}),
actions: {
imageLoaded(message, width) {
if (!this.isDestroyed) {
this.set('imageLoaded', message);
this.set('imageWidth', width);
}
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'img',
attributeBindings: ['src'],
imageService: Ember.inject.service('image'),
loadImage() {
let src= this.get('src');
this.get('imageService').load(src).then(img => {
this.get('onImageLoad')("image loaded", img.width);
})
.catch(error => {
Ember.Logger.log('error', error);
this.get('onImageLoad')("image could not load");
});
},
didInsertElement() {
Ember.run.scheduleOnce('afterRender', this, this.loadImage);
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
});
import Ember from 'ember';
export default Ember.Service.extend({
init() {
this._super(...arguments);
if (Ember.testing) {
this._loading = 0;
Ember.Test.registerWaiter(() => this._loading === 0);
}
},
load(src) {
let promise = new Ember.RSVP.Promise((resolve, reject) => {
const img = new Image();
img.src = src;
img.onload = e => {
Ember.run(() => resolve(img));
};
img.onerror = () => {
Ember.run(() => reject());
};
});
if (Ember.testing) {
++this._loading;
return promise.finally(() => --this._loading);
}
return promise;
}
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
h1, h2 {
//display: none;
}
<h1>Testing async behaviour</h1>
<h2>Using wait() in integration tests</h2>
<p>I thought that by using a promise or the run loop then the wait helper would pause before checking the assertion, but it only works with run.later and I can't work out why.</p>
{{async-image-wrapper src='http://emberjs.com/images/tomsters/sandiego-zoey.png'}}
{{async-image src=volatileSrc onImageLoad=(action 'imageLoaded')}}
<p id="image-loaded-text">{{imageLoaded}}</p>
<p>width: <span id="image-width-text">{{imageWidth}}</span></p>
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import { moduleForComponent, test } from 'ember-qunit';
import wait from 'ember-test-helpers/wait';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('async-image-wrapper', 'Uses Promise', {
integration: true
});
test('If image found update message to success', function(assert) {
assert.expect(2);
this.render(hbs`{{async-image-wrapper src='http://emberjs.com/images/tomsters/sandiego-zoey.png'}}`);
return wait().then( () => {
assert.equal(this.$('#image-loaded-text').text(), 'image loaded');
assert.equal(this.$('#image-width-text').text(), '412');
});
});
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.10.4",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.6.0",
"ember-data": "2.6.0",
"ember-template-compiler": "2.6.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment