theme-presenter.spec.ts 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // @vitest-environment jsdom
  2. // ThemePresenter behavior account: root color-scheme and the palette attribute
  3. // follow active.colorScheme only, token variables replace the previous apply's
  4. // set, theme-color metadata follows the rendered body background, and dispose
  5. // retracts everything the presenter wrote.
  6. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  7. import type { ThemeSnapshot } from '@deepseek-ai/dsh-client-ui-theme/client'
  8. import { DARK_ATTRIBUTE, ThemePresenter } from '@deepseek-ai/dsh-client-ui-layout/src/client/theme-presenter.ts'
  9. const LIGHT_THEME_COLOR = 'rgb(255, 255, 255)'
  10. const DARK_THEME_COLOR = 'rgb(21, 21, 23)'
  11. function snapshot(colorScheme: 'light' | 'dark', tokens: Record<string, string> = {}): ThemeSnapshot {
  12. // The presenter must key off colorScheme, not the id — keep them distinct.
  13. const active = { id: `${colorScheme}-test`, colorScheme, tokens }
  14. return { preference: colorScheme, active, themes: [active], revision: 1 }
  15. }
  16. function clearThemePresentation(): void {
  17. document.head.querySelectorAll('meta[name="theme-color"], style[data-theme-presenter-test]').forEach((node) => { node.remove() })
  18. }
  19. function themeColorMeta(): HTMLMetaElement | null {
  20. return document.head.querySelector<HTMLMetaElement>('meta[name="theme-color"]')
  21. }
  22. beforeEach(() => {
  23. clearThemePresentation()
  24. document.documentElement.style.removeProperty('color-scheme')
  25. document.body.removeAttribute(DARK_ATTRIBUTE)
  26. document.body.removeAttribute('style')
  27. const style = document.createElement('style')
  28. style.dataset.themePresenterTest = ''
  29. style.textContent = `
  30. body { background-color: ${LIGHT_THEME_COLOR}; }
  31. body[${DARK_ATTRIBUTE}] { background-color: ${DARK_THEME_COLOR}; }
  32. `
  33. document.head.append(style)
  34. })
  35. afterEach(clearThemePresentation)
  36. describe('ThemePresenter', () => {
  37. it('light scheme sets root color-scheme and leaves the dark attribute absent', () => {
  38. const presenter = new ThemePresenter()
  39. presenter.apply(snapshot('light'))
  40. expect(document.documentElement.style.colorScheme).toBe('light')
  41. expect(document.body.hasAttribute(DARK_ATTRIBUTE)).toBe(false)
  42. expect(themeColorMeta()?.content).toBe(LIGHT_THEME_COLOR)
  43. })
  44. it('dark scheme sets root color-scheme, the attribute, and metadata; switching to light updates one node', () => {
  45. const presenter = new ThemePresenter()
  46. presenter.apply(snapshot('dark'))
  47. const meta = themeColorMeta()
  48. expect(document.documentElement.style.colorScheme).toBe('dark')
  49. expect(document.body.hasAttribute(DARK_ATTRIBUTE)).toBe(true)
  50. expect(meta?.content).toBe(DARK_THEME_COLOR)
  51. presenter.apply(snapshot('light'))
  52. expect(document.documentElement.style.colorScheme).toBe('light')
  53. expect(document.body.hasAttribute(DARK_ATTRIBUTE)).toBe(false)
  54. expect(themeColorMeta()).toBe(meta)
  55. expect(meta?.content).toBe(LIGHT_THEME_COLOR)
  56. expect(document.head.querySelectorAll('meta[name="theme-color"]')).toHaveLength(1)
  57. })
  58. it('applies tokens as inline variables and clears the previous set on theme change', () => {
  59. const presenter = new ThemePresenter()
  60. presenter.apply(snapshot('dark', { '--dsw-alias-bg': '#111', '--dsw-alias-fg': '#eee' }))
  61. expect(document.body.style.getPropertyValue('--dsw-alias-bg')).toBe('#111')
  62. expect(document.body.style.getPropertyValue('--dsw-alias-fg')).toBe('#eee')
  63. presenter.apply(snapshot('light', { '--dsw-alias-bg': '#fff' }))
  64. expect(document.body.style.getPropertyValue('--dsw-alias-bg')).toBe('#fff')
  65. // The old theme's extra variable is gone, not merged.
  66. expect(document.body.style.getPropertyValue('--dsw-alias-fg')).toBe('')
  67. })
  68. it('dispose removes color-scheme, the attribute, and every applied variable, sparing foreign inline styles', () => {
  69. document.body.style.setProperty('--foreign', 'kept')
  70. const presenter = new ThemePresenter()
  71. presenter.apply(snapshot('dark', { '--dsw-alias-bg': '#111' }))
  72. const meta = themeColorMeta()
  73. presenter.dispose()
  74. expect(document.documentElement.style.colorScheme).toBe('')
  75. expect(document.body.hasAttribute(DARK_ATTRIBUTE)).toBe(false)
  76. expect(document.body.style.getPropertyValue('--dsw-alias-bg')).toBe('')
  77. expect(document.body.style.getPropertyValue('--foreign')).toBe('kept')
  78. expect(meta?.isConnected).toBe(false)
  79. })
  80. })