core-options.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { describe, expect, it, vi } from 'vitest'
  2. import {
  3. getFileViewerOptionsSearchParam,
  4. normalizeFileViewerTheme,
  5. parseFileViewerOptions,
  6. sanitizeFileViewerOptions,
  7. serializeFileViewerOptions,
  8. setFileViewerOptionsSearchParam
  9. } from '../packages/core/src'
  10. describe('@file-viewer/core option serialization helpers', () => {
  11. it('normalizes viewer theme options for every wrapper', () => {
  12. expect(normalizeFileViewerTheme('light')).toBe('light')
  13. expect(normalizeFileViewerTheme('dark')).toBe('dark')
  14. expect(normalizeFileViewerTheme('system')).toBe('system')
  15. expect(normalizeFileViewerTheme(undefined)).toBe('system')
  16. })
  17. it('serializes execution-safe options without hook-only hooks', () => {
  18. const serialized = serializeFileViewerOptions({
  19. theme: 'light',
  20. toolbar: {
  21. print: true,
  22. position: 'bottom-right',
  23. beforePrint: vi.fn()
  24. },
  25. archive: {
  26. workerUrl: '/viewer/vendor/libarchive/worker-bundle.js',
  27. cache: true
  28. },
  29. cad: {
  30. workerUrl: new URL('https://viewer.example.com/cad-worker.js'),
  31. dxfEncoding: 'gbk'
  32. },
  33. rendererMode: 'replace',
  34. renderers: {
  35. id: 'runtime-only-renderer',
  36. definitions: [],
  37. },
  38. hooks: {
  39. onLoadStart: vi.fn()
  40. },
  41. beforeOperation: vi.fn()
  42. })
  43. expect(serialized).toBeTruthy()
  44. const options = JSON.parse(serialized || '{}')
  45. expect(options.theme).toBe('light')
  46. expect(options.toolbar).toEqual({
  47. print: true,
  48. position: 'bottom-right'
  49. })
  50. expect(options.archive.cache).toBe(true)
  51. expect(options.cad.workerUrl).toBe('https://viewer.example.com/cad-worker.js')
  52. expect(options.rendererMode).toBeUndefined()
  53. expect(options.renderers).toBeUndefined()
  54. expect(options.hooks).toBeUndefined()
  55. expect(options.beforeOperation).toBeUndefined()
  56. })
  57. it('parses query options through the same sanitizer', () => {
  58. expect(parseFileViewerOptions('{bad json')).toBeUndefined()
  59. expect(parseFileViewerOptions(JSON.stringify({
  60. watermark: {
  61. text: '内部预览',
  62. opacity: 0.12
  63. },
  64. toolbar: {
  65. download: false
  66. }
  67. }))).toEqual({
  68. watermark: {
  69. text: '内部预览',
  70. opacity: 0.12
  71. },
  72. toolbar: {
  73. download: false
  74. }
  75. })
  76. })
  77. it('can write and read options from URLSearchParams', () => {
  78. const params = new URLSearchParams()
  79. setFileViewerOptionsSearchParam(params, {
  80. pdf: {
  81. toolbar: false,
  82. streaming: 'same-origin'
  83. }
  84. })
  85. expect(params.has('options')).toBe(true)
  86. expect(getFileViewerOptionsSearchParam(params)).toEqual({
  87. pdf: {
  88. toolbar: false,
  89. streaming: 'same-origin'
  90. }
  91. })
  92. setFileViewerOptionsSearchParam(params, null)
  93. expect(params.has('options')).toBe(false)
  94. })
  95. it('returns undefined for empty or hook-only option objects', () => {
  96. expect(sanitizeFileViewerOptions(undefined)).toBeUndefined()
  97. expect(sanitizeFileViewerOptions({
  98. hooks: {
  99. onLoadComplete: vi.fn()
  100. },
  101. beforeOperation: vi.fn()
  102. })).toBeUndefined()
  103. })
  104. })