index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import ChangeAvatarIndex from 'misago/components/change-avatar/index'; // jshint ignore:line
  4. import misago from 'misago/index';
  5. import snackbar from 'misago/services/snackbar';
  6. import * as testUtils from 'misago/utils/test-utils';
  7. let snackbarStore = null;
  8. let apiResponse = {
  9. "crop_tmp": false,
  10. "galleries": [
  11. {
  12. "images": [
  13. "avatars/Nature/arctic_fox.jpg",
  14. "avatars/Nature/baby_fox.jpg",
  15. "avatars/Nature/blackbird.jpg",
  16. "avatars/Nature/rabbit.jpg",
  17. "avatars/Nature/serval.jpg"
  18. ],
  19. "name": "Nature"
  20. },
  21. {
  22. "images": [
  23. "avatars/Space/andromeda.jpg",
  24. "avatars/Space/antennae_galaxies.jpg",
  25. "avatars/Space/barred_spiral_galaxy.jpg",
  26. "avatars/Space/messier_74.jpg",
  27. "avatars/Space/ngc_1672.jpg",
  28. "avatars/Space/ngc_4414.jpg"
  29. ],
  30. "name": "Space"
  31. }
  32. ],
  33. "crop_org": false,
  34. "upload": {
  35. "allowed_extensions": [
  36. ".gif",
  37. ".png",
  38. ".jpg",
  39. ".jpeg"
  40. ],
  41. "limit": 750000,
  42. "allowed_mime_types": [
  43. "image/gif",
  44. "image/jpeg",
  45. "image/png"
  46. ]
  47. },
  48. "generated": true,
  49. "gravatar": true
  50. };
  51. describe("Change Avatar Index", function() {
  52. beforeEach(function() {
  53. snackbarStore = testUtils.snackbarStoreMock();
  54. snackbar.init(snackbarStore);
  55. misago._context = {
  56. 'user': {
  57. 'id': 123,
  58. 'avatar_hash': 'aabbccdd',
  59. 'avatar_api_url': '/test-api/users/123/avatar/'
  60. }
  61. };
  62. });
  63. afterEach(function() {
  64. testUtils.unmountComponents();
  65. testUtils.snackbarClear(snackbar);
  66. $.mockjax.clear();
  67. });
  68. it("renders", function(done) {
  69. /* jshint ignore:start */
  70. testUtils.render(
  71. <ChangeAvatarIndex user={misago.get('user')}
  72. options={apiResponse} />
  73. );
  74. /* jshint ignore:end */
  75. testUtils.onElement('#test-mount .modal-avatar-index', function() {
  76. assert.ok(true, "component renders");
  77. done();
  78. });
  79. });
  80. it("renders without gravatar button", function(done) {
  81. /* jshint ignore:start */
  82. let amendedOptions = Object.assign({}, apiResponse, {gravatar: false});
  83. testUtils.render(
  84. <ChangeAvatarIndex user={misago.get('user')}
  85. options={amendedOptions} />
  86. );
  87. /* jshint ignore:end */
  88. testUtils.onElement('#test-mount .modal-avatar-index', function() {
  89. assert.ok(!$('#test-mount .btn-avatar-gravatar').length,
  90. "gravatar option is hidden");
  91. done();
  92. });
  93. });
  94. it("renders without gallery button", function(done) {
  95. /* jshint ignore:start */
  96. let amendedOptions = Object.assign({}, apiResponse, {galleries: false});
  97. testUtils.render(
  98. <ChangeAvatarIndex user={misago.get('user')}
  99. options={amendedOptions} />
  100. );
  101. /* jshint ignore:end */
  102. testUtils.onElement('#test-mount .modal-avatar-index', function() {
  103. assert.ok(!$('#test-mount .btn-avatar-gallery').length,
  104. "gallery option is hidden");
  105. done();
  106. });
  107. });
  108. it("shows alert with error on rejection", function(done) {
  109. $.mockjax({
  110. url: '/test-api/users/123/avatar/',
  111. status: 400,
  112. responseText: {
  113. detail: "You can't change avatar at the moment!"
  114. }
  115. });
  116. snackbarStore.callback(function(message) {
  117. assert.deepEqual(message, {
  118. message: "You can't change avatar at the moment!",
  119. type: 'error'
  120. }, "valid message was shown");
  121. done();
  122. });
  123. /* jshint ignore:start */
  124. testUtils.render(
  125. <ChangeAvatarIndex user={misago.get('user')}
  126. options={apiResponse} />
  127. );
  128. /* jshint ignore:end */
  129. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  130. });
  131. it("calls error callback on backend error", function(done) { // jshint ignore:line
  132. $.mockjax({
  133. url: '/test-api/users/123/avatar/',
  134. status: 403,
  135. responseText: {
  136. detail: "You need to sign in to change avatar."
  137. }
  138. });
  139. /* jshint ignore:start */
  140. let showError = function(error) {
  141. assert.equal(error.detail, "You need to sign in to change avatar.",
  142. "callback was called with backend error message");
  143. done();
  144. };
  145. testUtils.render(
  146. <ChangeAvatarIndex user={misago.get('user')}
  147. options={apiResponse}
  148. showError={showError} />
  149. );
  150. /* jshint ignore:end */
  151. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  152. });
  153. it("changes avatar to generated one successfully", function(done) { // jshint ignore:line
  154. $.mockjax({
  155. url: '/test-api/users/123/avatar/',
  156. status: 200,
  157. responseText: {
  158. detail: "Generated avataru set!",
  159. avatar_hash: 'n33wh44sh',
  160. options: apiResponse
  161. }
  162. });
  163. snackbarStore.callback(function(message) {
  164. assert.deepEqual(message, {
  165. message: "Generated avataru set!",
  166. type: 'success'
  167. }, "valid message was shown");
  168. });
  169. /* jshint ignore:start */
  170. let onComplete = function(avatarHash, options) {
  171. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  172. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  173. done();
  174. };
  175. testUtils.render(
  176. <ChangeAvatarIndex user={misago.get('user')}
  177. options={apiResponse}
  178. onComplete={onComplete} />
  179. );
  180. /* jshint ignore:end */
  181. testUtils.simulateClick('#test-mount .btn-avatar-generate');
  182. });
  183. it("changes avatar to gravatar successfully", function(done) { // jshint ignore:line
  184. $.mockjax({
  185. url: '/test-api/users/123/avatar/',
  186. status: 200,
  187. responseText: {
  188. detail: "Gravatar avataru set!",
  189. avatar_hash: 'n33wh44sh',
  190. options: apiResponse
  191. }
  192. });
  193. snackbarStore.callback(function(message) {
  194. assert.deepEqual(message, {
  195. message: "Gravatar avataru set!",
  196. type: 'success'
  197. }, "valid message was shown");
  198. });
  199. /* jshint ignore:start */
  200. let onComplete = function(avatarHash, options) {
  201. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  202. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  203. done();
  204. };
  205. testUtils.render(
  206. <ChangeAvatarIndex user={misago.get('user')}
  207. options={apiResponse}
  208. onComplete={onComplete} />
  209. );
  210. /* jshint ignore:end */
  211. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  212. });
  213. });