username-history.js 13 KB

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