index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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("shows alert with error on rejection", function(done) {
  95. $.mockjax({
  96. url: '/test-api/users/123/avatar/',
  97. status: 400,
  98. responseText: {
  99. detail: "You can't change avatar at the moment!"
  100. }
  101. });
  102. snackbarStore.callback(function(message) {
  103. assert.deepEqual(message, {
  104. message: "You can't change avatar at the moment!",
  105. type: 'error'
  106. }, "valid message was shown");
  107. done();
  108. });
  109. /* jshint ignore:start */
  110. testUtils.render(
  111. <ChangeAvatarIndex user={misago.get('user')}
  112. options={apiResponse} />
  113. );
  114. /* jshint ignore:end */
  115. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  116. });
  117. it("calls error callback on backend error", function(done) { // jshint ignore:line
  118. $.mockjax({
  119. url: '/test-api/users/123/avatar/',
  120. status: 403,
  121. responseText: {
  122. detail: "You need to sign in to change avatar."
  123. }
  124. });
  125. /* jshint ignore:start */
  126. let showError = function(error) {
  127. assert.equal(error.detail, "You need to sign in to change avatar.",
  128. "callback was called with backend error message");
  129. done();
  130. };
  131. testUtils.render(
  132. <ChangeAvatarIndex user={misago.get('user')}
  133. options={apiResponse}
  134. showError={showError} />
  135. );
  136. /* jshint ignore:end */
  137. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  138. });
  139. it("changes avatar to generated one successfully", function(done) { // jshint ignore:line
  140. $.mockjax({
  141. url: '/test-api/users/123/avatar/',
  142. status: 200,
  143. responseText: {
  144. detail: "Generated avataru set!",
  145. avatar_hash: 'n33wh44sh',
  146. options: apiResponse
  147. }
  148. });
  149. snackbarStore.callback(function(message) {
  150. assert.deepEqual(message, {
  151. message: "Generated avataru set!",
  152. type: 'success'
  153. }, "valid message was shown");
  154. });
  155. /* jshint ignore:start */
  156. let onComplete = function(avatarHash, options) {
  157. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  158. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  159. done();
  160. };
  161. testUtils.render(
  162. <ChangeAvatarIndex user={misago.get('user')}
  163. options={apiResponse}
  164. onComplete={onComplete} />
  165. );
  166. /* jshint ignore:end */
  167. testUtils.simulateClick('#test-mount .btn-avatar-generate');
  168. });
  169. it("changes avatar to gravatar successfully", function(done) { // jshint ignore:line
  170. $.mockjax({
  171. url: '/test-api/users/123/avatar/',
  172. status: 200,
  173. responseText: {
  174. detail: "Gravatar avataru set!",
  175. avatar_hash: 'n33wh44sh',
  176. options: apiResponse
  177. }
  178. });
  179. snackbarStore.callback(function(message) {
  180. assert.deepEqual(message, {
  181. message: "Gravatar avataru set!",
  182. type: 'success'
  183. }, "valid message was shown");
  184. });
  185. /* jshint ignore:start */
  186. let onComplete = function(avatarHash, options) {
  187. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  188. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  189. done();
  190. };
  191. testUtils.render(
  192. <ChangeAvatarIndex user={misago.get('user')}
  193. options={apiResponse}
  194. onComplete={onComplete} />
  195. );
  196. /* jshint ignore:end */
  197. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  198. });
  199. });