avatar-controls.js 4.5 KB

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