change-username.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import ChangeUsername from 'misago/components/profile/moderation/change-username'; // jshint ignore:line
  4. import misago from 'misago/index';
  5. import reducer from 'misago/reducers/profile';
  6. import snackbar from 'misago/services/snackbar';
  7. import store from 'misago/services/store';
  8. import * as testUtils from 'misago/utils/test-utils';
  9. let snackbarStore = null;
  10. let profileMock = {
  11. id: 242,
  12. avatar_hash: 'original_hash',
  13. is_followed: false,
  14. followers: 0,
  15. api_url: {
  16. moderate_username: '/test-api/users/123/moderate-username/'
  17. }
  18. };
  19. describe("User Profile Moderation Change Username", function() {
  20. beforeEach(function() {
  21. misago._context = {
  22. SETTINGS: {
  23. username_length_min: 3,
  24. username_length_max: 8
  25. }
  26. };
  27. snackbarStore = testUtils.snackbarStoreMock();
  28. snackbar.init(snackbarStore);
  29. store.constructor();
  30. store.addReducer('profile', reducer, profileMock);
  31. store.addReducer('auth', function(state, action) {
  32. if (action || true) {
  33. return testUtils.mockUser();
  34. }
  35. }, {});
  36. store.addReducer('tick', function(state, action) {
  37. if (action || true) {
  38. return {'tick': 123};
  39. }
  40. }, {});
  41. store.addReducer('username-history', function(state, action) {
  42. if (action) {
  43. return action;
  44. } else {
  45. return {};
  46. }
  47. }, {});
  48. store.init();
  49. });
  50. afterEach(function() {
  51. testUtils.unmountComponents();
  52. testUtils.snackbarClear(snackbar);
  53. $.mockjax.clear();
  54. });
  55. it("renders", function(done) {
  56. $.mockjax({
  57. url: profileMock.api_url.moderate_username,
  58. status: 200,
  59. responseText: {
  60. detail: 'ok'
  61. }
  62. });
  63. /* jshint ignore:start */
  64. testUtils.render(<ChangeUsername profile={profileMock} />);
  65. /* jshint ignore:end */
  66. testUtils.onElement('#test-mount form', function(element) {
  67. assert.ok(element.length, "component loads");
  68. done();
  69. });
  70. });
  71. it("handles backend error", function(done) {
  72. $.mockjax({
  73. url: profileMock.api_url.moderate_username,
  74. status: 500
  75. });
  76. /* jshint ignore:start */
  77. testUtils.render(<ChangeUsername profile={profileMock} />);
  78. /* jshint ignore:end */
  79. testUtils.onElement('#test-mount .modal-message', function(element) {
  80. assert.equal(element.find('p.lead').text(), "Unknown error has occured.",
  81. "error message renders");
  82. done();
  83. });
  84. });
  85. it("handles load rejection", function(done) {
  86. $.mockjax({
  87. url: profileMock.api_url.moderate_username,
  88. status: 403,
  89. responseText: {
  90. detail: "You can't mod username!"
  91. }
  92. });
  93. /* jshint ignore:start */
  94. testUtils.render(<ChangeUsername profile={profileMock} />);
  95. /* jshint ignore:end */
  96. testUtils.onElement('#test-mount .modal-message', function(element) {
  97. assert.equal(element.find('p.lead').text(), "You can't mod username!",
  98. "error message renders");
  99. done();
  100. });
  101. });
  102. it("handles empty submission", function(done) {
  103. $.mockjax({
  104. url: profileMock.api_url.moderate_username,
  105. type: 'GET',
  106. status: 200,
  107. responseText: {
  108. detail: 'ok'
  109. }
  110. });
  111. /* jshint ignore:start */
  112. testUtils.render(<ChangeUsername profile={profileMock} />);
  113. /* jshint ignore:end */
  114. snackbarStore.callback(function(message) {
  115. assert.equal(message.message, "This field is required.",
  116. "Rejection message is shown in snackbar.");
  117. done();
  118. });
  119. testUtils.onElement('#test-mount form', function() {
  120. testUtils.simulateSubmit('#test-mount form');
  121. });
  122. });
  123. it("handles invalid submission", function(done) {
  124. $.mockjax({
  125. url: profileMock.api_url.moderate_username,
  126. type: 'GET',
  127. status: 200,
  128. responseText: {
  129. detail: 'ok'
  130. }
  131. });
  132. /* jshint ignore:start */
  133. testUtils.render(<ChangeUsername profile={profileMock} />);
  134. /* jshint ignore:end */
  135. snackbarStore.callback(function(message) {
  136. assert.equal(message.message,
  137. "Username can only contain latin alphabet letters and digits.",
  138. "Rejection message is shown in snackbar.");
  139. done();
  140. });
  141. testUtils.onElement('#test-mount form', function() {
  142. testUtils.simulateChange('#id_username', '###');
  143. testUtils.simulateSubmit('#test-mount form');
  144. });
  145. });
  146. it("handles failed submission", function(done) {
  147. $.mockjax({
  148. url: profileMock.api_url.moderate_username,
  149. type: 'GET',
  150. status: 200,
  151. responseText: {
  152. detail: 'ok'
  153. }
  154. });
  155. $.mockjax({
  156. url: profileMock.api_url.moderate_username,
  157. type: 'POST',
  158. status: 400,
  159. responseText: {
  160. detail: "Can't do it now!"
  161. }
  162. });
  163. /* jshint ignore:start */
  164. testUtils.render(<ChangeUsername profile={profileMock} />);
  165. /* jshint ignore:end */
  166. snackbarStore.callback(function(message) {
  167. assert.equal(message.message, "Can't do it now!",
  168. "Rejection message is shown in snackbar.");
  169. done();
  170. });
  171. testUtils.onElement('#test-mount form', function() {
  172. testUtils.simulateChange('#id_username', 'NewName');
  173. testUtils.simulateSubmit('#test-mount form');
  174. });
  175. });
  176. it("handles submission", function(done) {
  177. $.mockjax({
  178. url: profileMock.api_url.moderate_username,
  179. type: 'GET',
  180. status: 200,
  181. responseText: {
  182. detail: 'ok'
  183. }
  184. });
  185. $.mockjax({
  186. url: profileMock.api_url.moderate_username,
  187. type: 'POST',
  188. status: 200,
  189. responseText: {
  190. username: 'NewName',
  191. slug: 'newname'
  192. }
  193. });
  194. /* jshint ignore:start */
  195. testUtils.render(<ChangeUsername profile={profileMock} />);
  196. /* jshint ignore:end */
  197. testUtils.onElement('#test-mount form', function() {
  198. testUtils.simulateChange('#id_username', 'NewName');
  199. testUtils.simulateSubmit('#test-mount form');
  200. window.setTimeout(function() {
  201. assert.equal(store.getState().profile.username, 'NewName',
  202. "profile username was updated");
  203. assert.equal(store.getState().profile.username, 'NewName',
  204. "profile slug was updated");
  205. assert.deepEqual(store.getState()['username-history'], {
  206. type: 'UPDATE_USERNAME',
  207. userId: 242,
  208. username: 'NewName',
  209. slug: 'newname'
  210. }, "name change was dispatched");
  211. done();
  212. }, 200);
  213. });
  214. });
  215. });