2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* 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]"
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
import { defaultSystemFeatures } from '@/types/feature'
|
|
|
|
|
import { act, renderHook } from '@testing-library/react'
|
|
|
|
|
import useDocumentTitle from './use-document-title'
|
|
|
|
|
import { useGlobalPublicStore } from '@/context/global-public-context'
|
|
|
|
|
|
|
|
|
|
jest.mock('@/service/common', () => ({
|
|
|
|
|
getSystemFeatures: jest.fn(() => ({ ...defaultSystemFeatures })),
|
|
|
|
|
}))
|
|
|
|
|
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test behavior when system features are still loading
|
|
|
|
|
* Title should remain empty to prevent flicker
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
describe('title should be empty if systemFeatures is pending', () => {
|
|
|
|
|
act(() => {
|
|
|
|
|
useGlobalPublicStore.setState({
|
|
|
|
|
systemFeatures: { ...defaultSystemFeatures, branding: { ...defaultSystemFeatures.branding, enabled: false } },
|
2025-06-05 10:55:17 +08:00
|
|
|
isGlobalPending: true,
|
2025-05-20 12:07:50 +08:00
|
|
|
})
|
|
|
|
|
})
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test that title stays empty during loading even when a title is provided
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
it('document title should be empty if set title', () => {
|
|
|
|
|
renderHook(() => useDocumentTitle('test'))
|
|
|
|
|
expect(document.title).toBe('')
|
|
|
|
|
})
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test that title stays empty during loading when no title is provided
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
it('document title should be empty if not set title', () => {
|
|
|
|
|
renderHook(() => useDocumentTitle(''))
|
|
|
|
|
expect(document.title).toBe('')
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test default Dify branding behavior
|
|
|
|
|
* When custom branding is disabled, should use "Dify" as the brand name
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
describe('use default branding', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
act(() => {
|
|
|
|
|
useGlobalPublicStore.setState({
|
2025-06-05 10:55:17 +08:00
|
|
|
isGlobalPending: false,
|
2025-05-20 12:07:50 +08:00
|
|
|
systemFeatures: { ...defaultSystemFeatures, branding: { ...defaultSystemFeatures.branding, enabled: false } },
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test title format with page title and default branding
|
|
|
|
|
* Format: "[page] - Dify"
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
it('document title should be test-Dify if set title', () => {
|
|
|
|
|
renderHook(() => useDocumentTitle('test'))
|
|
|
|
|
expect(document.title).toBe('test - Dify')
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test title with only default branding (no page title)
|
|
|
|
|
* Format: "Dify"
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
it('document title should be Dify if not set title', () => {
|
|
|
|
|
renderHook(() => useDocumentTitle(''))
|
|
|
|
|
expect(document.title).toBe('Dify')
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test custom branding behavior
|
|
|
|
|
* When custom branding is enabled, should use the configured application_title
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
describe('use specific branding', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
act(() => {
|
|
|
|
|
useGlobalPublicStore.setState({
|
2025-06-05 10:55:17 +08:00
|
|
|
isGlobalPending: false,
|
2025-05-20 12:07:50 +08:00
|
|
|
systemFeatures: { ...defaultSystemFeatures, branding: { ...defaultSystemFeatures.branding, enabled: true, application_title: 'Test' } },
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test title format with page title and custom branding
|
|
|
|
|
* Format: "[page] - [Custom Brand]"
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
it('document title should be test-Test if set title', () => {
|
|
|
|
|
renderHook(() => useDocumentTitle('test'))
|
|
|
|
|
expect(document.title).toBe('test - Test')
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-12 08:47:06 -05:00
|
|
|
/**
|
|
|
|
|
* Test title with only custom branding (no page title)
|
|
|
|
|
* Format: "[Custom Brand]"
|
|
|
|
|
*/
|
2025-05-20 12:07:50 +08:00
|
|
|
it('document title should be Test if not set title', () => {
|
|
|
|
|
renderHook(() => useDocumentTitle(''))
|
|
|
|
|
expect(document.title).toBe('Test')
|
|
|
|
|
})
|
|
|
|
|
})
|