vue-presentation-hook.spec.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { describe, expect, it } from 'vitest'
  2. import { computed, ref } from 'vue'
  3. import { useViewerErrorState, useViewerPresentation } from '../packages/components/vue3/src/package/components/FileViewer/hooks/useViewerPresentation'
  4. import { resolveFileViewerLoadingTheme } from '../packages/core/src'
  5. import type { FileViewerOptions } from '../packages/components/vue3/src/package/common/type'
  6. describe('Vue FileViewer presentation hook', () => {
  7. it('derives filename, extension, theme and toolbar defaults through core rules', () => {
  8. const filename = ref('')
  9. const options = ref<FileViewerOptions | undefined>({
  10. theme: 'dark',
  11. toolbar: {
  12. download: false,
  13. position: 'bottom-right'
  14. }
  15. })
  16. const presentation = useViewerPresentation({
  17. filename,
  18. getFile: () => new File(['demo'], '合同.docx'),
  19. getUrl: () => '/example/%E6%8A%A5%E5%91%8A.pdf?token=1',
  20. getOptions: () => options.value
  21. })
  22. expect(presentation.displayFilename.value).toBe('合同.docx')
  23. expect(presentation.currentExtend.value).toBe('docx')
  24. expect(presentation.viewerTheme.value).toBe('dark')
  25. expect(presentation.normalizedToolbar.value).toMatchObject({
  26. download: false,
  27. print: true,
  28. exportHtml: true,
  29. zoom: true
  30. })
  31. filename.value = 'manual.PDF'
  32. expect(presentation.displayFilename.value).toBe('manual.PDF')
  33. expect(presentation.currentExtend.value).toBe('pdf')
  34. options.value = { theme: 'system' }
  35. expect(presentation.viewerTheme.value).toBe('system')
  36. expect(presentation.normalizedToolbar.value).toMatchObject({
  37. download: true,
  38. print: true,
  39. exportHtml: true,
  40. zoom: true
  41. })
  42. expect(presentation.formatErrorMessage('加载失败', new Error('网络异常'))).toBe('加载失败:网络异常')
  43. })
  44. it('builds error state with the active extension and loading theme', () => {
  45. const currentExtend = ref('pdf')
  46. const error = ref('解析失败')
  47. const loadingTheme = computed(() => resolveFileViewerLoadingTheme(currentExtend.value))
  48. const errorState = useViewerErrorState({
  49. currentExtend: computed(() => currentExtend.value),
  50. error: computed(() => error.value),
  51. loadingTheme
  52. })
  53. expect(errorState.value).toMatchObject({
  54. state: 'error',
  55. extension: 'pdf',
  56. title: '预览失败',
  57. message: '解析失败',
  58. recoverable: true
  59. })
  60. currentExtend.value = 'docx'
  61. error.value = '读取失败'
  62. expect(errorState.value).toMatchObject({
  63. extension: 'docx',
  64. message: '读取失败'
  65. })
  66. })
  67. })