jquery.avatarSpec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. spyOn($div, 'css');
  50. $div.avatar();
  51. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  52. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  53. });
  54. describe('no avatar', function() {
  55. it('show placeholder for existing user', function() {
  56. spyOn($div, 'imageplaceholder');
  57. $div.avatar('foo');
  58. fakeServer.requests[0].respond(
  59. 200,
  60. { 'Content-Type': 'application/json' },
  61. JSON.stringify({
  62. data: {displayname: 'bar'}
  63. })
  64. );
  65. expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'bar');
  66. });
  67. it('show placeholder for non existing user', function() {
  68. spyOn($div, 'imageplaceholder');
  69. spyOn($div, 'css');
  70. $div.avatar('foo');
  71. fakeServer.requests[0].respond(
  72. 200,
  73. { 'Content-Type': 'application/json' },
  74. JSON.stringify({
  75. data: {}
  76. })
  77. );
  78. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  79. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  80. });
  81. it('show no placeholder', function() {
  82. spyOn($div, 'imageplaceholder');
  83. $div.avatar('foo', undefined, undefined, true);
  84. fakeServer.requests[0].respond(
  85. 200,
  86. { 'Content-Type': 'application/json' },
  87. JSON.stringify({
  88. data: {}
  89. })
  90. );
  91. expect($div.imageplaceholder.calls.any()).toEqual(false);
  92. expect($div.css('display')).toEqual('none');
  93. });
  94. });
  95. describe('url generation', function() {
  96. beforeEach(function() {
  97. window.devicePixelRatio = 1;
  98. });
  99. it('default', function() {
  100. window.devicePixelRatio = 1;
  101. $div.avatar('foo', 32);
  102. expect(fakeServer.requests[0].method).toEqual('GET');
  103. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/32');
  104. });
  105. it('high DPI icon', function() {
  106. window.devicePixelRatio = 4;
  107. $div.avatar('foo', 32);
  108. expect(fakeServer.requests[0].method).toEqual('GET');
  109. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/128');
  110. });
  111. it('high DPI icon round up size', function() {
  112. window.devicePixelRatio = 1.9;
  113. $div.avatar('foo', 32);
  114. expect(fakeServer.requests[0].method).toEqual('GET');
  115. expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/avatar/foo/61');
  116. });
  117. });
  118. describe('valid avatar', function() {
  119. beforeEach(function() {
  120. window.devicePixelRatio = 1;
  121. });
  122. it('default (no ie8 fix)', function() {
  123. $div.avatar('foo', 32);
  124. fakeServer.requests[0].respond(
  125. 200,
  126. { 'Content-Type': 'image/jpeg' },
  127. ''
  128. );
  129. var img = $div.children('img')[0];
  130. expect(img.height).toEqual(32);
  131. expect(img.width).toEqual(32);
  132. expect(img.src).toEqual('http://localhost/index.php/avatar/foo/32');
  133. });
  134. it('default high DPI icon', function() {
  135. window.devicePixelRatio = 1.9;
  136. $div.avatar('foo', 32);
  137. fakeServer.requests[0].respond(
  138. 200,
  139. { 'Content-Type': 'image/jpeg' },
  140. ''
  141. );
  142. var img = $div.children('img')[0];
  143. expect(img.height).toEqual(32);
  144. expect(img.width).toEqual(32);
  145. expect(img.src).toEqual('http://localhost/index.php/avatar/foo/61');
  146. });
  147. it('with ie8 fix', function() {
  148. sinon.stub(Math, 'random').callsFake(function() {
  149. return 0.5;
  150. });
  151. $div.avatar('foo', 32, true);
  152. fakeServer.requests[0].respond(
  153. 200,
  154. { 'Content-Type': 'image/jpeg' },
  155. ''
  156. );
  157. var img = $div.children('img')[0];
  158. expect(img.height).toEqual(32);
  159. expect(img.width).toEqual(32);
  160. expect(img.src).toEqual('http://localhost/index.php/avatar/foo/32#500');
  161. });
  162. it('unhide div', function() {
  163. $div.hide();
  164. $div.avatar('foo', 32);
  165. fakeServer.requests[0].respond(
  166. 200,
  167. { 'Content-Type': 'image/jpeg' },
  168. ''
  169. );
  170. expect($div.css('display')).toEqual('block');
  171. });
  172. it('callback called', function() {
  173. var observer = {callback: function() { dump("FOO"); }};
  174. spyOn(observer, 'callback');
  175. $div.avatar('foo', 32, undefined, undefined, function() {
  176. observer.callback();
  177. });
  178. fakeServer.requests[0].respond(
  179. 200,
  180. { 'Content-Type': 'image/jpeg' },
  181. ''
  182. );
  183. expect(observer.callback).toHaveBeenCalled();
  184. });
  185. });
  186. });