Ghost/ghost/admin/tests/integration/components/gh-profile-image-test.js

175 lines
5.0 KiB
JavaScript
Raw Normal View History

import Pretender from 'pretender';
import Service from '@ember/service';
import hbs from 'htmlbars-inline-precompile';
import md5 from 'blueimp-md5';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {find, render} from '@ember/test-helpers';
import {setupRenderingTest} from 'ember-mocha';
import {timeout} from 'ember-concurrency';
let pathsStub = Service.extend({
assetRoot: '/ghost/assets/',
init() {
this._super(...arguments);
this.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
});
2016-11-24 01:50:57 +03:00
describe('Integration: Component: gh-profile-image', function () {
setupRenderingTest();
2016-11-24 01:50:57 +03:00
let server;
2016-11-24 01:50:57 +03:00
beforeEach(function () {
this.owner.register('service:ghost-paths', pathsStub);
this.owner.register('service:config', configStubuseGravatar);
2016-11-24 01:50:57 +03:00
server = new Pretender();
stubKnownGravatar(server);
});
2016-11-24 01:50:57 +03:00
afterEach(function () {
server.shutdown();
});
it('renders', async function () {
2016-11-24 01:50:57 +03:00
this.set('email', '');
await render(hbs`
2016-11-24 01:50:57 +03:00
{{gh-profile-image email=email}}
`);
expect(find('.account-image')).to.exist;
expect(find('.placeholder-img')).to.exist;
expect(find('input[type="file"]')).to.exist;
2016-11-24 01:50:57 +03:00
});
it('renders default image if no email supplied', async function () {
2016-11-24 01:50:57 +03:00
this.set('email', null);
await render(hbs`
2016-11-24 01:50:57 +03:00
{{gh-profile-image email=email size=100 debounce=50}}
`);
expect(
find('.gravatar-img'),
'gravatar image style'
).to.have.attribute('style', 'display: none');
2016-11-24 01:50:57 +03:00
});
it('renders the gravatar if valid email supplied and privacy.useGravatar allows it', async function () {
2016-11-24 01:50:57 +03:00
let email = 'test@example.com';
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=404`;
this.set('email', email);
await render(hbs`
2016-11-24 01:50:57 +03:00
{{gh-profile-image email=email size=100 debounce=50}}
`);
expect(
find('.gravatar-img'),
'gravatar image style'
).to.have.attribute('style', `background-image: url(${expectedUrl}); display: block`);
});
it('doesn\'t render the gravatar if valid email supplied but privacy.useGravatar forbids it', async function () {
let config = this.owner.lookup('service:config');
let email = 'test@example.com';
this.set('email', email);
config.set('useGravatar', false);
await render(hbs`
{{gh-profile-image email=email size=100 debounce=50}}
`);
expect(
find('.gravatar-img'),
'gravatar image style'
).to.have.attribute('style', 'display: none');
2016-11-24 01:50:57 +03:00
});
it('doesn\'t add background url if gravatar image doesn\'t exist', async function () {
2016-11-24 01:50:57 +03:00
stubUnknownGravatar(server);
await render(hbs`
2016-11-24 01:50:57 +03:00
{{gh-profile-image email="test@example.com" size=100 debounce=50}}
`);
expect(
find('.gravatar-img'),
'gravatar image style'
).to.have.attribute('style', 'background-image: url(); display: none');
2016-11-24 01:50:57 +03:00
});
// skipped due to random failures on Travis - https://github.com/TryGhost/Ghost/issues/10308
it.skip('throttles gravatar loading as email is changed', async function () {
2016-11-24 01:50:57 +03:00
let email = 'test@example.com';
let expectedUrl = `//www.gravatar.com/avatar/${md5(email)}?s=100&d=404`;
2016-11-24 01:50:57 +03:00
this.set('email', 'test');
await render(hbs`
2016-11-24 01:50:57 +03:00
{{gh-profile-image email=email size=100 debounce=300}}
`);
this.set('email', email);
await timeout(50);
expect(
find('.gravatar-img'),
'.gravatar-img background not immediately changed on email change'
).to.have.attribute('style', 'display: none');
await timeout(250);
expect(
find('.gravatar-img'),
'.gravatar-img background still not changed before debounce timeout'
).to.have.attribute('style', 'display: none');
await timeout(100);
expect(
find('.gravatar-img'),
'.gravatar-img background changed after debounce timeout'
).to.have.attribute('style', `background-image: url(${expectedUrl}); display: block`);
2016-11-24 01:50:57 +03:00
});
});