2015-10-26 14:48:38 +03:00
|
|
|
/* jshint expr:true */
|
|
|
|
/* global md5 */
|
2016-11-14 16:16:51 +03:00
|
|
|
import {expect} from 'chai';
|
2016-11-24 01:50:57 +03:00
|
|
|
import {describe, it} from 'mocha';
|
|
|
|
import {setupComponentTest} from 'ember-mocha';
|
2015-10-26 14:48:38 +03:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2016-06-30 13:21:47 +03:00
|
|
|
import Service from 'ember-service';
|
|
|
|
import run from 'ember-runloop';
|
2016-04-12 14:34:40 +03:00
|
|
|
import Pretender from 'pretender';
|
|
|
|
import wait from 'ember-test-helpers/wait';
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-06-11 19:52:36 +03:00
|
|
|
let pathsStub = Service.extend({
|
2015-10-28 14:36:45 +03:00
|
|
|
url: {
|
|
|
|
api() {
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
asset(src) {
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-04-12 14:34:40 +03:00
|
|
|
const stubKnownGravatar = function (server) {
|
|
|
|
server.get('http://www.gravatar.com/avatar/:md5', function () {
|
|
|
|
return [200, {'Content-Type': 'image/png'}, ''];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const stubUnknownGravatar = function (server) {
|
|
|
|
server.get('http://www.gravatar.com/avatar/:md5', function () {
|
|
|
|
return [404, {}, ''];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
describe('Integration: Component: gh-profile-image', function () {
|
|
|
|
setupComponentTest('gh-profile-image', {
|
2015-10-26 14:48:38 +03:00
|
|
|
integration: true
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
let server;
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
this.register('service:ghost-paths', pathsStub);
|
|
|
|
this.inject.service('ghost-paths', {as: 'ghost-paths'});
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
server = new Pretender();
|
|
|
|
stubKnownGravatar(server);
|
|
|
|
});
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
afterEach(function () {
|
|
|
|
server.shutdown();
|
|
|
|
});
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('renders', function () {
|
|
|
|
this.set('email', '');
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`
|
|
|
|
{{gh-profile-image email=email}}
|
|
|
|
`);
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$()).to.have.length(1);
|
|
|
|
});
|
2015-12-21 14:08:49 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('renders and tears down ok with fileStorage:false', function () {
|
|
|
|
this.set('fileStorage', false);
|
2015-12-21 14:08:49 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`
|
|
|
|
{{gh-profile-image fileStorage=fileStorage}}
|
|
|
|
`);
|
2015-12-21 14:08:49 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$()).to.have.length(1);
|
|
|
|
expect(this.$('input')).to.have.length(0);
|
|
|
|
}),
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('renders default image if no email supplied', function () {
|
|
|
|
this.set('email', null);
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`
|
|
|
|
{{gh-profile-image email=email size=100 debounce=50}}
|
|
|
|
`);
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
|
|
|
|
.to.be.blank;
|
|
|
|
});
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('renders the gravatar if valid email supplied', function (done) {
|
|
|
|
let email = 'test@example.com';
|
|
|
|
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=404`;
|
|
|
|
|
|
|
|
this.set('email', email);
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`
|
|
|
|
{{gh-profile-image email=email size=100 debounce=50}}
|
|
|
|
`);
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
// wait for the ajax request to complete
|
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
|
|
|
|
.to.equal(`background-image: url(${expectedUrl})`);
|
|
|
|
done();
|
2016-04-12 14:34:40 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('doesn\'t add background url if gravatar image doesn\'t exist', function (done) {
|
|
|
|
stubUnknownGravatar(server);
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`
|
|
|
|
{{gh-profile-image email="test@example.com" size=100 debounce=50}}
|
|
|
|
`);
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
|
|
|
|
.to.be.blank;
|
|
|
|
done();
|
2016-04-12 14:34:40 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('throttles gravatar loading as email is changed', function (done) {
|
|
|
|
let email = 'test@example.com';
|
|
|
|
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=404`;
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.set('email', 'test');
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`
|
|
|
|
{{gh-profile-image email=email size=100 debounce=300}}
|
|
|
|
`);
|
2015-10-26 14:48:38 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run(() => {
|
|
|
|
this.set('email', email);
|
|
|
|
});
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$('.gravatar-img').attr('style'), '.gravatar-img background not immediately changed on email change')
|
|
|
|
.to.be.blank;
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('.gravatar-img').attr('style'), '.gravatar-img background still not changed before debounce timeout')
|
|
|
|
.to.be.blank;
|
|
|
|
}, 250);
|
2016-04-12 14:34:40 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('.gravatar-img').attr('style'), '.gravatar-img background changed after debounce timeout')
|
|
|
|
.to.equal(`background-image: url(${expectedUrl})`);
|
|
|
|
done();
|
|
|
|
}, 400);
|
|
|
|
});
|
|
|
|
});
|