follows.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import assert from 'assert';
  2. import moment from 'moment';
  3. import React from 'react'; // jshint ignore:line
  4. import Follows from 'misago/components/profile/follows'; // jshint ignore:line
  5. import misago from 'misago/index';
  6. import reducer from 'misago/reducers/users';
  7. import snackbar from 'misago/services/snackbar';
  8. import store from 'misago/services/store';
  9. import * as testUtils from 'misago/utils/test-utils';
  10. let snackbarStore = null;
  11. /* jshint ignore:start */
  12. let userMock = {
  13. id: 42,
  14. username: 'TestUser',
  15. avatar_hash: 'abcdfefa'
  16. };
  17. let profileMock = {
  18. id: 123,
  19. username: 'BobBoberson',
  20. avatar_hash: 'abcdfefa'
  21. };
  22. /* jshint ignore:end */
  23. describe("User Profile Follows List", function() {
  24. beforeEach(function() {
  25. misago._context = {
  26. USERS_API: '/test-api/users/'
  27. };
  28. snackbarStore = testUtils.snackbarStoreMock();
  29. snackbar.init(snackbarStore);
  30. store.constructor();
  31. store.addReducer('users', reducer, []);
  32. store.init();
  33. });
  34. afterEach(function() {
  35. testUtils.unmountComponents();
  36. $.mockjax.clear();
  37. });
  38. it("preloads empty", function(done) {
  39. misago._context.PROFILE_FOLLOWS = {
  40. count: 0,
  41. more: 0,
  42. page: 1,
  43. pages: 1,
  44. results: []
  45. };
  46. /* jshint ignore:start */
  47. testUtils.render(
  48. <Follows user={userMock}
  49. profile={profileMock}
  50. users={[]} />
  51. );
  52. /* jshint ignore:end */
  53. testUtils.onElement('#test-mount p.lead', function(element) {
  54. assert.ok(element.length, "element renders");
  55. assert.equal($('#test-mount h3').text(),
  56. "BobBoberson is following 0 users.",
  57. "component has valid header");
  58. assert.equal(element.text(), "BobBoberson is not following any users.",
  59. "empty message was displayed");
  60. done();
  61. });
  62. });
  63. it("preloads empty (owned)", function(done) {
  64. misago._context.PROFILE_FOLLOWS = {
  65. count: 0,
  66. more: 0,
  67. page: 1,
  68. pages: 1,
  69. results: []
  70. };
  71. /* jshint ignore:start */
  72. testUtils.render(
  73. <Follows user={Object.assign({}, userMock, {id: 123})}
  74. profile={profileMock}
  75. users={[]} />
  76. );
  77. /* jshint ignore:end */
  78. testUtils.onElement('#test-mount p.lead', function(element) {
  79. assert.ok(element.length, "element renders");
  80. assert.equal($('#test-mount h3').text(), "You are following 0 users.",
  81. "component has valid header");
  82. assert.equal(element.text(), "You are not following any users.",
  83. "empty message was displayed");
  84. done();
  85. });
  86. });
  87. it("loads empty", function(done) {
  88. $.mockjax({
  89. url: '/test-api/users/?follows=123&name=&page=1',
  90. status: 200,
  91. responseText: {
  92. count: 0,
  93. more: 0,
  94. page: 1,
  95. pages: 1,
  96. results: []
  97. }
  98. });
  99. /* jshint ignore:start */
  100. testUtils.render(
  101. <Follows user={userMock}
  102. profile={profileMock}
  103. users={[]} />
  104. );
  105. /* jshint ignore:end */
  106. testUtils.onElement('#test-mount p.lead', function(element) {
  107. assert.ok(element.length, "element renders");
  108. assert.equal(element.text(), "BobBoberson is not following any users.",
  109. "empty message was displayed");
  110. done();
  111. });
  112. });
  113. it("loads empty (owned)", function(done) {
  114. $.mockjax({
  115. url: '/test-api/users/?follows=123&name=&page=1',
  116. status: 200,
  117. responseText: {
  118. count: 0,
  119. more: 0,
  120. page: 1,
  121. pages: 1,
  122. results: []
  123. }
  124. });
  125. /* jshint ignore:start */
  126. testUtils.render(
  127. <Follows user={Object.assign({}, userMock, {id: 123})}
  128. profile={profileMock}
  129. users={[]} />
  130. );
  131. /* jshint ignore:end */
  132. testUtils.onElement('#test-mount p.lead', function(element) {
  133. assert.ok(element.length, "element renders");
  134. assert.equal(element.text(), "You are not following any users.",
  135. "empty message was displayed");
  136. done();
  137. });
  138. });
  139. it("loads users list", function(done) {
  140. $.mockjax({
  141. url: '/test-api/users/?follows=123&name=&page=1',
  142. status: 200,
  143. responseText: {
  144. count: 5,
  145. more: 0,
  146. page: 1,
  147. pages: 1,
  148. results: [1, 2, 3, 4, 5].map(function(id) {
  149. return {
  150. id: id,
  151. username: 'BobBoberson' + id,
  152. avatar_hash: 'abcdfefa',
  153. joined_on: moment().format(),
  154. rank: {
  155. name: 'Test Rank',
  156. absolute_url: '/users/test-rank'
  157. }
  158. };
  159. })
  160. }
  161. });
  162. /* jshint ignore:start */
  163. testUtils.render(
  164. <Follows user={userMock}
  165. profile={profileMock}
  166. users={[]} />
  167. );
  168. /* jshint ignore:end */
  169. testUtils.onElement('#test-mount .users-cards-list.ui-ready', function() {
  170. assert.equal($('#test-mount h3').text(),
  171. "BobBoberson is following 5 users.",
  172. "component has valid header");
  173. assert.equal(store.getState().users.length, 5,
  174. "component renders with five items");
  175. done();
  176. });
  177. });
  178. it("loads more changes", function(done) {
  179. $.mockjax({
  180. url: '/test-api/users/?follows=123&name=&page=1',
  181. status: 200,
  182. responseText: {
  183. count: 10,
  184. more: 5,
  185. page: 1,
  186. pages: 2,
  187. results: [1, 2, 3, 4, 5].map(function(id) {
  188. return {
  189. id: id,
  190. username: 'BobBoberson' + id,
  191. avatar_hash: 'abcdfefa',
  192. joined_on: moment().format(),
  193. rank: {
  194. name: 'Test Rank',
  195. absolute_url: '/users/test-rank'
  196. }
  197. };
  198. })
  199. }
  200. });
  201. $.mockjax({
  202. url: '/test-api/users/?follows=123&name=&page=2',
  203. status: 200,
  204. responseText: {
  205. count: 10,
  206. more: 0,
  207. page: 2,
  208. pages: 2,
  209. results: [1, 2, 3, 4, 5].map(function(id) {
  210. return {
  211. id: 5 + id,
  212. username: 'BobBoberson' + (5 + id),
  213. avatar_hash: 'abcdfefa',
  214. joined_on: moment().format(),
  215. rank: {
  216. name: 'Test Rank',
  217. absolute_url: '/users/test-rank'
  218. }
  219. };
  220. })
  221. }
  222. });
  223. /* jshint ignore:start */
  224. testUtils.render(
  225. <Follows user={userMock}
  226. profile={profileMock}
  227. users={[]} />
  228. );
  229. /* jshint ignore:end */
  230. testUtils.onElement('#test-mount .pager-more .btn', function() {
  231. assert.equal($('#test-mount h3').text(),
  232. "BobBoberson is following 10 users.",
  233. "component has valid header");
  234. testUtils.simulateClick('#test-mount .pager-more .btn');
  235. window.setTimeout(function() {
  236. assert.equal(store.getState().users.length, 10,
  237. "component renders with ten items");
  238. done();
  239. }, 300);
  240. });
  241. });
  242. it("loads search results", function(done) {
  243. $.mockjax({
  244. url: '/test-api/users/?follows=123&name=&page=1',
  245. status: 200,
  246. responseText: {
  247. count: 10,
  248. more: 5,
  249. page: 1,
  250. pages: 2,
  251. results: [1, 2, 3, 4, 5].map(function(id) {
  252. return {
  253. id: id,
  254. username: 'BobBoberson' + id,
  255. avatar_hash: 'abcdfefa',
  256. joined_on: moment().format(),
  257. rank: {
  258. name: 'Test Rank',
  259. absolute_url: '/users/test-rank'
  260. }
  261. };
  262. })
  263. }
  264. });
  265. $.mockjax({
  266. url: '/test-api/users/?follows=123&name=test&page=1',
  267. status: 200,
  268. responseText: {
  269. count: 3,
  270. more: 0,
  271. page: 1,
  272. pages: 1,
  273. results: [1, 2, 3].map(function(id) {
  274. return {
  275. id: 10 + id,
  276. username: 'BobBoberson' + (10 + id),
  277. avatar_hash: 'abcdfefa',
  278. joined_on: moment().format(),
  279. rank: {
  280. name: 'Test Rank',
  281. absolute_url: '/users/test-rank'
  282. }
  283. };
  284. })
  285. }
  286. });
  287. /* jshint ignore:start */
  288. testUtils.render(
  289. <Follows user={userMock}
  290. profile={profileMock}
  291. users={[]} />
  292. );
  293. /* jshint ignore:end */
  294. testUtils.onElement('#test-mount .pager-more .btn', function() {
  295. assert.equal($('#test-mount h3').text(),
  296. "BobBoberson is following 10 users.",
  297. "component has valid header");
  298. testUtils.simulateChange('#test-mount .form-control', 'test');
  299. window.setTimeout(function() {
  300. assert.equal(store.getState().users.length, 3,
  301. "component renders with three found items");
  302. assert.equal($('#test-mount h3').text(), "Found 3 users.",
  303. "component has valid header");
  304. done();
  305. }, 300);
  306. });
  307. });
  308. it("loads empty search results", function(done) {
  309. $.mockjax({
  310. url: '/test-api/users/?follows=123&name=&page=1',
  311. status: 200,
  312. responseText: {
  313. count: 10,
  314. more: 5,
  315. page: 1,
  316. pages: 2,
  317. results: [1, 2, 3, 4, 5].map(function(id) {
  318. return {
  319. id: id,
  320. username: 'BobBoberson' + id,
  321. avatar_hash: 'abcdfefa',
  322. joined_on: moment().format(),
  323. rank: {
  324. name: 'Test Rank',
  325. absolute_url: '/users/test-rank'
  326. }
  327. };
  328. })
  329. }
  330. });
  331. $.mockjax({
  332. url: '/test-api/users/?follows=123&name=test&page=1',
  333. status: 200,
  334. responseText: {
  335. count: 0,
  336. more: 0,
  337. page: 1,
  338. pages: 1,
  339. results: []
  340. }
  341. });
  342. /* jshint ignore:start */
  343. testUtils.render(
  344. <Follows user={userMock}
  345. profile={profileMock}
  346. users={[]} />
  347. );
  348. /* jshint ignore:end */
  349. testUtils.onElement('#test-mount .pager-more .btn', function() {
  350. assert.equal($('#test-mount h3').text(),
  351. "BobBoberson is following 10 users.",
  352. "component has valid header");
  353. testUtils.simulateChange('#test-mount .form-control', 'test');
  354. window.setTimeout(function() {
  355. assert.equal(store.getState().users.length, 0,
  356. "store is emptied");
  357. assert.equal($('#test-mount h3').text(), "Found 0 users.",
  358. "component has valid header");
  359. done();
  360. }, 300);
  361. });
  362. });
  363. it("handles backend error", function(done) {
  364. $.mockjax({
  365. url: '/test-api/users/?follows=123&name=&page=1',
  366. status: 500
  367. });
  368. snackbarStore.callback(function(message) {
  369. assert.deepEqual(message, {
  370. message: "Unknown error has occured.",
  371. type: 'error'
  372. }, "error message was shown");
  373. done();
  374. });
  375. /* jshint ignore:start */
  376. testUtils.render(
  377. <Follows user={userMock}
  378. profile={profileMock}
  379. users={[]} />
  380. );
  381. /* jshint ignore:end */
  382. });
  383. it("handles backend rejection", function(done) {
  384. $.mockjax({
  385. url: '/test-api/users/?follows=123&name=&page=1',
  386. status: 403,
  387. responseText: {
  388. detail: "You can't see it yo!"
  389. }
  390. });
  391. snackbarStore.callback(function(message) {
  392. assert.deepEqual(message, {
  393. message: "You can't see it yo!",
  394. type: 'error'
  395. }, "error message was shown");
  396. done();
  397. });
  398. /* jshint ignore:start */
  399. testUtils.render(
  400. <Follows user={userMock}
  401. profile={profileMock}
  402. users={[]} />
  403. );
  404. /* jshint ignore:end */
  405. });
  406. });