Files
dify/web/hooks/use-document-title.spec.ts
2026-05-25 17:22:11 +08:00

101 lines
3.5 KiB
TypeScript

import { renderHookWithSystemFeatures } from '@/__tests__/utils/mock-system-features'
/**
* Test suite for useDocumentTitle hook
*
* This hook manages the browser document title with support for:
* - Custom branding (when enabled in system features)
* - Default "Dify" branding
* - Pending state handling (prevents title flicker during loading)
* - Page-specific titles with automatic suffix
*
* Title format: "[Page Title] - [Brand Name]"
* If no page title: "[Brand Name]"
*/
import useDocumentTitle from './use-document-title'
beforeEach(() => {
document.head.innerHTML = ''
})
/**
* Test behavior when system features are still loading
* Title should remain empty to prevent flicker
*/
describe('title should be empty if systemFeatures is pending', () => {
it('document title should be empty if set title', () => {
renderHookWithSystemFeatures(() => useDocumentTitle('test'), { systemFeatures: null })
expect(document.title).toBe('')
})
it('document title should be empty if not set title', () => {
renderHookWithSystemFeatures(() => useDocumentTitle(''), { systemFeatures: null })
expect(document.title).toBe('')
})
})
/**
* Test default Dify branding behavior
* When custom branding is disabled, should use "Dify" as the brand name
*/
describe('use default branding', () => {
it('document title should be test-Dify if set title', () => {
renderHookWithSystemFeatures(() => useDocumentTitle('test'), {
systemFeatures: { branding: { enabled: false } },
})
expect(document.title).toBe('test - Dify')
})
it('document title should be Dify if not set title', () => {
renderHookWithSystemFeatures(() => useDocumentTitle(''), {
systemFeatures: { branding: { enabled: false } },
})
expect(document.title).toBe('Dify')
})
})
/**
* Test custom branding behavior
* When custom branding is enabled, should use the configured application_title
*/
describe('use specific branding', () => {
it('document title should be test-Test if set title', () => {
renderHookWithSystemFeatures(() => useDocumentTitle('test'), {
systemFeatures: { branding: { enabled: true, application_title: 'Test' } },
})
expect(document.title).toBe('test - Test')
})
it('document title should be Test if not set title', () => {
renderHookWithSystemFeatures(() => useDocumentTitle(''), {
systemFeatures: { branding: { enabled: true, application_title: 'Test' } },
})
expect(document.title).toBe('Test')
})
})
describe('favicon management', () => {
it('should keep server-rendered icon links when custom branding is applied', () => {
document.head.innerHTML = `
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="icon" href="/icon-192x192.png">
`
const serverAppleIcon = document.head.querySelector<HTMLLinkElement>('link[rel="apple-touch-icon"]')!
const serverIcon = document.head.querySelector<HTMLLinkElement>('link[rel="icon"]')!
renderHookWithSystemFeatures(() => useDocumentTitle('test'), {
systemFeatures: {
branding: {
enabled: true,
application_title: 'Test',
favicon: '/custom-favicon.ico',
},
},
})
expect(document.head).toContainElement(serverAppleIcon)
expect(document.head).toContainElement(serverIcon)
expect(document.head.querySelector('link[data-dify-runtime-favicon="document"]')).toHaveAttribute('href', '/custom-favicon.ico')
expect(document.head.querySelector('link[data-dify-runtime-apple-touch-icon="document"]')).toHaveAttribute('href', '/custom-favicon.ico')
})
})