statuses.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.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('The Statuses module', () => {
  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('removes statuses by tag on deletion', () => {
  64. const state = defaultState()
  65. const status = makeMockStatus({id: '1'})
  66. const otherStatus = makeMockStatus({id: '3'})
  67. status.uri = 'xxx'
  68. const deletion = makeMockStatus({id: '2', type: 'deletion'})
  69. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  70. deletion.uri = 'xxx'
  71. mutations.addNewStatuses(state, { statuses: [status, otherStatus], showImmediately: true, timeline: 'public' })
  72. mutations.addNewStatuses(state, { statuses: [deletion], showImmediately: true, timeline: 'public' })
  73. expect(state.allStatuses).to.eql([otherStatus])
  74. expect(state.timelines.public.statuses).to.eql([otherStatus])
  75. expect(state.timelines.public.visibleStatuses).to.eql([otherStatus])
  76. expect(state.timelines.public.maxId).to.eql('3')
  77. })
  78. it('does not update the maxId when the noIdUpdate flag is set', () => {
  79. const state = defaultState()
  80. const status = makeMockStatus({id: '1'})
  81. const secondStatus = makeMockStatus({id: '2'})
  82. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  83. expect(state.timelines.public.maxId).to.eql('1')
  84. mutations.addNewStatuses(state, { statuses: [secondStatus], showImmediately: true, timeline: 'public', noIdUpdate: true })
  85. expect(state.timelines.public.statuses).to.eql([secondStatus, status])
  86. expect(state.timelines.public.visibleStatuses).to.eql([secondStatus, status])
  87. expect(state.timelines.public.maxId).to.eql('1')
  88. })
  89. it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => {
  90. const state = defaultState()
  91. const nonVisibleStatus = makeMockStatus({id: '1'})
  92. const status = makeMockStatus({id: '3'})
  93. const statusTwo = makeMockStatus({id: '2'})
  94. const statusThree = makeMockStatus({id: '4'})
  95. mutations.addNewStatuses(state, { statuses: [nonVisibleStatus], showImmediately: false, timeline: 'public' })
  96. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  97. mutations.addNewStatuses(state, { statuses: [statusTwo], showImmediately: true, timeline: 'public' })
  98. expect(state.timelines.public.minVisibleId).to.equal('2')
  99. mutations.addNewStatuses(state, { statuses: [statusThree], showImmediately: true, timeline: 'public' })
  100. expect(state.timelines.public.statuses).to.eql([statusThree, status, statusTwo, nonVisibleStatus])
  101. expect(state.timelines.public.visibleStatuses).to.eql([statusThree, status, statusTwo])
  102. })
  103. it('splits retweets from their status and links them', () => {
  104. const state = defaultState()
  105. const status = makeMockStatus({id: '1'})
  106. const retweet = makeMockStatus({id: '2', type: 'retweet'})
  107. const modStatus = makeMockStatus({id: '1', text: 'something else'})
  108. retweet.retweeted_status = status
  109. // It adds both statuses, but only the retweet to visible.
  110. mutations.addNewStatuses(state, { statuses: [retweet], timeline: 'public', showImmediately: true })
  111. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  112. expect(state.timelines.public.statuses).to.have.length(1)
  113. expect(state.allStatuses).to.have.length(2)
  114. expect(state.allStatuses[0].id).to.equal('1')
  115. expect(state.allStatuses[1].id).to.equal('2')
  116. // It refers to the modified status.
  117. mutations.addNewStatuses(state, { statuses: [modStatus], timeline: 'public' })
  118. expect(state.allStatuses).to.have.length(2)
  119. expect(state.allStatuses[0].id).to.equal('1')
  120. expect(state.allStatuses[0].text).to.equal(modStatus.text)
  121. expect(state.allStatuses[1].id).to.equal('2')
  122. expect(retweet.retweeted_status.text).to.eql(modStatus.text)
  123. })
  124. it('replaces existing statuses with the same id', () => {
  125. const state = defaultState()
  126. const status = makeMockStatus({id: '1'})
  127. const modStatus = makeMockStatus({id: '1', text: 'something else'})
  128. // Add original status
  129. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  130. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  131. expect(state.allStatuses).to.have.length(1)
  132. // Add new version of status
  133. mutations.addNewStatuses(state, { statuses: [modStatus], showImmediately: true, timeline: 'public' })
  134. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  135. expect(state.allStatuses).to.have.length(1)
  136. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  137. })
  138. it('replaces existing statuses with the same id, coming from a retweet', () => {
  139. const state = defaultState()
  140. const status = makeMockStatus({id: '1'})
  141. const modStatus = makeMockStatus({id: '1', text: 'something else'})
  142. const retweet = makeMockStatus({id: '2', type: 'retweet'})
  143. retweet.retweeted_status = modStatus
  144. // Add original status
  145. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  146. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  147. expect(state.allStatuses).to.have.length(1)
  148. // Add new version of status
  149. mutations.addNewStatuses(state, { statuses: [retweet], showImmediately: false, timeline: 'public' })
  150. expect(state.timelines.public.visibleStatuses).to.have.length(1)
  151. // Don't add the retweet itself if the tweet is visible
  152. expect(state.timelines.public.statuses).to.have.length(1)
  153. expect(state.allStatuses).to.have.length(2)
  154. expect(state.allStatuses[0].text).to.eql(modStatus.text)
  155. })
  156. it('handles favorite actions', () => {
  157. const state = defaultState()
  158. const status = makeMockStatus({id: '1'})
  159. const favorite = {
  160. id: '2',
  161. type: 'favorite',
  162. in_reply_to_status_id: '1', // The API uses strings here...
  163. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  164. text: 'a favorited something by b',
  165. user: { id: '99' }
  166. }
  167. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  168. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  169. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  170. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  171. expect(state.timelines.public.maxId).to.eq(favorite.id)
  172. // Adding it again does nothing
  173. mutations.addNewStatuses(state, { statuses: [favorite], showImmediately: true, timeline: 'public' })
  174. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  175. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  176. expect(state.timelines.public.maxId).to.eq(favorite.id)
  177. // 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.
  178. const user = {
  179. id: '1'
  180. }
  181. const ownFavorite = {
  182. id: '3',
  183. type: 'favorite',
  184. in_reply_to_status_id: '1', // The API uses strings here...
  185. uri: 'tag:shitposter.club,2016-08-21:fave:3895:note:773501:2016-08-21T16:52:15+00:00',
  186. text: 'a favorited something by b',
  187. user
  188. }
  189. mutations.addNewStatuses(state, { statuses: [ownFavorite], showImmediately: true, timeline: 'public', user })
  190. expect(state.timelines.public.visibleStatuses.length).to.eql(1)
  191. expect(state.timelines.public.visibleStatuses[0].fave_num).to.eql(1)
  192. expect(state.timelines.public.visibleStatuses[0].favorited).to.eql(true)
  193. })
  194. it('keeps userId when clearing user timeline', () => {
  195. const state = defaultState()
  196. state.timelines.user.userId = 123
  197. mutations.clearTimeline(state, { timeline: 'user' })
  198. expect(state.timelines.user.userId).to.eql(123)
  199. })
  200. describe('notifications', () => {
  201. it('removes a notification when the notice gets removed', () => {
  202. const user = { id: '1' }
  203. const state = defaultState()
  204. const status = makeMockStatus({id: '1'})
  205. const otherStatus = makeMockStatus({id: '3'})
  206. const mentionedStatus = makeMockStatus({id: '2'})
  207. mentionedStatus.attentions = [user]
  208. mentionedStatus.uri = 'xxx'
  209. otherStatus.attentions = [user]
  210. const deletion = makeMockStatus({id: '4', type: 'deletion'})
  211. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  212. deletion.uri = 'xxx'
  213. mutations.addNewStatuses(state, { statuses: [status, otherStatus], user })
  214. mutations.addNewNotifications(
  215. state,
  216. {
  217. notifications: [{
  218. from_profile: { id: '2' },
  219. id: '998',
  220. type: 'mention',
  221. status: otherStatus,
  222. action: otherStatus,
  223. seen: false
  224. }]
  225. })
  226. expect(state.notifications.data.length).to.eql(1)
  227. mutations.addNewNotifications(
  228. state,
  229. {
  230. notifications: [{
  231. from_profile: { id: '2' },
  232. id: '999',
  233. type: 'mention',
  234. status: mentionedStatus,
  235. action: mentionedStatus,
  236. seen: false
  237. }]
  238. })
  239. mutations.addNewStatuses(state, { statuses: [mentionedStatus], user })
  240. expect(state.allStatuses.length).to.eql(3)
  241. expect(state.notifications.data.length).to.eql(2)
  242. expect(state.notifications.data[1].status).to.eql(mentionedStatus)
  243. expect(state.notifications.data[1].action).to.eql(mentionedStatus)
  244. expect(state.notifications.data[1].type).to.eql('mention')
  245. mutations.addNewStatuses(state, { statuses: [deletion], user })
  246. expect(state.allStatuses.length).to.eql(2)
  247. expect(state.notifications.data.length).to.eql(1)
  248. })
  249. })
  250. })