vue-toolbar-hook.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { computed, effectScope, nextTick, reactive, ref, shallowRef } from 'vue'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import { useViewerToolbar } from '../packages/components/vue3/src/package/components/FileViewer/hooks/useViewerToolbar'
  4. import type {
  5. FileViewerOperationAvailability,
  6. FileViewerToolbarOptions,
  7. FileViewerZoomState
  8. } from '../packages/components/vue3/src/package/common/type'
  9. describe('Vue FileViewer toolbar hook', () => {
  10. it('keeps toolbar visibility, PDF floating position and emitted capability states in one facade', async () => {
  11. const activeExportAdapter = shallowRef(null)
  12. const currentBuffer = ref(new ArrayBuffer(8))
  13. const currentExtend = computed(() => 'pdf')
  14. const currentFile = ref<File | null>(null)
  15. const currentSourceUrl = ref<string | null>(null)
  16. const error = ref('')
  17. const loading = ref(false)
  18. const renderedReady = ref(true)
  19. const normalizedToolbar = computed<FileViewerToolbarOptions>(() => ({
  20. download: true,
  21. print: true,
  22. exportHtml: true,
  23. zoom: true
  24. }))
  25. const zoomState = reactive<FileViewerZoomState>({
  26. scale: 1,
  27. label: '100%',
  28. canZoomIn: true,
  29. canZoomOut: false,
  30. canReset: false,
  31. minScale: 0.25,
  32. maxScale: 4
  33. })
  34. const availabilityEvents: FileViewerOperationAvailability[] = []
  35. const zoomEvents: FileViewerZoomState[] = []
  36. const scope = effectScope()
  37. const toolbar = scope.run(() => useViewerToolbar({
  38. activeExportAdapter,
  39. currentBuffer,
  40. currentExtend,
  41. currentFile,
  42. currentSourceUrl,
  43. error,
  44. getOptions: () => undefined,
  45. getZoomState: () => ({ ...zoomState }),
  46. loading,
  47. normalizedToolbar,
  48. renderedReady,
  49. zoomState,
  50. emitOperationAvailabilityChange: availability => {
  51. availabilityEvents.push(availability)
  52. },
  53. emitZoomChange: state => {
  54. zoomEvents.push(state)
  55. }
  56. }))
  57. expect(toolbar).toBeTruthy()
  58. expect(toolbar?.operationAvailability.value).toMatchObject({
  59. download: true,
  60. print: false,
  61. exportHtml: true,
  62. zoom: true,
  63. zoomIn: true,
  64. zoomOut: false
  65. })
  66. expect(toolbar?.visibleToolbar.value).toMatchObject({
  67. download: true,
  68. print: false,
  69. exportHtml: true,
  70. zoom: true
  71. })
  72. expect(toolbar?.showToolbar.value).toBe(true)
  73. expect(toolbar?.toolbarPosition.value).toBe('bottom-right')
  74. expect(toolbar?.zoomButtonDisabled('canZoomIn')).toBe(false)
  75. expect(toolbar?.zoomButtonDisabled('canZoomOut')).toBe(true)
  76. expect(availabilityEvents[0]).toMatchObject(toolbar?.operationAvailability.value || {})
  77. expect(zoomEvents[0]).toMatchObject({ label: '100%' })
  78. const zoomEventCount = zoomEvents.length
  79. zoomState.minScale = 0.5
  80. zoomState.maxScale = 5
  81. await nextTick()
  82. expect(zoomEvents).toHaveLength(zoomEventCount)
  83. error.value = 'render failed'
  84. await nextTick()
  85. expect(toolbar?.toolbarDisabled.value).toBe(true)
  86. expect(availabilityEvents.at(-1)).toMatchObject({
  87. download: true,
  88. print: false,
  89. exportHtml: false,
  90. zoom: false
  91. })
  92. scope.stop()
  93. })
  94. })