statuses.spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 = [ { name: '😂', 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].me).to.eql(true)
  205. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
  206. })
  207. it('adds a new reaction', () => {
  208. const state = defaultState()
  209. const status = makeMockStatus({ id: '1' })
  210. status.emoji_reactions = []
  211. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  212. mutations.addOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  213. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
  214. expect(state.allStatusesObject['1'].emoji_reactions[0].me).to.eql(true)
  215. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
  216. })
  217. it('decreases count in existing reaction', () => {
  218. const state = defaultState()
  219. const status = makeMockStatus({ id: '1' })
  220. status.emoji_reactions = [ { name: '😂', count: 2, accounts: [{ id: 'me' }] } ]
  221. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  222. mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  223. expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
  224. expect(state.allStatusesObject['1'].emoji_reactions[0].me).to.eql(false)
  225. expect(state.allStatusesObject['1'].emoji_reactions[0].accounts).to.eql([])
  226. })
  227. it('removes a reaction', () => {
  228. const state = defaultState()
  229. const status = makeMockStatus({ id: '1' })
  230. status.emoji_reactions = [{ name: '😂', count: 1, accounts: [{ id: 'me' }] }]
  231. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  232. mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
  233. expect(state.allStatusesObject['1'].emoji_reactions.length).to.eql(0)
  234. })
  235. })
  236. describe('showNewStatuses', () => {
  237. it('resets the minId to the min of the visible statuses when adding new to visible statuses', () => {
  238. const state = defaultState()
  239. const status = makeMockStatus({ id: '10' })
  240. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  241. const newStatus = makeMockStatus({ id: '20' })
  242. mutations.addNewStatuses(state, { statuses: [newStatus], showImmediately: false, timeline: 'public' })
  243. state.timelines.public.minId = '5'
  244. mutations.showNewStatuses(state, { timeline: 'public' })
  245. expect(state.timelines.public.visibleStatuses.length).to.eql(2)
  246. expect(state.timelines.public.minVisibleId).to.eql('10')
  247. expect(state.timelines.public.minId).to.eql('10')
  248. })
  249. })
  250. describe('clearTimeline', () => {
  251. it('keeps userId when clearing user timeline when excludeUserId param is true', () => {
  252. const state = defaultState()
  253. state.timelines.user.userId = 123
  254. mutations.clearTimeline(state, { timeline: 'user', excludeUserId: true })
  255. expect(state.timelines.user.userId).to.eql(123)
  256. })
  257. })
  258. describe('notifications', () => {
  259. it('removes a notification when the notice gets removed', () => {
  260. const user = { id: '1' }
  261. const state = defaultState()
  262. const status = makeMockStatus({ id: '1' })
  263. const otherStatus = makeMockStatus({ id: '3' })
  264. const mentionedStatus = makeMockStatus({ id: '2' })
  265. mentionedStatus.attentions = [user]
  266. mentionedStatus.uri = 'xxx'
  267. otherStatus.attentions = [user]
  268. const deletion = makeMockStatus({ id: '4', type: 'deletion' })
  269. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  270. deletion.uri = 'xxx'
  271. const newNotificationSideEffects = () => {}
  272. mutations.addNewStatuses(state, { statuses: [status, otherStatus], user })
  273. mutations.addNewNotifications(
  274. state,
  275. {
  276. notifications: [{
  277. from_profile: { id: '2' },
  278. id: '998',
  279. type: 'mention',
  280. status: otherStatus,
  281. action: otherStatus,
  282. seen: false
  283. }],
  284. newNotificationSideEffects
  285. })
  286. expect(state.notifications.data.length).to.eql(1)
  287. mutations.addNewNotifications(
  288. state,
  289. {
  290. notifications: [{
  291. from_profile: { id: '2' },
  292. id: '999',
  293. type: 'mention',
  294. status: mentionedStatus,
  295. action: mentionedStatus,
  296. seen: false
  297. }],
  298. newNotificationSideEffects
  299. })
  300. mutations.addNewStatuses(state, { statuses: [mentionedStatus], user })
  301. expect(state.allStatuses.length).to.eql(3)
  302. expect(state.notifications.data.length).to.eql(2)
  303. expect(state.notifications.data[1].status).to.eql(mentionedStatus)
  304. expect(state.notifications.data[1].action).to.eql(mentionedStatus)
  305. expect(state.notifications.data[1].type).to.eql('mention')
  306. mutations.addNewStatuses(state, { statuses: [deletion], user })
  307. expect(state.allStatuses.length).to.eql(2)
  308. expect(state.notifications.data.length).to.eql(1)
  309. })
  310. })
  311. })