delete-account.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import DeleteAccount from 'misago/components/profile/moderation/delete-account'; // jshint ignore:line
  4. import polls from 'misago/services/polls';
  5. import snackbar from 'misago/services/snackbar';
  6. import * as testUtils from 'misago/utils/test-utils';
  7. let component = null;
  8. let snackbarStore = null;
  9. let profileMock = {
  10. id: 242,
  11. avatar_hash: 'original_hash',
  12. username: "BobBoberson",
  13. is_followed: false,
  14. followers: 0,
  15. api_url: {
  16. delete: '/test-api/users/123/delete/'
  17. }
  18. };
  19. describe("User Profile Deletion", function() {
  20. beforeEach(function() {
  21. component = null;
  22. snackbarStore = testUtils.snackbarStoreMock();
  23. snackbar.init(snackbarStore);
  24. polls.init();
  25. });
  26. afterEach(function() {
  27. testUtils.unmountComponents();
  28. testUtils.snackbarClear(snackbar);
  29. $.mockjax.clear();
  30. });
  31. it("renders", function(done) {
  32. $.mockjax({
  33. url: profileMock.api_url.delete,
  34. status: 200,
  35. responseText: {
  36. detail: 'ok'
  37. }
  38. });
  39. /* jshint ignore:start */
  40. component = testUtils.render(<DeleteAccount profile={profileMock} />)
  41. /* jshint ignore:end */
  42. testUtils.onElement('#test-mount form', function(element) {
  43. assert.ok(element.length, "component loads");
  44. let btn = element.find('.btn-danger');
  45. assert.equal(btn.text().indexOf("Please wait..."), 0,
  46. "countdown is displayed in button");
  47. component.setState({
  48. countdown: 0,
  49. confirm: true
  50. });
  51. component.forceUpdate(function() {
  52. assert.equal(btn.text(), "Delete BobBoberson", "countdown ends");
  53. done();
  54. });
  55. });
  56. });
  57. it("handles backend error", function(done) {
  58. $.mockjax({
  59. url: profileMock.api_url.delete,
  60. status: 500
  61. });
  62. /* jshint ignore:start */
  63. testUtils.render(<DeleteAccount profile={profileMock} />);
  64. /* jshint ignore:end */
  65. testUtils.onElement('#test-mount .modal-message', function(element) {
  66. assert.equal(element.find('p.lead').text(), "Unknown error has occured.",
  67. "error message renders");
  68. done();
  69. });
  70. });
  71. it("handles load rejection", function(done) {
  72. $.mockjax({
  73. url: profileMock.api_url.delete,
  74. status: 403,
  75. responseText: {
  76. detail: "You can't nuke user!"
  77. }
  78. });
  79. /* jshint ignore:start */
  80. testUtils.render(<DeleteAccount profile={profileMock} />);
  81. /* jshint ignore:end */
  82. testUtils.onElement('#test-mount .modal-message', function(element) {
  83. assert.equal(element.find('p.lead').text(), "You can't nuke user!",
  84. "error message renders");
  85. done();
  86. });
  87. });
  88. it("handles failed submission", function(done) {
  89. $.mockjax({
  90. url: profileMock.api_url.delete,
  91. type: 'GET',
  92. status: 200,
  93. responseText: {
  94. detail: 'ok'
  95. }
  96. });
  97. $.mockjax({
  98. url: profileMock.api_url.delete,
  99. type: 'POST',
  100. status: 400,
  101. responseText: {
  102. detail: "Can't do it now!"
  103. }
  104. });
  105. /* jshint ignore:start */
  106. component = testUtils.render(<DeleteAccount profile={profileMock} />)
  107. /* jshint ignore:end */
  108. snackbarStore.callback(function(message) {
  109. assert.equal(message.message, "Can't do it now!",
  110. "Rejection message is shown in snackbar.");
  111. done();
  112. });
  113. testUtils.onElement('#test-mount form', function() {
  114. component.setState({
  115. countdown: 0,
  116. confirm: true
  117. });
  118. component.forceUpdate(function() {
  119. testUtils.simulateSubmit('#test-mount form');
  120. });
  121. });
  122. });
  123. it("delets account without content", function(done) {
  124. $.mockjax({
  125. url: profileMock.api_url.delete,
  126. type: 'GET',
  127. status: 200,
  128. responseText: {
  129. detail: 'ok'
  130. }
  131. });
  132. $.mockjax({
  133. url: profileMock.api_url.delete,
  134. type: 'POST',
  135. status: 200,
  136. responseText: {
  137. detail: 'ok'
  138. }
  139. });
  140. /* jshint ignore:start */
  141. component = testUtils.render(<DeleteAccount profile={profileMock} />);
  142. /* jshint ignore:end */
  143. testUtils.onElement('#test-mount form', function() {
  144. component.setState({
  145. countdown: 0,
  146. confirm: true
  147. });
  148. component.forceUpdate(function() {
  149. testUtils.simulateSubmit('#test-mount form');
  150. });
  151. testUtils.onElement('#test-mount .modal-message', function(element) {
  152. assert.equal(element.find('p.lead').text(),
  153. "BobBoberson's account has been deleted and other content has been hidden.",
  154. "valid account deletion message was displayed");
  155. done();
  156. });
  157. });
  158. });
  159. it("delets account with content", function(done) {
  160. $.mockjax({
  161. url: profileMock.api_url.delete,
  162. type: 'GET',
  163. status: 200,
  164. responseText: {
  165. detail: 'ok'
  166. }
  167. });
  168. $.mockjax({
  169. url: profileMock.api_url.delete,
  170. type: 'POST',
  171. status: 200,
  172. responseText: {
  173. detail: 'ok'
  174. }
  175. });
  176. /* jshint ignore:start */
  177. component = testUtils.render(<DeleteAccount profile={profileMock} />);
  178. /* jshint ignore:end */
  179. testUtils.onElement('#test-mount form', function() {
  180. component.setState({
  181. countdown: 0,
  182. confirm: true,
  183. with_content: true
  184. });
  185. component.forceUpdate(function() {
  186. testUtils.simulateSubmit('#test-mount form');
  187. });
  188. testUtils.onElement('#test-mount .modal-message', function(element) {
  189. assert.equal(element.find('p.lead').text(),
  190. "BobBoberson's account, threads, posts and other content has been deleted.",
  191. "valid account deletion message was displayed");
  192. done();
  193. });
  194. });
  195. });
  196. });