statuses.spec.js 13 KB

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