statuses.spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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('removes statuses by tag on deletion', () => {
  65. const state = defaultState()
  66. const status = makeMockStatus({ id: '1' })
  67. const otherStatus = makeMockStatus({ id: '3' })
  68. status.uri = 'xxx'
  69. const deletion = makeMockStatus({ id: '2', type: 'deletion' })
  70. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  71. deletion.uri = 'xxx'
  72. mutations.addNewStatuses(state, { statuses: [status, otherStatus], showImmediately: true, timeline: 'public' })
  73. mutations.addNewStatuses(state, { statuses: [deletion], showImmediately: true, timeline: 'public' })
  74. expect(state.allStatuses).to.eql([otherStatus])
  75. expect(state.timelines.public.statuses).to.eql([otherStatus])
  76. expect(state.timelines.public.visibleStatuses).to.eql([otherStatus])
  77. expect(state.timelines.public.maxId).to.eql('3')
  78. })
  79. it('does not update the maxId when the noIdUpdate flag is set', () => {
  80. const state = defaultState()
  81. const status = makeMockStatus({ id: '1' })
  82. const secondStatus = makeMockStatus({ id: '2' })
  83. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  84. expect(state.timelines.public.maxId).to.eql('1')
  85. mutations.addNewStatuses(state, { statuses: [secondStatus], showImmediately: true, timeline: 'public', noIdUpdate: true })
  86. expect(state.timelines.public.statuses).to.eql([secondStatus, status])
  87. expect(state.timelines.public.visibleStatuses).to.eql([secondStatus, status])
  88. expect(state.timelines.public.maxId).to.eql('1')
  89. })
  90. it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => {
  91. const state = defaultState()
  92. const nonVisibleStatus = makeMockStatus({ id: '1' })
  93. const status = makeMockStatus({ id: '3' })
  94. const statusTwo = makeMockStatus({ id: '2' })
  95. const statusThree = makeMockStatus({ id: '4' })
  96. mutations.addNewStatuses(state, { statuses: [nonVisibleStatus], showImmediately: false, timeline: 'public' })
  97. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  98. mutations.addNewStatuses(state, { statuses: [statusTwo], showImmediately: true, timeline: 'public' })
  99. expect(state.timelines.public.minVisibleId).to.equal('2')
  100. mutations.addNewStatuses(state, { statuses: [statusThree], showImmediately: true, timeline: 'public' })
  101. expect(state.timelines.public.statuses).to.eql([statusThree, status, statusTwo, nonVisibleStatus])
  102. expect(state.timelines.public.visibleStatuses).to.eql([statusThree, status, statusTwo])
  103. })
  104. it('splits retweets from their status and links them', () => {
  105. const state = defaultState()
  106. const status = makeMockStatus({ id: '1' })
  107. const retweet = makeMockStatus({ id: '2', type: 'retweet' })
  108. const modStatus = makeMockStatus({ id: '1', text: 'something else' })
  109. retweet.retweeted_status = status
  110. // It adds both statuses, but only the retweet to visible.
  111. mutations.addNewStatuses(state, { statuses: [retweet], timeline: 'public', showImmediately: true })
  112. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  113. expect(state.timelines.public.statuses).to.have.length(1)
  114. expect(state.allStatuses).to.have.length(2)
  115. expect(state.allStatuses[0].id).to.equal('1')
  116. expect(state.allStatuses[1].id).to.equal('2')
  117. // It refers to the modified status.
  118. mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' })
  119. expect(state.allStatuses).to.have.length(2)
  120. expect(state.allStatuses[0].id).to.equal('1')
  121. expect(state.allStatuses[0].text).to.equal(modStatus.text)
  122. expect(state.allStatuses[1].id).to.equal('2')
  123. expect(retweet.retweeted_status.text).to.eql(modStatus.text)
  124. })
  125. it('replaces existing statuses with the same id', () => {
  126. const state = defaultState()
  127. const status = makeMockStatus({ id: '1' })
  128. const modStatus = makeMockStatus({ id: '1', text: 'something else' })
  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: [modStatus], showImmediately: true, timeline: 'public' })
  135. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  136. expect(state.allStatuses).to.have.length(1)
  137. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  138. })
  139. it('replaces existing statuses with the same id, coming from a retweet', () => {
  140. const state = defaultState()
  141. const status = makeMockStatus({ id: '1' })
  142. const modStatus = makeMockStatus({ id: '1', text: 'something else' })
  143. const retweet = makeMockStatus({ id: '2', type: 'retweet' })
  144. retweet.retweeted_status = modStatus
  145. // Add original status
  146. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  147. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  148. expect(state.allStatuses).to.have.length(1)
  149. // Add new version of status
  150. mutations.addNewStatuses(state, { statuses: [retweet], showImmediately: false, timeline: 'public' })
  151. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  152. // Don't add the retweet itself if the tweet is visible
  153. expect(state.timelines.public.statuses).to.have.length(1)
  154. expect(state.allStatuses).to.have.length(2)
  155. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  156. })
  157. it('handles favorite actions', () => {
  158. const state = defaultState()
  159. const status = makeMockStatus({ id: '1' })
  160. const favorite = {
  161. id: '2',
  162. type: 'favorite',
  163. in_reply_to_status_id: '1', // The API uses strings here...
  164. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  165. text: 'a favorited something by b',
  166. user: { id: '99' }
  167. }
  168. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  169. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  170. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  171. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  172. expect(state.timelines.public.maxId).to.eq(favorite.id)
  173. // Adding it again does nothing
  174. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  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.maxId).to.eq(favorite.id)
  178. // 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.
  179. const user = {
  180. id: '1'
  181. }
  182. const ownFavorite = {
  183. id: '3',
  184. type: 'favorite',
  185. in_reply_to_status_id: '1', // The API uses strings here...
  186. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  187. text: 'a favorited something by b',
  188. user
  189. }
  190. mutations.addNewStatuses(state, { statuses: [ownFavorite], showImmediately: true, timeline: 'public', user })
  191. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  192. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  193. expect(state.timelines.public.visibleStatuses[0].favorited).to.eql(true)
  194. })
  195. })
  196. describe('emojiReactions', () => {
  197. it('increments count in existing reaction', () => {
  198. const state = defaultState()
  199. const status = makeMockStatus({ id: '1' })
  200. status.emoji_reactions = [ { emoji: '😂', count: 1, accounts: [] } ]
  201. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  202. mutations.addOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  203. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(2)
  204. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
  205. })
  206. it('adds a new reaction', () => {
  207. const state = defaultState()
  208. const status = makeMockStatus({ id: '1' })
  209. status.emoji_reactions = []
  210. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  211. mutations.addOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  212. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
  213. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
  214. })
  215. it('decreases count in existing reaction', () => {
  216. const state = defaultState()
  217. const status = makeMockStatus({ id: '1' })
  218. status.emoji_reactions = [ { emoji: '😂', count: 2, accounts: [{ id: 'me' }] } ]
  219. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  220. mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: {} })
  221. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
  222. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts).to.eql([])
  223. })
  224. it('removes a reaction', () => {
  225. const state = defaultState()
  226. const status = makeMockStatus({ id: '1' })
  227. status.emoji_reactions = [{ emoji: '😂', count: 1, accounts: [{ id: 'me' }] }]
  228. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  229. mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: {} })
  230. expect(state.allStatusesObject['1'].emoji_reactions.length).to.eql(0)
  231. })
  232. })
  233. describe('showNewStatuses', () => {
  234. it('resets the minId to the min of the visible statuses when adding new to visible statuses', () => {
  235. const state = defaultState()
  236. const status = makeMockStatus({ id: '10' })
  237. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  238. const newStatus = makeMockStatus({ id: '20' })
  239. mutations.addNewStatuses(state, { statuses: [newStatus], showImmediately: false, timeline: 'public' })
  240. state.timelines.public.minId = '5'
  241. mutations.showNewStatuses(state, { timeline: 'public' })
  242. expect(state.timelines.public.visibleStatuses.length).to.eql(2)
  243. expect(state.timelines.public.minVisibleId).to.eql('10')
  244. expect(state.timelines.public.minId).to.eql('10')
  245. })
  246. })
  247. describe('clearTimeline', () => {
  248. it('keeps userId when clearing user timeline when excludeUserId param is true', () => {
  249. const state = defaultState()
  250. state.timelines.user.userId = 123
  251. mutations.clearTimeline(state, { timeline: 'user', excludeUserId: true })
  252. expect(state.timelines.user.userId).to.eql(123)
  253. })
  254. })
  255. describe('notifications', () => {
  256. it('removes a notification when the notice gets removed', () => {
  257. const user = { id: '1' }
  258. const state = defaultState()
  259. const status = makeMockStatus({ id: '1' })
  260. const otherStatus = makeMockStatus({ id: '3' })
  261. const mentionedStatus = makeMockStatus({ id: '2' })
  262. mentionedStatus.attentions = [user]
  263. mentionedStatus.uri = 'xxx'
  264. otherStatus.attentions = [user]
  265. const deletion = makeMockStatus({ id: '4', type: 'deletion' })
  266. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  267. deletion.uri = 'xxx'
  268. mutations.addNewStatuses(state, { statuses: [status, otherStatus], user })
  269. mutations.addNewNotifications(
  270. state,
  271. {
  272. notifications: [{
  273. from_profile: { id: '2' },
  274. id: '998',
  275. type: 'mention',
  276. status: otherStatus,
  277. action: otherStatus,
  278. seen: false
  279. }]
  280. })
  281. expect(state.notifications.data.length).to.eql(1)
  282. mutations.addNewNotifications(
  283. state,
  284. {
  285. notifications: [{
  286. from_profile: { id: '2' },
  287. id: '999',
  288. type: 'mention',
  289. status: mentionedStatus,
  290. action: mentionedStatus,
  291. seen: false
  292. }]
  293. })
  294. mutations.addNewStatuses(state, { statuses: [mentionedStatus], user })
  295. expect(state.allStatuses.length).to.eql(3)
  296. expect(state.notifications.data.length).to.eql(2)
  297. expect(state.notifications.data[1].status).to.eql(mentionedStatus)
  298. expect(state.notifications.data[1].action).to.eql(mentionedStatus)
  299. expect(state.notifications.data[1].type).to.eql('mention')
  300. mutations.addNewStatuses(state, { statuses: [deletion], user })
  301. expect(state.allStatuses.length).to.eql(2)
  302. expect(state.notifications.data.length).to.eql(1)
  303. })
  304. })
  305. })