logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

statuses.spec.js (13624B)


  1. import { defaultState, mutations, prepareStatus } from '../../../../src/modules/statuses.js'
  2. const makeMockStatus = ({ id, text, type = 'status' }) => {
  3. return {
  4. id,
  5. user: { id: '0' },
  6. name: 'status',
  7. text: text || `Text number ${id}`,
  8. fave_num: 0,
  9. uri: '',
  10. type,
  11. attentions: []
  12. }
  13. }
  14. describe('Statuses module', () => {
  15. describe('prepareStatus', () => {
  16. it('sets deleted flag to false', () => {
  17. const aStatus = makeMockStatus({ id: '1', text: 'Hello oniichan' })
  18. expect(prepareStatus(aStatus).deleted).to.eq(false)
  19. })
  20. })
  21. describe('addNewStatuses', () => {
  22. it('adds the status to allStatuses and to the given timeline', () => {
  23. const state = defaultState()
  24. const status = makeMockStatus({ id: '1' })
  25. mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' })
  26. expect(state.allStatuses).to.eql([status])
  27. expect(state.timelines.public.statuses).to.eql([status])
  28. expect(state.timelines.public.visibleStatuses).to.eql([])
  29. expect(state.timelines.public.newStatusCount).to.equal(1)
  30. })
  31. it('counts the status as new if it has not been seen on this timeline', () => {
  32. const state = defaultState()
  33. const status = makeMockStatus({ id: '1' })
  34. mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' })
  35. mutations.addNewStatuses(state, { statuses: [status], timeline: 'friends' })
  36. expect(state.allStatuses).to.eql([status])
  37. expect(state.timelines.public.statuses).to.eql([status])
  38. expect(state.timelines.public.visibleStatuses).to.eql([])
  39. expect(state.timelines.public.newStatusCount).to.equal(1)
  40. expect(state.allStatuses).to.eql([status])
  41. expect(state.timelines.friends.statuses).to.eql([status])
  42. expect(state.timelines.friends.visibleStatuses).to.eql([])
  43. expect(state.timelines.friends.newStatusCount).to.equal(1)
  44. })
  45. it('add the statuses to allStatuses if no timeline is given', () => {
  46. const state = defaultState()
  47. const status = makeMockStatus({ id: '1' })
  48. mutations.addNewStatuses(state, { statuses: [status] })
  49. expect(state.allStatuses).to.eql([status])
  50. expect(state.timelines.public.statuses).to.eql([])
  51. expect(state.timelines.public.visibleStatuses).to.eql([])
  52. expect(state.timelines.public.newStatusCount).to.equal(0)
  53. })
  54. it('adds the status to allStatuses and to the given timeline, directly visible', () => {
  55. const state = defaultState()
  56. const status = makeMockStatus({ id: '1' })
  57. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  58. expect(state.allStatuses).to.eql([status])
  59. expect(state.timelines.public.statuses).to.eql([status])
  60. expect(state.timelines.public.visibleStatuses).to.eql([status])
  61. expect(state.timelines.public.newStatusCount).to.equal(0)
  62. })
  63. it('does not update the maxId when the noIdUpdate flag is set', () => {
  64. const state = defaultState()
  65. const status = makeMockStatus({ id: '1' })
  66. const secondStatus = makeMockStatus({ id: '2' })
  67. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  68. expect(state.timelines.public.maxId).to.eql('1')
  69. mutations.addNewStatuses(state, { statuses: [secondStatus], showImmediately: true, timeline: 'public', noIdUpdate: true })
  70. expect(state.timelines.public.statuses).to.eql([secondStatus, status])
  71. expect(state.timelines.public.visibleStatuses).to.eql([secondStatus, status])
  72. expect(state.timelines.public.maxId).to.eql('1')
  73. })
  74. it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => {
  75. const state = defaultState()
  76. const nonVisibleStatus = makeMockStatus({ id: '1' })
  77. const status = makeMockStatus({ id: '3' })
  78. const statusTwo = makeMockStatus({ id: '2' })
  79. const statusThree = makeMockStatus({ id: '4' })
  80. mutations.addNewStatuses(state, { statuses: [nonVisibleStatus], showImmediately: false, timeline: 'public' })
  81. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  82. mutations.addNewStatuses(state, { statuses: [statusTwo], showImmediately: true, timeline: 'public' })
  83. expect(state.timelines.public.minVisibleId).to.equal('2')
  84. mutations.addNewStatuses(state, { statuses: [statusThree], showImmediately: true, timeline: 'public' })
  85. expect(state.timelines.public.statuses).to.eql([statusThree, status, statusTwo, nonVisibleStatus])
  86. expect(state.timelines.public.visibleStatuses).to.eql([statusThree, status, statusTwo])
  87. })
  88. it('splits retweets from their status and links them', () => {
  89. const state = defaultState()
  90. const status = makeMockStatus({ id: '1' })
  91. const retweet = makeMockStatus({ id: '2', type: 'retweet' })
  92. const modStatus = makeMockStatus({ id: '1', text: 'something else' })
  93. retweet.retweeted_status = status
  94. // It adds both statuses, but only the retweet to visible.
  95. mutations.addNewStatuses(state, { statuses: [retweet], timeline: 'public', showImmediately: true })
  96. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  97. expect(state.timelines.public.statuses).to.have.length(1)
  98. expect(state.allStatuses).to.have.length(2)
  99. expect(state.allStatuses[0].id).to.equal('1')
  100. expect(state.allStatuses[1].id).to.equal('2')
  101. // It refers to the modified status.
  102. mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' })
  103. expect(state.allStatuses).to.have.length(2)
  104. expect(state.allStatuses[0].id).to.equal('1')
  105. expect(state.allStatuses[0].text).to.equal(modStatus.text)
  106. expect(state.allStatuses[1].id).to.equal('2')
  107. expect(retweet.retweeted_status.text).to.eql(modStatus.text)
  108. })
  109. it('replaces existing statuses with the same id', () => {
  110. const state = defaultState()
  111. const status = makeMockStatus({ id: '1' })
  112. const modStatus = makeMockStatus({ id: '1', text: 'something else' })
  113. // Add original status
  114. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  115. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  116. expect(state.allStatuses).to.have.length(1)
  117. // Add new version of status
  118. mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' })
  119. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  120. expect(state.allStatuses).to.have.length(1)
  121. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  122. })
  123. it('replaces existing statuses with the same id, coming from a retweet', () => {
  124. const state = defaultState()
  125. const status = makeMockStatus({ id: '1' })
  126. const modStatus = makeMockStatus({ id: '1', text: 'something else' })
  127. const retweet = makeMockStatus({ id: '2', type: 'retweet' })
  128. retweet.retweeted_status = modStatus
  129. // Add original status
  130. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  131. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  132. expect(state.allStatuses).to.have.length(1)
  133. // Add new version of status
  134. mutations.addNewStatuses(state, { statuses: [retweet], showImmediately: false, timeline: 'public' })
  135. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  136. // Don't add the retweet itself if the tweet is visible
  137. expect(state.timelines.public.statuses).to.have.length(1)
  138. expect(state.allStatuses).to.have.length(2)
  139. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  140. })
  141. it('handles favorite actions', () => {
  142. const state = defaultState()
  143. const status = makeMockStatus({ id: '1' })
  144. const favorite = {
  145. id: '2',
  146. type: 'favorite',
  147. in_reply_to_status_id: '1', // The API uses strings here...
  148. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  149. text: 'a favorited something by b',
  150. user: { id: '99' }
  151. }
  152. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  153. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  154. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  155. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  156. expect(state.timelines.public.maxId).to.eq(favorite.id)
  157. // Adding it again does nothing
  158. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  159. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  160. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  161. expect(state.timelines.public.maxId).to.eq(favorite.id)
  162. // If something is favorited by the current user, it also sets the 'favorited' property but does not increment counter to avoid over-counting. Counter is incremented (updated, really) via response to the favorite request.
  163. const user = {
  164. id: '1'
  165. }
  166. const ownFavorite = {
  167. id: '3',
  168. type: 'favorite',
  169. in_reply_to_status_id: '1', // The API uses strings here...
  170. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  171. text: 'a favorited something by b',
  172. user
  173. }
  174. mutations.addNewStatuses(state, { statuses: [ownFavorite], showImmediately: true, timeline: 'public', user })
  175. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  176. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  177. expect(state.timelines.public.visibleStatuses[0].favorited).to.eql(true)
  178. })
  179. })
  180. describe('emojiReactions', () => {
  181. it('increments count in existing reaction', () => {
  182. const state = defaultState()
  183. const status = makeMockStatus({ id: '1' })
  184. status.emoji_reactions = [{ name: '😂', count: 1, accounts: [] }]
  185. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  186. mutations.addOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  187. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(2)
  188. expect(state.allStatusesObject['1'].emoji_reactions[0].me).to.eql(true)
  189. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
  190. })
  191. it('adds a new reaction', () => {
  192. const state = defaultState()
  193. const status = makeMockStatus({ id: '1' })
  194. status.emoji_reactions = []
  195. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  196. mutations.addOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  197. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
  198. expect(state.allStatusesObject['1'].emoji_reactions[0].me).to.eql(true)
  199. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
  200. })
  201. it('decreases count in existing reaction', () => {
  202. const state = defaultState()
  203. const status = makeMockStatus({ id: '1' })
  204. status.emoji_reactions = [{ name: '😂', count: 2, accounts: [{ id: 'me' }] }]
  205. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  206. mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  207. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
  208. expect(state.allStatusesObject['1'].emoji_reactions[0].me).to.eql(false)
  209. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts).to.eql([])
  210. })
  211. it('removes a reaction', () => {
  212. const state = defaultState()
  213. const status = makeMockStatus({ id: '1' })
  214. status.emoji_reactions = [{ name: '😂', count: 1, accounts: [{ id: 'me' }] }]
  215. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  216. mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  217. expect(state.allStatusesObject['1'].emoji_reactions.length).to.eql(0)
  218. })
  219. })
  220. describe('showNewStatuses', () => {
  221. it('resets the minId to the min of the visible statuses when adding new to visible statuses', () => {
  222. const state = defaultState()
  223. const status = makeMockStatus({ id: '10' })
  224. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  225. const newStatus = makeMockStatus({ id: '20' })
  226. mutations.addNewStatuses(state, { statuses: [newStatus], showImmediately: false, timeline: 'public' })
  227. state.timelines.public.minId = '5'
  228. mutations.showNewStatuses(state, { timeline: 'public' })
  229. expect(state.timelines.public.visibleStatuses.length).to.eql(2)
  230. expect(state.timelines.public.minVisibleId).to.eql('10')
  231. expect(state.timelines.public.minId).to.eql('10')
  232. })
  233. })
  234. describe('clearTimeline', () => {
  235. it('keeps userId when clearing user timeline when excludeUserId param is true', () => {
  236. const state = defaultState()
  237. state.timelines.user.userId = 123
  238. mutations.clearTimeline(state, { timeline: 'user', excludeUserId: true })
  239. expect(state.timelines.user.userId).to.eql(123)
  240. })
  241. })
  242. })