root.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 .page-categories');
  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('renders and loads empty list', function(done) {
  182. misago._context.CATEGORIES = [];
  183. $.mockjax({
  184. url: '/test-api/categories/',
  185. status: 200,
  186. responseText: []
  187. });
  188. /* jshint ignore:start */
  189. testUtils.render(<CategoriesList />);
  190. /* jshint ignore:end */
  191. assert.equal($('#test-mount .panel-category').length, 0,
  192. "no categories rendered initially");
  193. window.setTimeout(function() {
  194. let element = $('#test-mount .page-message .message-body');
  195. assert.ok(element.length, "component renders message about no data");
  196. assert.equal(element.find('.panel-category').length, 0,
  197. "no categories rendered");
  198. done();
  199. }, 200);
  200. });
  201. it('renders and empties list', function(done) {
  202. $.mockjax({
  203. url: '/test-api/categories/',
  204. status: 200,
  205. responseText: []
  206. });
  207. /* jshint ignore:start */
  208. testUtils.render(<CategoriesList />);
  209. /* jshint ignore:end */
  210. assert.equal($('#test-mount .panel-category').length, 2,
  211. "two categories rendered initially");
  212. testUtils.onElement('#test-mount .page-message .message-body', function() {
  213. let element = $('#test-mount .page-message .message-body');
  214. assert.ok(element.length, "component renders message about no data");
  215. assert.equal(element.find('.panel-category').length, 0,
  216. "no categories rendered");
  217. done();
  218. });
  219. });
  220. it('renders empty and populates list', function(done) {
  221. misago._context.CATEGORIES = [];
  222. $.mockjax({
  223. url: '/test-api/categories/',
  224. status: 200,
  225. responseText: categories
  226. });
  227. /* jshint ignore:start */
  228. testUtils.render(<CategoriesList />);
  229. /* jshint ignore:end */
  230. assert.equal($('#test-mount .panel-category').length, 0,
  231. "no categories rendered initially");
  232. testUtils.onElement('#test-mount .categories-list', function(element) {
  233. assert.equal(element.find('.panel-category').length, 2,
  234. "two categories rendered");
  235. done();
  236. });
  237. });
  238. it('has valid header if not forum index', function() {
  239. misago._context.CATEGORIES = [];
  240. misago._context.CATEGORIES_ON_INDEX = false;
  241. $.mockjax({
  242. url: '/test-api/categories/',
  243. status: 200,
  244. responseText: []
  245. });
  246. /* jshint ignore:start */
  247. testUtils.render(<CategoriesList />);
  248. /* jshint ignore:end */
  249. assert.equal($('#test-mount h1').text(), "Categories",
  250. "renders with non-home header");
  251. });
  252. it('has valid header if forum index', function() {
  253. misago._context.CATEGORIES = [];
  254. misago._context.CATEGORIES_ON_INDEX = true;
  255. misago._context.SETTINGS = {
  256. forum_name: "Test Misago Forum"
  257. };
  258. $.mockjax({
  259. url: '/test-api/categories/',
  260. status: 200,
  261. responseText: []
  262. });
  263. /* jshint ignore:start */
  264. testUtils.render(<CategoriesList />);
  265. /* jshint ignore:end */
  266. assert.equal($('#test-mount h1').text(), "Test Misago Forum",
  267. "renders with forum name in header");
  268. });
  269. });