root.js 6.6 KB

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