statuses.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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('showNewStatuses', () => {
  197. it('resets the minId to the min of the visible statuses when adding new to visible statuses', () => {
  198. const state = defaultState()
  199. const status = makeMockStatus({ id: '10' })
  200. mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
  201. const newStatus = makeMockStatus({ id: '20' })
  202. mutations.addNewStatuses(state, { statuses: [newStatus], showImmediately: false, timeline: 'public' })
  203. state.timelines.public.minId = '5'
  204. mutations.showNewStatuses(state, { timeline: 'public' })
  205. expect(state.timelines.public.visibleStatuses.length).to.eql(2)
  206. expect(state.timelines.public.minVisibleId).to.eql('10')
  207. expect(state.timelines.public.minId).to.eql('10')
  208. })
  209. })
  210. describe('clearTimeline', () => {
  211. it('keeps userId when clearing user timeline when excludeUserId param is true', () => {
  212. const state = defaultState()
  213. state.timelines.user.userId = 123
  214. mutations.clearTimeline(state, { timeline: 'user', excludeUserId: true })
  215. expect(state.timelines.user.userId).to.eql(123)
  216. })
  217. })
  218. describe('notifications', () => {
  219. it('removes a notification when the notice gets removed', () => {
  220. const user = { id: '1' }
  221. const state = defaultState()
  222. const status = makeMockStatus({ id: '1' })
  223. const otherStatus = makeMockStatus({ id: '3' })
  224. const mentionedStatus = makeMockStatus({ id: '2' })
  225. mentionedStatus.attentions = [user]
  226. mentionedStatus.uri = 'xxx'
  227. otherStatus.attentions = [user]
  228. const deletion = makeMockStatus({ id: '4', type: 'deletion' })
  229. deletion.text = 'Dolus deleted notice {{tag:gs.smuglo.li,2016-11-18:noticeId=1038007:objectType=note}}.'
  230. deletion.uri = 'xxx'
  231. mutations.addNewStatuses(state, { statuses: [status, otherStatus], user })
  232. mutations.addNewNotifications(
  233. state,
  234. {
  235. notifications: [{
  236. from_profile: { id: '2' },
  237. id: '998',
  238. type: 'mention',
  239. status: otherStatus,
  240. action: otherStatus,
  241. seen: false
  242. }]
  243. })
  244. expect(state.notifications.data.length).to.eql(1)
  245. mutations.addNewNotifications(
  246. state,
  247. {
  248. notifications: [{
  249. from_profile: { id: '2' },
  250. id: '999',
  251. type: 'mention',
  252. status: mentionedStatus,
  253. action: mentionedStatus,
  254. seen: false
  255. }]
  256. })
  257. mutations.addNewStatuses(state, { statuses: [mentionedStatus], user })
  258. expect(state.allStatuses.length).to.eql(3)
  259. expect(state.notifications.data.length).to.eql(2)
  260. expect(state.notifications.data[1].status).to.eql(mentionedStatus)
  261. expect(state.notifications.data[1].action).to.eql(mentionedStatus)
  262. expect(state.notifications.data[1].type).to.eql('mention')
  263. mutations.addNewStatuses(state, { statuses: [deletion], user })
  264. expect(state.allStatuses.length).to.eql(2)
  265. expect(state.notifications.data.length).to.eql(1)
  266. })
  267. })
  268. })