Skip to content

Instantly share code, notes, and snippets.

@chrism
Last active August 11, 2016 11:22
Show Gist options
  • Save chrism/0c452892310071d81d43f99f8619ede9 to your computer and use it in GitHub Desktop.
Save chrism/0c452892310071d81d43f99f8619ede9 to your computer and use it in GitHub Desktop.
Testing async behaviour - 2
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'img',
attributeBindings: ['src'],
simpleLoadImage() {
let img = new Image();
img.src = this.get('src');
img.onload = () => {
Ember.run( () => {
this.get('onImageLoad')('image now loaded');
});
};
},
returnSwatchesOnInsert: Ember.on('didInsertElement', function() {
Ember.run.scheduleOnce('afterRender', this, this.simpleLoadImage);
})
});
import Ember from 'ember';
export default Ember.Controller.extend({
imageLoaded: 'image not yet loaded',
actions: {
imageLoaded(message) {
this.set('imageLoaded', message);
}
}
});
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>2 - Illustrates that the test fails if the async behaviour isn't properly considered</h2>
<p>Even though we use the wait helper in the test with a large image it's clear that the async test fails depending on whether the image has loaded in time (ie from cache)/p>
<p><strong>{{imageLoaded}}</strong></p>
{{async-image src='http://emberjs.com/images/tomsters/sandiego-zoey.png' onImageLoad=(action 'imageLoaded')}}
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', 'Tests for async behaviour', {
integration: true
});
test('onImageLoad gets fired', function(assert) {
assert.expect(2);
let expectedText = 'yolo';
this.set('stubbedAction', (actual) => {
let expectedText = 'image now loaded';
assert.deepEqual(actual, expectedText, 'action fired with parameter');
});
this.render(hbs`{{async-image src='http://imgsrc.hubblesite.org/hu/db/images/hs-2016-19-a-print.jpg' onImageLoad=(action stubbedAction)}}`);
return wait().then( () => {
assert.ok(expectedText, 'yolo');
});
});
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