statuses.spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { cloneDeep } from 'lodash'
  2. import { defaultState, mutations, prepareStatus } from '../../../../src/modules/statuses.js'
  3. // eslint-disable-next-line camelcase
  4. const makeMockStatus = ({id, text, type = 'status'}) => {
  5. return {
  6. id,
  7. user: {id: '0'},
  8. name: 'status',
  9. text: text || `Text number ${id}`,
  10. fave_num: 0,
  11. uri: '',
  12. type,
  13. attentions: []
  14. }
  15. }
  16. describe('Statuses.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('The Statuses module', () => {
  23. it('adds the status to allStatuses and to the given timeline', () => {
  24. const state = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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 = cloneDeep(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. it('keeps userId when clearing user timeline', () => {
  196. const state = cloneDeep(defaultState)
  197. state.timelines.user.userId = 123
  198. mutations.clearTimeline(state, { timeline: 'user' })
  199. expect(state.timelines.user.userId).to.eql(123)
  200. })
  201. describe('notifications', () => {
  202. it('removes a notification when the notice gets removed', () => {
  203. const user = { id: '1' }
  204. const state = cloneDeep(defaultState)
  205. const status = makeMockStatus({id: '1'})
  206. const otherStatus = makeMockStatus({id: '3'})
  207. const mentionedStatus = makeMockStatus({id: '2'})
  208. mentionedStatus.attentions = [user]
  209. mentionedStatus.uri = 'xxx'
  210. otherStatus.attentions = [user]
  211. const deletion = makeMockStatus({id: '4', type: 'deletion'})
  212. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  213. deletion.uri = 'xxx'
  214. mutations.addNewStatuses(state, { statuses: [status, otherStatus], user })
  215. mutations.addNewNotifications(
  216. state,
  217. {
  218. notifications: [{
  219. from_profile: { id: '2' },
  220. id: '998',
  221. type: 'mention',
  222. status: otherStatus,
  223. action: otherStatus,
  224. seen: false
  225. }]
  226. })
  227. expect(state.notifications.data.length).to.eql(1)
  228. mutations.addNewNotifications(
  229. state,
  230. {
  231. notifications: [{
  232. from_profile: { id: '2' },
  233. id: '999',
  234. type: 'mention',
  235. status: mentionedStatus,
  236. action: mentionedStatus,
  237. seen: false
  238. }]
  239. })
  240. mutations.addNewStatuses(state, { statuses: [mentionedStatus], user })
  241. expect(state.allStatuses.length).to.eql(3)
  242. expect(state.notifications.data.length).to.eql(2)
  243. expect(state.notifications.data[1].status).to.eql(mentionedStatus)
  244. expect(state.notifications.data[1].action).to.eql(mentionedStatus)
  245. expect(state.notifications.data[1].type).to.eql('mention')
  246. mutations.addNewStatuses(state, { statuses: [deletion], user })
  247. expect(state.allStatuses.length).to.eql(2)
  248. expect(state.notifications.data.length).to.eql(1)
  249. })
  250. })
  251. })