subscription-menu.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import SubscriptionMenu from 'misago/components/threads-list/thread/subscription/options'; // jshint ignore:line
  4. import modal from 'misago/services/modal';
  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. const thread = {
  10. id: 123,
  11. api_url: '/test-api/threads/1321/',
  12. subscription: null
  13. };
  14. describe("Threads List Subscription Options", function() {
  15. beforeEach(function() {
  16. snackbarStore = testUtils.snackbarStoreMock();
  17. snackbar.init(snackbarStore);
  18. testUtils.initModal(modal);
  19. });
  20. afterEach(function() {
  21. testUtils.unmountComponents();
  22. testUtils.snackbarClear(snackbar);
  23. $.mockjax.clear();
  24. });
  25. it("handles backend error", function(done) {
  26. Store._store = {
  27. dispatch: function(action) {
  28. this.action = action;
  29. }
  30. };
  31. $.mockjax({
  32. url: thread.api_url,
  33. status: 500
  34. });
  35. /* jshint ignore:start */
  36. testUtils.render(<SubscriptionMenu thread={thread} />);
  37. /* jshint ignore:end */
  38. testUtils.simulateClick('#test-mount button:eq(2)');
  39. window.setTimeout(function() {
  40. assert.deepEqual(Store._store.action, {
  41. type: 'PATCH_THREAD',
  42. thread: {
  43. id: 123,
  44. api_url: '/test-api/threads/1321/',
  45. subscription: null
  46. },
  47. patch: {
  48. subscription: null
  49. },
  50. sorting: null
  51. }, "action was set on store");
  52. assert.deepEqual(snackbarStore.message, {
  53. message: 'Unknown error has occured.',
  54. type: 'error'
  55. });
  56. done();
  57. }, 300);
  58. });
  59. it("unsubscribes thread", function(done) {
  60. Store._store = {
  61. dispatch: function(action) {
  62. this.action = action;
  63. }
  64. };
  65. $.mockjax({
  66. url: thread.api_url,
  67. status: 200,
  68. responseText: {
  69. subscription: null
  70. }
  71. });
  72. /* jshint ignore:start */
  73. testUtils.render(<SubscriptionMenu thread={thread} />);
  74. /* jshint ignore:end */
  75. testUtils.simulateClick('#test-mount button:eq(0)');
  76. window.setTimeout(function() {
  77. assert.deepEqual(Store._store.action, {
  78. type: 'PATCH_THREAD',
  79. thread: {
  80. id: 123,
  81. api_url: '/test-api/threads/1321/',
  82. subscription: null
  83. },
  84. patch: {
  85. subscription: null
  86. },
  87. sorting: null
  88. }, "action was set on store");
  89. done();
  90. }, 300);
  91. });
  92. it("subscribes thread", function(done) {
  93. Store._store = {
  94. dispatch: function(action) {
  95. this.action = action;
  96. }
  97. };
  98. $.mockjax({
  99. url: thread.api_url,
  100. status: 200,
  101. responseText: {
  102. subscription: false
  103. }
  104. });
  105. /* jshint ignore:start */
  106. testUtils.render(<SubscriptionMenu thread={thread} />);
  107. /* jshint ignore:end */
  108. testUtils.simulateClick('#test-mount button:eq(1)');
  109. window.setTimeout(function() {
  110. assert.deepEqual(Store._store.action, {
  111. type: 'PATCH_THREAD',
  112. thread: {
  113. id: 123,
  114. api_url: '/test-api/threads/1321/',
  115. subscription: null
  116. },
  117. patch: {
  118. subscription: false
  119. },
  120. sorting: null
  121. }, "action was set on store");
  122. done();
  123. }, 300);
  124. });
  125. it("subscribes thread with email", function(done) {
  126. Store._store = {
  127. dispatch: function(action) {
  128. this.action = action;
  129. }
  130. };
  131. $.mockjax({
  132. url: thread.api_url,
  133. status: 200,
  134. responseText: {
  135. subscription: true
  136. }
  137. });
  138. /* jshint ignore:start */
  139. testUtils.render(<SubscriptionMenu thread={thread} />);
  140. /* jshint ignore:end */
  141. testUtils.simulateClick('#test-mount button:eq(2)');
  142. window.setTimeout(function() {
  143. assert.deepEqual(Store._store.action, {
  144. type: 'PATCH_THREAD',
  145. thread: {
  146. id: 123,
  147. api_url: '/test-api/threads/1321/',
  148. subscription: null
  149. },
  150. patch: {
  151. subscription: true
  152. },
  153. sorting: null
  154. }, "action was set on store");
  155. done();
  156. }, 300);
  157. });
  158. });