root.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import assert from 'assert';
  2. import React from 'react'; // jshint ignore:line
  3. import CategoriesList from 'misago/components/categories/root'; // jshint ignore:line
  4. import misago from 'misago/index';
  5. import ajax from 'misago/services/ajax';
  6. import polls from 'misago/services/polls';
  7. import snackbar from 'misago/services/snackbar';
  8. import * as testUtils from 'misago/utils/test-utils';
  9. let snackbarStore = null;
  10. let categories = [
  11. {
  12. "id": 3,
  13. "name": "Games",
  14. "description": null,
  15. "is_closed": false,
  16. "threads": 82,
  17. "posts": 1944,
  18. "last_post_on": "2016-02-25T21:15:53.231778Z",
  19. "last_thread_title": "Nemo quibusdam sunt ab odit omnis totam.",
  20. "last_poster_name": "Raelyn",
  21. "css_class": "accent",
  22. "is_read": false,
  23. "subcategories": [],
  24. "absolute_url": "/category/games-3/",
  25. "last_thread_url": "/thread/test-thread-132/",
  26. "last_post_url": "/thread/test-thread-132/last/",
  27. "last_poster_url": "/user/raelyn-74/",
  28. "acl": {
  29. "can_browse": true,
  30. "can_see_all_threads": 1
  31. }
  32. },
  33. {
  34. "id": 4,
  35. "name": "Second category",
  36. "description": {
  37. "plain": "Lorem ipsum dolor met sit amet elit.",
  38. "html": "<p>Lorem ipsum dolor met sit amet elit.</p>"
  39. },
  40. "is_closed": false,
  41. "threads": 418,
  42. "posts": 7741,
  43. "last_post_on": "2016-02-25T21:15:54.483911Z",
  44. "last_thread_title": "Iste officiis debitis velit non magnam aut a.",
  45. "last_poster_name": "Myrna",
  46. "css_class": "",
  47. "is_read": false,
  48. "subcategories": [
  49. {
  50. "id": 5,
  51. "name": "Action",
  52. "description": {
  53. "plain": "Lorem ipsum dolor met sit amet elit pacem bellum sequor.",
  54. "html": "<p>Lorem ipsum dolor met sit amet elit pacem bellum sequor.</p>"
  55. },
  56. "is_closed": true,
  57. "threads": 263,
  58. "posts": 5386,
  59. "last_post_on": "2016-02-25T21:15:54.483911Z",
  60. "last_thread_title": "Iste officiis debitis velit non magnam aut a.",
  61. "last_poster_name": "Myrna",
  62. "css_class": "",
  63. "is_read": false,
  64. "subcategories": [
  65. {
  66. "id": 7,
  67. "name": "Multiplayer",
  68. "description": null,
  69. "is_closed": false,
  70. "threads": 95,
  71. "posts": 1567,
  72. "last_post_on": "2016-02-25T21:15:53.697017Z",
  73. "last_thread_title": "Et debitis unde in eius.",
  74. "last_poster_name": "Morton",
  75. "css_class": "",
  76. "is_read": false,
  77. "subcategories": [],
  78. "absolute_url": "/category/multiplayer-7/",
  79. "last_thread_url": "/thread/test-thread-134/",
  80. "last_post_url": "/thread/test-thread-134/last/",
  81. "last_poster_url": "/user/morton-30/",
  82. "acl": {
  83. "can_browse": true,
  84. "can_see_all_threads": 1
  85. }
  86. },
  87. {
  88. "id": 8,
  89. "name": "Single player",
  90. "description": null,
  91. "is_closed": false,
  92. "threads": 93,
  93. "posts": 2422,
  94. "last_post_on": "2016-02-25T21:15:54.483911Z",
  95. "last_thread_title": "Iste officiis debitis velit non magnam aut a.",
  96. "last_poster_name": "Myrna",
  97. "css_class": "",
  98. "is_read": false,
  99. "subcategories": [],
  100. "absolute_url": "/category/single-player-8/",
  101. "last_thread_url": "/thread/test-thread-135/",
  102. "last_post_url": "/thread/test-thread-135/last/",
  103. "last_poster_url": "/user/myrna-18/",
  104. "acl": {
  105. "can_browse": true,
  106. "can_see_all_threads": 1
  107. }
  108. }
  109. ],
  110. "absolute_url": "/category/action-5/",
  111. "last_thread_url": "/thread/test-thread-136/",
  112. "last_post_url": "/thread/test-thread-136/last/",
  113. "last_poster_url": "/user/myrna-52/",
  114. "acl": {
  115. "can_browse": true,
  116. "can_see_all_threads": 1
  117. }
  118. },
  119. {
  120. "id": 6,
  121. "name": "Sandbox",
  122. "description": null,
  123. "is_closed": false,
  124. "threads": 73,
  125. "posts": 979,
  126. "last_post_on": "2016-02-25T21:15:54.240616Z",
  127. "last_thread_title": "Totam hic excepturi nulla asperiores illum.",
  128. "last_poster_name": "Camille",
  129. "css_class": "",
  130. "is_read": false,
  131. "subcategories": [],
  132. "absolute_url": "/category/sandbox-6/",
  133. "last_thread_url": "/thread/test-thread-137/",
  134. "last_post_url": "/thread/test-thread-137/last/",
  135. "last_poster_url": "/user/camille-29/",
  136. "acl": {
  137. "can_browse": true,
  138. "can_see_all_threads": 1
  139. }
  140. }
  141. ],
  142. "absolute_url": "/category/second-category-4/",
  143. "last_thread_url": "/thread/test-thread-133/",
  144. "last_post_url": "/thread/test-thread-133/last/",
  145. "last_poster_url": "/user/myrna-88/",
  146. "acl": {
  147. "can_browse": true,
  148. "can_see_all_threads": 1
  149. }
  150. }
  151. ];
  152. describe("Categories List", function() {
  153. beforeEach(function() {
  154. misago._context = {
  155. CATEGORIES: categories,
  156. CATEGORIES_API: '/test-api/categories/',
  157. CATEGORIES_ON_INDEX: false
  158. };
  159. snackbarStore = testUtils.snackbarStoreMock();
  160. snackbar.init(snackbarStore);
  161. polls.init(ajax, snackbar);
  162. });
  163. afterEach(function() {
  164. polls.stop('categories');
  165. testUtils.unmountComponents();
  166. $.mockjax.clear();
  167. });
  168. it("renders and loads", function(done) {
  169. $.mockjax({
  170. url: '/test-api/categories/',
  171. status: 200,
  172. responseText: categories
  173. });
  174. /* jshint ignore:start */
  175. testUtils.render(<CategoriesList />);
  176. /* jshint ignore:end */
  177. assert.equal($('#test-mount .panel-category').length, 2,
  178. "two categories rendered initially");
  179. window.setTimeout(function() {
  180. let element = $('#test-mount .categories-list');
  181. assert.ok(element.length, "component renders");
  182. assert.equal(element.find('.panel-category').length, 2,
  183. "two categories rendered");
  184. done();
  185. }, 200);
  186. });
  187. it("handles backend error", function(done) {
  188. $.mockjax({
  189. url: '/test-api/categories/',
  190. status: 500
  191. });
  192. snackbarStore.callback(function(message) {
  193. assert.deepEqual(message, {
  194. message: "Unknown error has occured.",
  195. type: 'error'
  196. }, "error message was shown");
  197. done();
  198. });
  199. /* jshint ignore:start */
  200. testUtils.render(<CategoriesList />);
  201. /* jshint ignore:end */
  202. });
  203. it("handles backend rejection", function(done) {
  204. $.mockjax({
  205. url: '/test-api/categories/',
  206. status: 403,
  207. responseText: {
  208. detail: "You can't see it yo!"
  209. }
  210. });
  211. snackbarStore.callback(function(message) {
  212. assert.deepEqual(message, {
  213. message: "You can't see it yo!",
  214. type: 'error'
  215. }, "backend returned error message was shown");
  216. done();
  217. });
  218. /* jshint ignore:start */
  219. testUtils.render(<CategoriesList />);
  220. /* jshint ignore:end */
  221. });
  222. });