2015-08-12 10:33:11 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3
|
|
|
|
* or later.
|
|
|
|
*
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('jquery.avatar tests', function() {
|
|
|
|
|
|
|
|
var $div;
|
2017-11-06 11:57:59 +03:00
|
|
|
var devicePixelRatio;
|
2015-08-12 10:33:11 +03:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
$('#testArea').append($('<div id="avatardiv">'));
|
|
|
|
$div = $('#avatardiv');
|
|
|
|
|
|
|
|
devicePixelRatio = window.devicePixelRatio;
|
|
|
|
window.devicePixelRatio = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
$div.remove();
|
|
|
|
|
2017-11-06 11:57:59 +03:00
|
|
|
window.devicePixelRatio = devicePixelRatio;
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('size', function() {
|
|
|
|
it('undefined', function() {
|
|
|
|
$div.avatar('foo');
|
|
|
|
|
|
|
|
expect($div.height()).toEqual(64);
|
|
|
|
expect($div.width()).toEqual(64);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('undefined but div has height', function() {
|
|
|
|
$div.height(9);
|
|
|
|
$div.avatar('foo');
|
|
|
|
|
|
|
|
expect($div.height()).toEqual(9);
|
|
|
|
expect($div.width()).toEqual(9);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('undefined but data size is set', function() {
|
|
|
|
$div.data('size', 10);
|
|
|
|
$div.avatar('foo');
|
|
|
|
|
|
|
|
expect($div.height()).toEqual(10);
|
|
|
|
expect($div.width()).toEqual(10);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('defined', function() {
|
|
|
|
$div.avatar('foo', 8);
|
|
|
|
|
|
|
|
expect($div.height()).toEqual(8);
|
|
|
|
expect($div.width()).toEqual(8);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('undefined user', function() {
|
|
|
|
spyOn($div, 'imageplaceholder');
|
2017-09-20 18:19:35 +03:00
|
|
|
spyOn($div, 'css');
|
2015-08-12 10:33:11 +03:00
|
|
|
|
|
|
|
$div.avatar();
|
|
|
|
|
2016-11-23 20:02:23 +03:00
|
|
|
expect($div.imageplaceholder).toHaveBeenCalledWith('?');
|
2017-09-20 18:19:35 +03:00
|
|
|
expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('no avatar', function() {
|
|
|
|
it('show placeholder for existing user', function() {
|
|
|
|
spyOn($div, 'imageplaceholder');
|
|
|
|
$div.avatar('foo');
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'application/json' },
|
|
|
|
JSON.stringify({
|
|
|
|
data: {displayname: 'bar'}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('show placeholder for non existing user', function() {
|
|
|
|
spyOn($div, 'imageplaceholder');
|
2017-09-20 18:19:35 +03:00
|
|
|
spyOn($div, 'css');
|
2015-08-12 10:33:11 +03:00
|
|
|
$div.avatar('foo');
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'application/json' },
|
|
|
|
JSON.stringify({
|
|
|
|
data: {}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2017-09-20 19:14:00 +03:00
|
|
|
expect($div.imageplaceholder).toHaveBeenCalledWith('?');
|
2017-09-20 18:19:35 +03:00
|
|
|
expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
|
2015-08-12 10:33:11 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('show no placeholder', function() {
|
|
|
|
spyOn($div, 'imageplaceholder');
|
|
|
|
$div.avatar('foo', undefined, undefined, true);
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'application/json' },
|
|
|
|
JSON.stringify({
|
|
|
|
data: {}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
expect($div.imageplaceholder.calls.any()).toEqual(false);
|
|
|
|
expect($div.css('display')).toEqual('none');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('url generation', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
window.devicePixelRatio = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('default', function() {
|
|
|
|
window.devicePixelRatio = 1;
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
|
|
|
expect(fakeServer.requests[0].method).toEqual('GET');
|
|
|
|
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/32');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('high DPI icon', function() {
|
|
|
|
window.devicePixelRatio = 4;
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
|
|
|
expect(fakeServer.requests[0].method).toEqual('GET');
|
|
|
|
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/128');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('high DPI icon round up size', function() {
|
|
|
|
window.devicePixelRatio = 1.9;
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
|
|
|
expect(fakeServer.requests[0].method).toEqual('GET');
|
|
|
|
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/61');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('valid avatar', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
window.devicePixelRatio = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('default (no ie8 fix)', function() {
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'image/jpeg' },
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
var img = $div.children('img')[0];
|
|
|
|
|
|
|
|
expect(img.height).toEqual(32);
|
|
|
|
expect(img.width).toEqual(32);
|
|
|
|
expect(img.src).toEqual('http://localhost/index.php/avatar/foo/32');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('default high DPI icon', function() {
|
|
|
|
window.devicePixelRatio = 1.9;
|
|
|
|
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'image/jpeg' },
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
var img = $div.children('img')[0];
|
|
|
|
|
|
|
|
expect(img.height).toEqual(32);
|
|
|
|
expect(img.width).toEqual(32);
|
|
|
|
expect(img.src).toEqual('http://localhost/index.php/avatar/foo/61');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with ie8 fix', function() {
|
2017-04-24 11:39:37 +03:00
|
|
|
sinon.stub(Math, 'random').callsFake(function() {
|
2015-08-12 10:33:11 +03:00
|
|
|
return 0.5;
|
|
|
|
});
|
|
|
|
|
|
|
|
$div.avatar('foo', 32, true);
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'image/jpeg' },
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
var img = $div.children('img')[0];
|
|
|
|
|
|
|
|
expect(img.height).toEqual(32);
|
|
|
|
expect(img.width).toEqual(32);
|
|
|
|
expect(img.src).toEqual('http://localhost/index.php/avatar/foo/32#500');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('unhide div', function() {
|
|
|
|
$div.hide();
|
|
|
|
|
|
|
|
$div.avatar('foo', 32);
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'image/jpeg' },
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
expect($div.css('display')).toEqual('block');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('callback called', function() {
|
|
|
|
var observer = {callback: function() { dump("FOO"); }};
|
|
|
|
|
|
|
|
spyOn(observer, 'callback');
|
|
|
|
|
|
|
|
$div.avatar('foo', 32, undefined, undefined, function() {
|
|
|
|
observer.callback();
|
|
|
|
});
|
|
|
|
|
|
|
|
fakeServer.requests[0].respond(
|
|
|
|
200,
|
|
|
|
{ 'Content-Type': 'image/jpeg' },
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(observer.callback).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|