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

49 lines
1.3 KiB

  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { fetchWikiPage, fetchWikiPageByTitle, fetchWikiPages } from '@/lib/wiki'
  3. const api = vi.hoisted (() => ({
  4. apiGet: vi.fn (),
  5. }))
  6. vi.mock ('@/lib/api', () => api)
  7. describe ('wiki API functions', () => {
  8. beforeEach (() => {
  9. vi.clearAllMocks ()
  10. })
  11. it ('fetches wiki index and show pages with expected parameters', async () => {
  12. api.apiGet.mockResolvedValueOnce ([])
  13. api.apiGet.mockResolvedValueOnce ({ id: 1 })
  14. await fetchWikiPages ({ title: '虹' })
  15. await fetchWikiPage ('1', { version: '3' })
  16. expect (api.apiGet).toHaveBeenNthCalledWith (
  17. 1,
  18. '/wiki',
  19. { params: { title: '虹' } },
  20. )
  21. expect (api.apiGet).toHaveBeenNthCalledWith (
  22. 2,
  23. '/wiki/1',
  24. { params: { version: '3' } },
  25. )
  26. })
  27. it ('encodes title path segments and returns null on misses', async () => {
  28. api.apiGet.mockResolvedValueOnce ({ id: 2 })
  29. api.apiGet.mockRejectedValueOnce (new Error ('missing'))
  30. await fetchWikiPageByTitle ('a/b c', { version: undefined })
  31. await expect (fetchWikiPageByTitle ('missing', {})).resolves.toBeNull ()
  32. expect (api.apiGet).toHaveBeenNthCalledWith (
  33. 1,
  34. '/wiki/title/a%2Fb%20c',
  35. { params: { version: undefined } },
  36. )
  37. })
  38. })