Ghost/ghost/admin/tests/integration/components/gh-profile-image-test.js
Fixer fc706d1c02 Additional check of privacy.useGravatar for gh-profile-image component (#761)
closes TryGhost/Ghost#8612

* Additional check of privacy.useGravatar for gh-profile-image component
- added a check for privacy.useGravatar flag
- checked: tests are OK

* fix other issues in gh-profile-image
- we had CPs with side-effects 🤢
  - replace CP with basic properties that can be set within the component
  - use `didReceiveAttrs` and `ember-concurrency` to debounce changes to the email property and make the functionality easier to reason about
- fix the broken fade-in animation when the avatar changes
- fix tests - `.to.be.blank` was always returning true, replaced with `to.be.empty` which caused the tests to fail properly then replaced them with the expectations for the actual values
2017-07-06 16:33:15 +07:00

164 lines
4.7 KiB
JavaScript

/* jshint expr:true */
/* global md5 */
import Pretender from 'pretender';
import Service from 'ember-service';
import hbs from 'htmlbars-inline-precompile';
import run from 'ember-runloop';
import wait from 'ember-test-helpers/wait';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {setupComponentTest} from 'ember-mocha';
import {timeout} from 'ember-concurrency';
let pathsStub = Service.extend({
url: {
api() {
return '';
},
asset(src) {
return src;
}
}
});
const stubKnownGravatar = function (server) {
server.get('http://www.gravatar.com/avatar/:md5', function () {
return [200, {'Content-Type': 'image/png'}, ''];
});
server.head('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, {}, ''];
});
server.head('http://www.gravatar.com/avatar/:md5', function () {
return [404, {}, ''];
});
};
let configStubuseGravatar = Service.extend({
useGravatar: true
});
describe('Integration: Component: gh-profile-image', function () {
setupComponentTest('gh-profile-image', {
integration: true
});
let server;
beforeEach(function () {
this.register('service:ghost-paths', pathsStub);
this.inject.service('ghost-paths', {as: 'ghost-paths'});
this.register('service:config', configStubuseGravatar);
this.inject.service('config', {as: 'config'});
server = new Pretender();
stubKnownGravatar(server);
});
afterEach(function () {
server.shutdown();
});
it('renders', function () {
this.set('email', '');
this.render(hbs`
{{gh-profile-image email=email}}
`);
expect(this.$()).to.have.length(1);
});
it('renders default image if no email supplied', function () {
this.set('email', null);
this.render(hbs`
{{gh-profile-image email=email size=100 debounce=50}}
`);
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
.to.equal('display: none');
});
it('renders the gravatar if valid email supplied and privacy.useGravatar allows it', async function () {
let email = 'test@example.com';
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=404`;
this.set('email', email);
this.render(hbs`
{{gh-profile-image email=email size=100 debounce=50}}
`);
// wait for the ajax request to complete
await wait();
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
.to.equal(`background-image: url(${expectedUrl}); display: block`);
});
it('doesn\'t render the gravatar if valid email supplied but privacy.useGravatar forbids it', async function () {
let email = 'test@example.com';
this.set('email', email);
this.set('config.useGravatar', false);
this.render(hbs`
{{gh-profile-image email=email size=100 debounce=50}}
`);
await wait();
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
.to.equal('display: none');
});
it('doesn\'t add background url if gravatar image doesn\'t exist', async function () {
stubUnknownGravatar(server);
this.render(hbs`
{{gh-profile-image email="test@example.com" size=100 debounce=50}}
`);
await wait();
expect(this.$('.gravatar-img').attr('style'), 'gravatar image style')
.to.equal('background-image: url(); display: none');
});
it('throttles gravatar loading as email is changed', async function () {
let email = 'test@example.com';
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=404`;
this.set('email', 'test');
this.render(hbs`
{{gh-profile-image email=email size=100 debounce=300}}
`);
run(() => {
this.set('email', email);
});
expect(this.$('.gravatar-img').attr('style'), '.gravatar-img background not immediately changed on email change')
.to.equal('display: none');
await timeout(250);
expect(this.$('.gravatar-img').attr('style'), '.gravatar-img background still not changed before debounce timeout')
.to.equal('display: none');
await timeout(100);
expect(this.$('.gravatar-img').attr('style'), '.gravatar-img background changed after debounce timeout')
.to.equal(`background-image: url(${expectedUrl}); display: block`);
});
});