threads.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import assert from 'assert';
  2. import moment from 'moment';
  3. import reducer, { append, hydrate, patch, hydrateThread } from 'misago/reducers/threads';
  4. describe("Threads Reducer", function() {
  5. it("hydrates thread", function() {
  6. let timestamp = moment().format();
  7. let thread = hydrateThread({
  8. started_on: timestamp,
  9. last_post_on: timestamp
  10. });
  11. assert.equal(thread.started_on.format(), timestamp,
  12. "thread start date was hydrated");
  13. assert.equal(thread.last_post_on.format(), timestamp,
  14. "thread last reply date was hydrated");
  15. });
  16. it("hydrates threads list", function() {
  17. let timestamp = moment().format();
  18. let threads = reducer([], hydrate([
  19. {
  20. id: 1,
  21. started_on: timestamp,
  22. last_post_on: timestamp
  23. },
  24. {
  25. id: 3,
  26. started_on: timestamp,
  27. last_post_on: timestamp
  28. }
  29. ]));
  30. assert.equal(threads.length, 2,
  31. "two threads were hydrated and set as state");
  32. assert.equal(threads[0].started_on.format(), timestamp,
  33. "first thread was hydrated");
  34. assert.equal(threads[1].started_on.format(), timestamp,
  35. "second thread was hydrated");
  36. });
  37. it("appends threads to list", function() {
  38. let timestamp = moment().format();
  39. let threads = reducer([
  40. {
  41. id: 1,
  42. started_on: timestamp,
  43. last_post_on: timestamp,
  44. last_post: 4
  45. },
  46. {
  47. id: 3,
  48. started_on: timestamp,
  49. last_post_on: timestamp,
  50. last_post: 1
  51. }
  52. ], append([
  53. {
  54. id: 1,
  55. started_on: timestamp,
  56. last_post_on: timestamp,
  57. last_post: 5
  58. },
  59. {
  60. id: 2,
  61. started_on: timestamp,
  62. last_post_on: timestamp,
  63. last_post: 3
  64. }
  65. ]));
  66. assert.equal(threads.length, 3, "one thread was added to state");
  67. assert.equal(threads[0].id, 1, "first thread wasn't moved");
  68. assert.equal(threads[0].last_post, 5, "first thread was updated");
  69. assert.equal(threads[1].id, 2, "new thread was appended as second");
  70. assert.equal(threads[2].id, 3, "old second thread is now last");
  71. });
  72. it("patches thread", function() {
  73. let timestamp = moment().format();
  74. let threads = reducer([
  75. {
  76. id: 1,
  77. started_on: timestamp,
  78. last_post_on: timestamp,
  79. last_post: 4
  80. },
  81. {
  82. id: 3,
  83. started_on: timestamp,
  84. last_post_on: timestamp,
  85. last_post: 1
  86. }
  87. ], patch({id: 3}, {
  88. id: 3,
  89. patch: 'yep'
  90. }));
  91. assert.equal(threads.length, 2, "state length remained same");
  92. assert.equal(threads[0].patch, undefined, "first thread wasn't changed");
  93. assert.equal(threads[1].patch, 'yep', "second thread was patched");
  94. });
  95. });