jquery.avatarSpec.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. spyOn(window, 'Image').and.returnValue({
  19. onload: function() {
  20. },
  21. onerror: function() {
  22. }
  23. });
  24. });
  25. afterEach(function() {
  26. $div.remove();
  27. window.devicePixelRatio = devicePixelRatio;
  28. });
  29. describe('size', function() {
  30. it('undefined', function() {
  31. $div.avatar('foo');
  32. expect(Math.round($div.height())).toEqual(64);
  33. expect(Math.round($div.width())).toEqual(64);
  34. });
  35. it('undefined but div has height', function() {
  36. $div.height(9);
  37. $div.avatar('foo');
  38. expect(window.Image).toHaveBeenCalled();
  39. window.Image().onerror();
  40. expect(Math.round($div.height())).toEqual(9);
  41. expect(Math.round($div.width())).toEqual(9);
  42. });
  43. it('undefined but data size is set', function() {
  44. $div.data('size', 10);
  45. $div.avatar('foo');
  46. expect(window.Image).toHaveBeenCalled();
  47. window.Image().onerror();
  48. expect(Math.round($div.height())).toEqual(10);
  49. expect(Math.round($div.width())).toEqual(10);
  50. });
  51. it('defined', function() {
  52. $div.avatar('foo', 8);
  53. expect(window.Image).toHaveBeenCalled();
  54. window.Image().onerror();
  55. expect(Math.round($div.height())).toEqual(8);
  56. expect(Math.round($div.width())).toEqual(8);
  57. });
  58. });
  59. it('undefined user', function() {
  60. spyOn($div, 'imageplaceholder');
  61. spyOn($div, 'css');
  62. $div.avatar();
  63. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  64. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  65. });
  66. describe('no avatar', function() {
  67. it('show placeholder for existing user', function() {
  68. spyOn($div, 'imageplaceholder');
  69. $div.avatar('foo', undefined, undefined, undefined, undefined, 'bar');
  70. expect(window.Image).toHaveBeenCalled();
  71. window.Image().onerror();
  72. expect($div.imageplaceholder).toHaveBeenCalledWith('foo', 'bar');
  73. });
  74. it('show placeholder for non existing user', function() {
  75. spyOn($div, 'imageplaceholder');
  76. spyOn($div, 'css');
  77. $div.avatar('foo');
  78. expect(window.Image).toHaveBeenCalled();
  79. window.Image().onerror();
  80. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  81. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  82. });
  83. it('show no placeholder is ignored', function() {
  84. spyOn($div, 'imageplaceholder');
  85. spyOn($div, 'css');
  86. $div.avatar('foo', undefined, undefined, true);
  87. expect(window.Image).toHaveBeenCalled();
  88. window.Image().onerror();
  89. expect($div.imageplaceholder).toHaveBeenCalledWith('?');
  90. expect($div.css).toHaveBeenCalledWith('background-color', '#b9b9b9');
  91. });
  92. });
  93. describe('url generation', function() {
  94. beforeEach(function() {
  95. window.devicePixelRatio = 1;
  96. });
  97. it('default', function() {
  98. window.devicePixelRatio = 1;
  99. $div.avatar('foo', 32);
  100. expect(window.Image).toHaveBeenCalled();
  101. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  102. });
  103. it('high DPI icon', function() {
  104. window.devicePixelRatio = 4;
  105. $div.avatar('foo', 32);
  106. expect(window.Image).toHaveBeenCalled();
  107. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/128');
  108. });
  109. it('high DPI icon round up size', function() {
  110. window.devicePixelRatio = 1.9;
  111. $div.avatar('foo', 32);
  112. expect(window.Image).toHaveBeenCalled();
  113. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
  114. });
  115. });
  116. describe('valid avatar', function() {
  117. beforeEach(function() {
  118. window.devicePixelRatio = 1;
  119. });
  120. it('default (no ie8 fix)', function() {
  121. $div.avatar('foo', 32);
  122. expect(window.Image).toHaveBeenCalled();
  123. window.Image().onload();
  124. expect(window.Image().height).toEqual(32);
  125. expect(window.Image().width).toEqual(32);
  126. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  127. });
  128. it('default high DPI icon', function() {
  129. window.devicePixelRatio = 1.9;
  130. $div.avatar('foo', 32);
  131. expect(window.Image).toHaveBeenCalled();
  132. window.Image().onload();
  133. expect(window.Image().height).toEqual(32);
  134. expect(window.Image().width).toEqual(32);
  135. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/61');
  136. });
  137. it('with ie8 fix (ignored)', function() {
  138. $div.avatar('foo', 32, true);
  139. expect(window.Image).toHaveBeenCalled();
  140. window.Image().onload();
  141. expect(window.Image().height).toEqual(32);
  142. expect(window.Image().width).toEqual(32);
  143. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  144. });
  145. it('unhide div', function() {
  146. $div.hide();
  147. $div.avatar('foo', 32);
  148. expect(window.Image).toHaveBeenCalled();
  149. window.Image().onload();
  150. expect(window.Image().height).toEqual(32);
  151. expect(window.Image().width).toEqual(32);
  152. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  153. });
  154. it('callback called', function() {
  155. var observer = {callback: function() { dump("FOO"); }};
  156. spyOn(observer, 'callback');
  157. $div.avatar('foo', 32, undefined, undefined, function() {
  158. observer.callback();
  159. });
  160. expect(window.Image).toHaveBeenCalled();
  161. window.Image().onload();
  162. expect(window.Image().height).toEqual(32);
  163. expect(window.Image().width).toEqual(32);
  164. expect(window.Image().src).toEqual('http://localhost/index.php/avatar/foo/32');
  165. expect(observer.callback).toHaveBeenCalled();
  166. });
  167. });
  168. });