jquery.avatarSpec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Copyright (c) 2015 Roeland Jago Douma <roeland@famdouma.nl>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. describe('jquery.avatar tests', function() {
  11. var $div;
  12. var devicePixelRatio
  13. beforeEach(function() {
  14. $('#testArea').append($('<div id="avatardiv">'));
  15. $div = $('#avatardiv');
  16. devicePixelRatio = window.devicePixelRatio;
  17. window.devicePixelRatio = 1;
  18. });
  19. afterEach(function() {
  20. $div.remove();
  21. window.devicePixelRatio = devicePixelRatio
  22. });
  23. describe('size', function() {
  24. it('undefined', function() {
  25. $div.avatar('foo');
  26. expect($div.height()).toEqual(64);
  27. expect($div.width()).toEqual(64);
  28. });
  29. it('undefined but div has height', function() {
  30. $div.height(9);
  31. $div.avatar('foo');
  32. expect($div.height()).toEqual(9);
  33. expect($div.width()).toEqual(9);
  34. });
  35. it('undefined but data size is set', function() {
  36. $div.data('size', 10);
  37. $div.avatar('foo');
  38. expect($div.height()).toEqual(10);
  39. expect($div.width()).toEqual(10);
  40. });
  41. it('defined', function() {
  42. $div.avatar('foo', 8);
  43. expect($div.height()).toEqual(8);
  44. expect($div.width()).toEqual(8);
  45. });
  46. });
  47. it('undefined user', function() {
  48. spyOn($div, 'imageplaceholder');
  49. $div.avatar();
  50. expect($div.imageplaceholder).toHaveBeenCalledWith('x');
  51. });
  52. describe('no avatar', function() {
  53. it('show placeholder for existing user', function() {
  54. spyOn($div, 'imageplaceholder');
  55. $div.avatar('foo');
  56. fakeServer.requests[0].respond(
  57. 200,
  58. { 'Content-Type': 'application/json' },
  59. JSON.stringify({
  60. data: {displayname: 'bar'}
  61. })
  62. );
  63. expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'bar');
  64. });
  65. it('show placeholder for non existing user', function() {
  66. spyOn($div, 'imageplaceholder');
  67. $div.avatar('foo');
  68. fakeServer.requests[0].respond(
  69. 200,
  70. { 'Content-Type': 'application/json' },
  71. JSON.stringify({
  72. data: {}
  73. })
  74. );
  75. expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'X');
  76. });
  77. it('show no placeholder', function() {
  78. spyOn($div, 'imageplaceholder');
  79. $div.avatar('foo', undefined, undefined, true);
  80. fakeServer.requests[0].respond(
  81. 200,
  82. { 'Content-Type': 'application/json' },
  83. JSON.stringify({
  84. data: {}
  85. })
  86. );
  87. expect($div.imageplaceholder.calls.any()).toEqual(false);
  88. expect($div.css('display')).toEqual('none');
  89. });
  90. });
  91. describe('url generation', function() {
  92. beforeEach(function() {
  93. window.devicePixelRatio = 1;
  94. });
  95. it('default', function() {
  96. window.devicePixelRatio = 1;
  97. $div.avatar('foo', 32);
  98. expect(fakeServer.requests[0].method).toEqual('GET');
  99. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/32');
  100. });
  101. it('high DPI icon', function() {
  102. window.devicePixelRatio = 4;
  103. $div.avatar('foo', 32);
  104. expect(fakeServer.requests[0].method).toEqual('GET');
  105. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/128');
  106. });
  107. it('high DPI icon round up size', function() {
  108. window.devicePixelRatio = 1.9;
  109. $div.avatar('foo', 32);
  110. expect(fakeServer.requests[0].method).toEqual('GET');
  111. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/61');
  112. });
  113. });
  114. describe('valid avatar', function() {
  115. beforeEach(function() {
  116. window.devicePixelRatio = 1;
  117. });
  118. it('default (no ie8 fix)', function() {
  119. $div.avatar('foo', 32);
  120. fakeServer.requests[0].respond(
  121. 200,
  122. { 'Content-Type': 'image/jpeg' },
  123. ''
  124. );
  125. var img = $div.children('img')[0];
  126. expect(img.height).toEqual(32);
  127. expect(img.width).toEqual(32);
  128. expect(img.src).toEqual('http://localhost/index.php/avatar/foo/32');
  129. });
  130. it('default high DPI icon', function() {
  131. window.devicePixelRatio = 1.9;
  132. $div.avatar('foo', 32);
  133. fakeServer.requests[0].respond(
  134. 200,
  135. { 'Content-Type': 'image/jpeg' },
  136. ''
  137. );
  138. var img = $div.children('img')[0];
  139. expect(img.height).toEqual(32);
  140. expect(img.width).toEqual(32);
  141. expect(img.src).toEqual('http://localhost/index.php/avatar/foo/61');
  142. });
  143. it('with ie8 fix', function() {
  144. sinon.stub(Math, 'random', function() {
  145. return 0.5;
  146. });
  147. $div.avatar('foo', 32, true);
  148. fakeServer.requests[0].respond(
  149. 200,
  150. { 'Content-Type': 'image/jpeg' },
  151. ''
  152. );
  153. var img = $div.children('img')[0];
  154. expect(img.height).toEqual(32);
  155. expect(img.width).toEqual(32);
  156. expect(img.src).toEqual('http://localhost/index.php/avatar/foo/32#500');
  157. });
  158. it('unhide div', function() {
  159. $div.hide();
  160. $div.avatar('foo', 32);
  161. fakeServer.requests[0].respond(
  162. 200,
  163. { 'Content-Type': 'image/jpeg' },
  164. ''
  165. );
  166. expect($div.css('display')).toEqual('block');
  167. });
  168. it('callback called', function() {
  169. var observer = {callback: function() { dump("FOO"); }};
  170. spyOn(observer, 'callback');
  171. $div.avatar('foo', 32, undefined, undefined, function() {
  172. observer.callback();
  173. });
  174. fakeServer.requests[0].respond(
  175. 200,
  176. { 'Content-Type': 'image/jpeg' },
  177. ''
  178. );
  179. expect(observer.callback).toHaveBeenCalled();
  180. });
  181. });
  182. });