avatar-controls.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import AvatarControls from 'misago/components/profile/moderation/avatar-controls'; // jshint ignore:line
  4. import reducer from 'misago/reducers/profile';
  5. import snackbar from 'misago/services/snackbar';
  6. import store from 'misago/services/store';
  7. import * as testUtils from 'misago/utils/test-utils';
  8. let snackbarStore = null;
  9. let profileMock = {
  10. avatar_hash: 'original_hash',
  11. is_followed: false,
  12. followers: 0,
  13. api_url: {
  14. moderate_avatar: '/test-api/users/123/moderate_avatar/'
  15. }
  16. };
  17. describe("User Profile Moderation Avatar Controls", function() {
  18. beforeEach(function() {
  19. snackbarStore = testUtils.snackbarStoreMock();
  20. snackbar.init(snackbarStore);
  21. store.constructor();
  22. store.addReducer('profile', reducer, profileMock);
  23. store.addReducer('auth', function(state, action) {
  24. if (action || true) {
  25. return testUtils.mockUser();
  26. }
  27. }, {});
  28. store.addReducer('tick', function(state, action) {
  29. if (action || true) {
  30. return {'tick': 123};
  31. }
  32. }, {});
  33. store.init();
  34. });
  35. afterEach(function() {
  36. testUtils.unmountComponents();
  37. testUtils.snackbarClear(snackbar);
  38. $.mockjax.clear();
  39. });
  40. it("renders", function(done) {
  41. $.mockjax({
  42. url: profileMock.api_url.moderate_avatar,
  43. status: 200,
  44. responseText: {
  45. is_avatar_locked: false,
  46. avatar_lock_user_message: null,
  47. avatar_lock_staff_message: null
  48. }
  49. });
  50. /* jshint ignore:start */
  51. testUtils.render(<AvatarControls profile={profileMock} />);
  52. /* jshint ignore:end */
  53. testUtils.onElement('#test-mount form', function(element) {
  54. assert.ok(element.length, "component loads");
  55. done();
  56. });
  57. });
  58. it("handles backend error", function(done) {
  59. $.mockjax({
  60. url: profileMock.api_url.moderate_avatar,
  61. status: 500
  62. });
  63. /* jshint ignore:start */
  64. testUtils.render(<AvatarControls profile={profileMock} />);
  65. /* jshint ignore:end */
  66. testUtils.onElement('#test-mount .modal-message', function(element) {
  67. assert.equal(element.find('p.lead').text(), "Unknown error has occured.",
  68. "error message renders");
  69. done();
  70. });
  71. });
  72. it("handles load rejection", function(done) {
  73. $.mockjax({
  74. url: profileMock.api_url.moderate_avatar,
  75. status: 403,
  76. responseText: {
  77. detail: "You can't mod user avatar!"
  78. }
  79. });
  80. /* jshint ignore:start */
  81. testUtils.render(<AvatarControls profile={profileMock} />);
  82. /* jshint ignore:end */
  83. testUtils.onElement('#test-mount .modal-message', function(element) {
  84. assert.equal(element.find('p.lead').text(), "You can't mod user avatar!",
  85. "error message renders");
  86. done();
  87. });
  88. });
  89. it("handles failed submission", function(done) {
  90. $.mockjax({
  91. url: profileMock.api_url.moderate_avatar,
  92. type: 'GET',
  93. status: 200,
  94. responseText: {
  95. is_avatar_locked: false,
  96. avatar_lock_user_message: null,
  97. avatar_lock_staff_message: null
  98. }
  99. });
  100. $.mockjax({
  101. url: profileMock.api_url.moderate_avatar,
  102. type: 'POST',
  103. status: 400,
  104. responseText: {
  105. detail: "Can't do it now!"
  106. }
  107. });
  108. /* jshint ignore:start */
  109. testUtils.render(<AvatarControls profile={profileMock} />);
  110. /* jshint ignore:end */
  111. snackbarStore.callback(function(message) {
  112. assert.equal(message.message, "Can't do it now!",
  113. "Rejection message is shown in snackbar.");
  114. done();
  115. });
  116. testUtils.onElement('#test-mount form', function() {
  117. testUtils.simulateSubmit('#test-mount form');
  118. });
  119. });
  120. it("handles submission", function(done) {
  121. $.mockjax({
  122. url: profileMock.api_url.moderate_avatar,
  123. type: 'GET',
  124. status: 200,
  125. responseText: {
  126. is_avatar_locked: false,
  127. avatar_lock_user_message: null,
  128. avatar_lock_staff_message: null
  129. }
  130. });
  131. $.mockjax({
  132. url: profileMock.api_url.moderate_avatar,
  133. type: 'POST',
  134. status: 200,
  135. responseText: {
  136. avatar_hash: 'yolo'
  137. }
  138. });
  139. /* jshint ignore:start */
  140. testUtils.render(<AvatarControls profile={profileMock} />);
  141. /* jshint ignore:end */
  142. testUtils.onElement('#test-mount form', function() {
  143. testUtils.simulateSubmit('#test-mount form');
  144. window.setTimeout(function() {
  145. assert.equal(store.getState().profile.avatar_hash, 'yolo',
  146. "profile's avatar hash was updated");
  147. done();
  148. }, 200);
  149. });
  150. });
  151. });