threads.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import assert from 'assert';
  2. import moment from 'moment';
  3. import reducer, { append, hydrate, patch, read, 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. acl: {}
  11. });
  12. assert.equal(thread.started_on.format(), timestamp,
  13. "thread start date was hydrated");
  14. assert.equal(thread.last_post_on.format(), timestamp,
  15. "thread last reply date was hydrated");
  16. });
  17. it("hydrates threads list", function() {
  18. let timestamp = moment().format();
  19. let threads = reducer([], hydrate([
  20. {
  21. id: 1,
  22. started_on: timestamp,
  23. last_post_on: timestamp,
  24. acl: {}
  25. },
  26. {
  27. id: 3,
  28. started_on: timestamp,
  29. last_post_on: timestamp,
  30. acl: {}
  31. }
  32. ]));
  33. assert.equal(threads.length, 2,
  34. "two threads were hydrated and set as state");
  35. assert.equal(threads[0].started_on.format(), timestamp,
  36. "first thread was hydrated");
  37. assert.equal(threads[1].started_on.format(), timestamp,
  38. "second thread was hydrated");
  39. });
  40. it("appends threads to list", function() {
  41. let timestamp = moment().format();
  42. let threads = reducer([
  43. {
  44. id: 1,
  45. started_on: timestamp,
  46. last_post_on: timestamp,
  47. last_post: 4,
  48. acl: {}
  49. },
  50. {
  51. id: 3,
  52. started_on: timestamp,
  53. last_post_on: timestamp,
  54. last_post: 1,
  55. acl: {}
  56. }
  57. ], append([
  58. {
  59. id: 1,
  60. started_on: timestamp,
  61. last_post_on: timestamp,
  62. last_post: 5,
  63. acl: {}
  64. },
  65. {
  66. id: 2,
  67. started_on: timestamp,
  68. last_post_on: timestamp,
  69. last_post: 3,
  70. acl: {}
  71. }
  72. ]));
  73. assert.equal(threads.length, 3, "one thread was added to state");
  74. assert.equal(threads[0].id, 1, "first thread wasn't moved");
  75. assert.equal(threads[0].last_post, 5, "first thread was updated");
  76. assert.equal(threads[1].id, 2, "new thread was appended as second");
  77. assert.equal(threads[2].id, 3, "old second thread is now last");
  78. });
  79. it("patches thread", function() {
  80. let timestamp = moment().format();
  81. let threads = reducer([
  82. {
  83. id: 1,
  84. started_on: timestamp,
  85. last_post_on: timestamp,
  86. last_post: 4
  87. },
  88. {
  89. id: 3,
  90. started_on: timestamp,
  91. last_post_on: timestamp,
  92. last_post: 1
  93. }
  94. ], patch({id: 3}, {
  95. id: 3,
  96. patch: 'yep'
  97. }));
  98. assert.equal(threads.length, 2, "state length remained same");
  99. assert.equal(threads[0].patch, undefined, "first thread wasn't changed");
  100. assert.equal(threads[1].patch, 'yep', "second thread was patched");
  101. });
  102. it("marks threads as read", function() {
  103. let timestamp = moment().format();
  104. let threads = reducer([
  105. {
  106. id: 1,
  107. started_on: timestamp,
  108. last_post_on: timestamp,
  109. last_post: 4,
  110. is_read: false
  111. },
  112. {
  113. id: 3,
  114. started_on: timestamp,
  115. last_post_on: timestamp,
  116. last_post: 1,
  117. is_read: false
  118. }
  119. ], read());
  120. assert.equal(threads.length, 2, "state length remained same");
  121. assert.ok(threads[0].is_read, "first thread was marked as read");
  122. assert.ok(threads[1].is_read, "second thread was marked as read");
  123. });
  124. });