ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 lines
3.2 KiB

  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { fetchPostChanges, fetchPosts, toggleViewedFlg, updatePost } from '@/lib/posts'
  3. import type { FetchPostsParams } from '@/types'
  4. const api = vi.hoisted (() => ({
  5. apiDelete: vi.fn (),
  6. apiGet: vi.fn (),
  7. apiPost: vi.fn (),
  8. apiPut: vi.fn (),
  9. }))
  10. vi.mock ('@/lib/api', () => api)
  11. const baseParams: FetchPostsParams = {
  12. url: '',
  13. title: '',
  14. tags: '',
  15. match: 'all',
  16. originalCreatedFrom: '',
  17. originalCreatedTo: '',
  18. createdFrom: '',
  19. createdTo: '',
  20. updatedFrom: '',
  21. updatedTo: '',
  22. page: 1,
  23. limit: 20,
  24. order: 'updated_at:desc',
  25. }
  26. describe ('posts API functions', () => {
  27. beforeEach (() => {
  28. vi.clearAllMocks ()
  29. })
  30. it ('maps post search parameters to backend snake_case names', async () => {
  31. api.apiGet.mockResolvedValueOnce ({ posts: [], count: 0 })
  32. await fetchPosts ({
  33. ...baseParams,
  34. title: 'title',
  35. tags: 'a b',
  36. originalCreatedFrom: '2026-01-01',
  37. updatedTo: '2026-02-01',
  38. })
  39. expect (api.apiGet).toHaveBeenCalledWith (
  40. '/posts',
  41. {
  42. params: {
  43. title: 'title',
  44. tags: 'a b',
  45. match: 'all',
  46. original_created_from: '2026-01-01',
  47. updated_to: '2026-02-01',
  48. page: 1,
  49. limit: 20,
  50. order: 'updated_at:desc',
  51. },
  52. },
  53. )
  54. })
  55. it ('updates posts with version and merge controls', async () => {
  56. api.apiPut.mockResolvedValueOnce ({ id: 5 })
  57. await updatePost (
  58. {
  59. id: 5,
  60. title: 'new title',
  61. tags: 'tag',
  62. parentPostIds: '1 2',
  63. originalCreatedFrom: null,
  64. originalCreatedBefore: '2026-01-02T00:00:00Z',
  65. },
  66. { baseVersionNo: 7, force: true, merge: false },
  67. )
  68. expect (api.apiPut).toHaveBeenCalledWith (
  69. '/posts/5',
  70. {
  71. title: 'new title',
  72. tags: 'tag',
  73. parent_post_ids: '1 2',
  74. original_created_from: null,
  75. original_created_before: '2026-01-02T00:00:00Z',
  76. },
  77. {
  78. params: {
  79. base_version_no: '7',
  80. force: '1',
  81. merge: '0',
  82. },
  83. },
  84. )
  85. })
  86. it ('uses the viewed endpoint method matching the requested state', async () => {
  87. await toggleViewedFlg ('9', true)
  88. await toggleViewedFlg ('9', false)
  89. expect (api.apiPost).toHaveBeenCalledWith ('/posts/9/viewed')
  90. expect (api.apiDelete).toHaveBeenCalledWith ('/posts/9/viewed')
  91. })
  92. it ('keeps optional post history filters out when blank', async () => {
  93. api.apiGet.mockResolvedValueOnce ({ versions: [], count: 0 })
  94. await fetchPostChanges ({ page: 2, limit: 50 })
  95. expect (api.apiGet).toHaveBeenCalledWith (
  96. '/posts/versions',
  97. { params: { page: 2, limit: 50 } },
  98. )
  99. })
  100. })