change-username.js 6.3 KB

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