index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. 'api_url': {
  60. 'avatar': '/test-api/users/123/avatar/'
  61. }
  62. }
  63. };
  64. });
  65. afterEach(function() {
  66. testUtils.unmountComponents();
  67. testUtils.snackbarClear(snackbar);
  68. $.mockjax.clear();
  69. });
  70. it("renders", function(done) {
  71. /* jshint ignore:start */
  72. testUtils.render(
  73. <ChangeAvatarIndex user={misago.get('user')}
  74. options={apiResponse} />
  75. );
  76. /* jshint ignore:end */
  77. testUtils.onElement('#test-mount .modal-avatar-index', function() {
  78. assert.ok(true, "component renders");
  79. done();
  80. });
  81. });
  82. it("renders without gravatar button", function(done) {
  83. /* jshint ignore:start */
  84. let amendedOptions = Object.assign({}, apiResponse, {gravatar: false});
  85. testUtils.render(
  86. <ChangeAvatarIndex user={misago.get('user')}
  87. options={amendedOptions} />
  88. );
  89. /* jshint ignore:end */
  90. testUtils.onElement('#test-mount .modal-avatar-index', function() {
  91. assert.ok(!$('#test-mount .btn-avatar-gravatar').length,
  92. "gravatar option is hidden");
  93. done();
  94. });
  95. });
  96. it("renders without gallery button", function(done) {
  97. /* jshint ignore:start */
  98. let amendedOptions = Object.assign({}, apiResponse, {galleries: false});
  99. testUtils.render(
  100. <ChangeAvatarIndex user={misago.get('user')}
  101. options={amendedOptions} />
  102. );
  103. /* jshint ignore:end */
  104. testUtils.onElement('#test-mount .modal-avatar-index', function() {
  105. assert.ok(!$('#test-mount .btn-avatar-gallery').length,
  106. "gallery option is hidden");
  107. done();
  108. });
  109. });
  110. it("shows alert with error on rejection", function(done) {
  111. $.mockjax({
  112. url: '/test-api/users/123/avatar/',
  113. status: 400,
  114. responseText: {
  115. detail: "You can't change avatar at the moment!"
  116. }
  117. });
  118. snackbarStore.callback(function(message) {
  119. assert.deepEqual(message, {
  120. message: "You can't change avatar at the moment!",
  121. type: 'error'
  122. }, "valid message was shown");
  123. done();
  124. });
  125. /* jshint ignore:start */
  126. testUtils.render(
  127. <ChangeAvatarIndex user={misago.get('user')}
  128. options={apiResponse} />
  129. );
  130. /* jshint ignore:end */
  131. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  132. });
  133. it("calls error callback on backend error", function(done) { // jshint ignore:line
  134. $.mockjax({
  135. url: '/test-api/users/123/avatar/',
  136. status: 403,
  137. responseText: {
  138. detail: "You need to sign in to change avatar."
  139. }
  140. });
  141. /* jshint ignore:start */
  142. let showError = function(error) {
  143. assert.equal(error.detail, "You need to sign in to change avatar.",
  144. "callback was called with backend error message");
  145. done();
  146. };
  147. testUtils.render(
  148. <ChangeAvatarIndex user={misago.get('user')}
  149. options={apiResponse}
  150. showError={showError} />
  151. );
  152. /* jshint ignore:end */
  153. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  154. });
  155. it("changes avatar to generated one successfully", function(done) { // jshint ignore:line
  156. $.mockjax({
  157. url: '/test-api/users/123/avatar/',
  158. status: 200,
  159. responseText: {
  160. detail: "Generated avataru set!",
  161. avatar_hash: 'n33wh44sh',
  162. options: apiResponse
  163. }
  164. });
  165. snackbarStore.callback(function(message) {
  166. assert.deepEqual(message, {
  167. message: "Generated avataru set!",
  168. type: 'success'
  169. }, "valid message was shown");
  170. });
  171. /* jshint ignore:start */
  172. let onComplete = function(avatarHash, options) {
  173. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  174. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  175. done();
  176. };
  177. testUtils.render(
  178. <ChangeAvatarIndex user={misago.get('user')}
  179. options={apiResponse}
  180. onComplete={onComplete} />
  181. );
  182. /* jshint ignore:end */
  183. testUtils.simulateClick('#test-mount .btn-avatar-generate');
  184. });
  185. it("changes avatar to gravatar successfully", function(done) { // jshint ignore:line
  186. $.mockjax({
  187. url: '/test-api/users/123/avatar/',
  188. status: 200,
  189. responseText: {
  190. detail: "Gravatar avataru set!",
  191. avatar_hash: 'n33wh44sh',
  192. options: apiResponse
  193. }
  194. });
  195. snackbarStore.callback(function(message) {
  196. assert.deepEqual(message, {
  197. message: "Gravatar avataru set!",
  198. type: 'success'
  199. }, "valid message was shown");
  200. });
  201. /* jshint ignore:start */
  202. let onComplete = function(avatarHash, options) {
  203. assert.equal(avatarHash, 'n33wh44sh', "new hash was passed to callback");
  204. assert.deepEqual(options, apiResponse, "new ops ware passed to callback");
  205. done();
  206. };
  207. testUtils.render(
  208. <ChangeAvatarIndex user={misago.get('user')}
  209. options={apiResponse}
  210. onComplete={onComplete} />
  211. );
  212. /* jshint ignore:end */
  213. testUtils.simulateClick('#test-mount .btn-avatar-gravatar');
  214. });
  215. });