| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { describe, expect, it, vi } from 'vitest'
- import {
- getFileViewerOptionsSearchParam,
- normalizeFileViewerTheme,
- parseFileViewerOptions,
- sanitizeFileViewerOptions,
- serializeFileViewerOptions,
- setFileViewerOptionsSearchParam
- } from '../packages/core/src'
- describe('@file-viewer/core option serialization helpers', () => {
- it('normalizes viewer theme options for every wrapper', () => {
- expect(normalizeFileViewerTheme('light')).toBe('light')
- expect(normalizeFileViewerTheme('dark')).toBe('dark')
- expect(normalizeFileViewerTheme('system')).toBe('system')
- expect(normalizeFileViewerTheme(undefined)).toBe('system')
- })
- it('serializes execution-safe options without hook-only hooks', () => {
- const serialized = serializeFileViewerOptions({
- theme: 'light',
- toolbar: {
- print: true,
- position: 'bottom-right',
- beforePrint: vi.fn()
- },
- archive: {
- workerUrl: '/viewer/vendor/libarchive/worker-bundle.js',
- cache: true
- },
- cad: {
- workerUrl: new URL('https://viewer.example.com/cad-worker.js'),
- dxfEncoding: 'gbk'
- },
- rendererMode: 'replace',
- renderers: {
- id: 'runtime-only-renderer',
- definitions: [],
- },
- hooks: {
- onLoadStart: vi.fn()
- },
- beforeOperation: vi.fn()
- })
- expect(serialized).toBeTruthy()
- const options = JSON.parse(serialized || '{}')
- expect(options.theme).toBe('light')
- expect(options.toolbar).toEqual({
- print: true,
- position: 'bottom-right'
- })
- expect(options.archive.cache).toBe(true)
- expect(options.cad.workerUrl).toBe('https://viewer.example.com/cad-worker.js')
- expect(options.rendererMode).toBeUndefined()
- expect(options.renderers).toBeUndefined()
- expect(options.hooks).toBeUndefined()
- expect(options.beforeOperation).toBeUndefined()
- })
- it('parses query options through the same sanitizer', () => {
- expect(parseFileViewerOptions('{bad json')).toBeUndefined()
- expect(parseFileViewerOptions(JSON.stringify({
- watermark: {
- text: '内部预览',
- opacity: 0.12
- },
- toolbar: {
- download: false
- }
- }))).toEqual({
- watermark: {
- text: '内部预览',
- opacity: 0.12
- },
- toolbar: {
- download: false
- }
- })
- })
- it('can write and read options from URLSearchParams', () => {
- const params = new URLSearchParams()
- setFileViewerOptionsSearchParam(params, {
- pdf: {
- toolbar: false,
- streaming: 'same-origin'
- }
- })
- expect(params.has('options')).toBe(true)
- expect(getFileViewerOptionsSearchParam(params)).toEqual({
- pdf: {
- toolbar: false,
- streaming: 'same-origin'
- }
- })
- setFileViewerOptionsSearchParam(params, null)
- expect(params.has('options')).toBe(false)
- })
- it('returns undefined for empty or hook-only option objects', () => {
- expect(sanitizeFileViewerOptions(undefined)).toBeUndefined()
- expect(sanitizeFileViewerOptions({
- hooks: {
- onLoadComplete: vi.fn()
- },
- beforeOperation: vi.fn()
- })).toBeUndefined()
- })
- })
|