ぼざクリタグ広場 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.
 
 
 
 

113 lines
3.4 KiB

  1. import { QueryClient } from '@tanstack/react-query'
  2. import { beforeEach, describe, expect, it, vi } from 'vitest'
  3. import { prefetchForURL } from '@/lib/prefetchers'
  4. const postsApi = vi.hoisted (() => ({
  5. fetchPost: vi.fn (),
  6. fetchPostChanges: vi.fn (),
  7. fetchPosts: vi.fn (),
  8. }))
  9. const tagsApi = vi.hoisted (() => ({
  10. fetchTag: vi.fn (),
  11. fetchTagByName: vi.fn (),
  12. fetchTagChanges: vi.fn (),
  13. fetchTags: vi.fn (),
  14. }))
  15. const wikiApi = vi.hoisted (() => ({
  16. fetchWikiPage: vi.fn (),
  17. fetchWikiPageByTitle: vi.fn (),
  18. fetchWikiPages: vi.fn (),
  19. }))
  20. vi.mock ('@/lib/posts', () => postsApi)
  21. vi.mock ('@/lib/tags', () => tagsApi)
  22. vi.mock ('@/lib/wiki', () => wikiApi)
  23. const qc = () => new QueryClient ({
  24. defaultOptions: { queries: { retry: false } },
  25. })
  26. describe ('prefetchForURL', () => {
  27. beforeEach (() => {
  28. vi.clearAllMocks ()
  29. postsApi.fetchPosts.mockResolvedValue ({ posts: [], count: 0 })
  30. postsApi.fetchPost.mockResolvedValue ({ id: 1 })
  31. postsApi.fetchPostChanges.mockResolvedValue ({ versions: [], count: 0 })
  32. tagsApi.fetchTags.mockResolvedValue ({ tags: [], count: 0 })
  33. tagsApi.fetchTag.mockResolvedValue ({ id: 1 })
  34. tagsApi.fetchTagByName.mockResolvedValue (null)
  35. tagsApi.fetchTagChanges.mockResolvedValue ({ versions: [], count: 0 })
  36. wikiApi.fetchWikiPages.mockResolvedValue ([])
  37. wikiApi.fetchWikiPage.mockResolvedValue ({ id: 1 })
  38. wikiApi.fetchWikiPageByTitle.mockResolvedValue (null)
  39. })
  40. it ('prefetches post indexes from query parameters', async () => {
  41. await prefetchForURL (
  42. qc (),
  43. 'http://localhost/posts?tags=a+b&match=any&page=2&limit=5&order=title%3Aasc',
  44. )
  45. expect (postsApi.fetchPosts).toHaveBeenCalledWith (
  46. expect.objectContaining ({
  47. tags: 'a b',
  48. match: 'any',
  49. page: 2,
  50. limit: 5,
  51. order: 'title:asc',
  52. }),
  53. )
  54. })
  55. it ('prefetches post detail pages', async () => {
  56. await prefetchForURL (qc (), 'http://localhost/posts/12')
  57. expect (postsApi.fetchPost).toHaveBeenCalledWith ('12')
  58. })
  59. it ('prefetches tag indexes from query parameters', async () => {
  60. await prefetchForURL (
  61. qc (),
  62. 'http://localhost/tags?post=9&name=x&category=general&page=4&post_count_lte=10',
  63. )
  64. expect (tagsApi.fetchTags).toHaveBeenCalledWith (
  65. expect.objectContaining ({
  66. post: 9,
  67. name: 'x',
  68. category: 'general',
  69. page: 4,
  70. postCountLTE: 10,
  71. }),
  72. )
  73. })
  74. it ('prefetches wiki show pages and related tag/post data', async () => {
  75. wikiApi.fetchWikiPageByTitle.mockResolvedValueOnce ({
  76. id: 3,
  77. title: 'Actual',
  78. body: 'body',
  79. })
  80. await prefetchForURL (qc (), 'http://localhost/wiki/Alias')
  81. expect (wikiApi.fetchWikiPageByTitle).toHaveBeenCalledWith ('Alias', { version: undefined })
  82. expect (wikiApi.fetchWikiPage).toHaveBeenCalledWith ('3', {})
  83. expect (tagsApi.fetchTagByName).toHaveBeenCalledWith ('Actual')
  84. expect (postsApi.fetchPosts).toHaveBeenCalledWith (
  85. expect.objectContaining ({ tags: 'Actual', limit: 8 }),
  86. )
  87. })
  88. it ('ignores routes without a prefetcher', async () => {
  89. await prefetchForURL (qc (), 'http://localhost/unknown')
  90. expect (postsApi.fetchPosts).not.toHaveBeenCalled ()
  91. expect (tagsApi.fetchTags).not.toHaveBeenCalled ()
  92. expect (wikiApi.fetchWikiPages).not.toHaveBeenCalled ()
  93. })
  94. })