theme.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { setActivePinia, createPinia } from 'pinia'
  2. import { beforeEach, describe, it, expect, vi } from 'vitest'
  3. import { useThemeStore } from '../../stores/theme'
  4. describe('theme store', () => {
  5. let store
  6. beforeEach(() => {
  7. setActivePinia(createPinia())
  8. store = useThemeStore()
  9. })
  10. // ── init() ──────────────────────────────────────────────────────
  11. describe('init()', () => {
  12. it('loads saved theme from storage', () => {
  13. uni.__mockStorage.theme = 'light'
  14. store.init()
  15. expect(store.theme).toBe('light')
  16. })
  17. it('defaults to dark when no saved theme and no system preference', () => {
  18. // mockStorage is already empty (beforeEach cleanup)
  19. // getSystemInfoSync returns { theme: 'dark' } so "sysInfo.theme === 'light'" is false
  20. store.init()
  21. expect(store.theme).toBe('dark')
  22. })
  23. it('falls back to dark when getSystemInfoSync throws', () => {
  24. uni.getSystemInfoSync.mockImplementation(() => { throw new Error('no sys') })
  25. store.init()
  26. expect(store.theme).toBe('dark')
  27. })
  28. it('follows system light preference when no saved theme', () => {
  29. uni.getSystemInfoSync.mockReturnValue({ theme: 'light' })
  30. store.init()
  31. expect(store.theme).toBe('light')
  32. })
  33. it('calls setTabBarStyle with correct colors on init', () => {
  34. uni.__mockStorage.theme = 'light'
  35. store.init()
  36. expect(uni.setTabBarStyle).toHaveBeenCalledWith({
  37. color: '#4A5568',
  38. selectedColor: '#C9A84C',
  39. backgroundColor: '#FFFFFF',
  40. borderStyle: 'black',
  41. })
  42. })
  43. })
  44. // ── toggle() ────────────────────────────────────────────────────
  45. describe('toggle()', () => {
  46. it('switches from dark to light', () => {
  47. store.theme = 'dark'
  48. store.toggle()
  49. expect(store.theme).toBe('light')
  50. })
  51. it('switches from light to dark', () => {
  52. store.theme = 'light'
  53. store.toggle()
  54. expect(store.theme).toBe('dark')
  55. })
  56. it('persists toggled theme to storage', () => {
  57. store.theme = 'dark'
  58. store.toggle()
  59. expect(uni.setStorageSync).toHaveBeenCalledWith('theme', 'light')
  60. })
  61. it('updates tab bar on toggle', () => {
  62. store.theme = 'dark'
  63. store.toggle()
  64. expect(uni.setTabBarStyle).toHaveBeenCalled()
  65. })
  66. })
  67. // ── setTheme() ──────────────────────────────────────────────────
  68. describe('setTheme()', () => {
  69. it('sets theme to light and persists', () => {
  70. store.setTheme('light')
  71. expect(store.theme).toBe('light')
  72. expect(uni.setStorageSync).toHaveBeenCalledWith('theme', 'light')
  73. expect(uni.setTabBarStyle).toHaveBeenCalledWith({
  74. color: '#4A5568',
  75. selectedColor: '#C9A84C',
  76. backgroundColor: '#FFFFFF',
  77. borderStyle: 'black',
  78. })
  79. })
  80. it('sets theme to dark and persists', () => {
  81. store.theme = 'light'
  82. store.setTheme('dark')
  83. expect(store.theme).toBe('dark')
  84. expect(uni.setStorageSync).toHaveBeenCalledWith('theme', 'dark')
  85. expect(uni.setTabBarStyle).toHaveBeenCalledWith({
  86. color: '#A0AEC0',
  87. selectedColor: '#C9A84C',
  88. backgroundColor: '#1E3A5F',
  89. borderStyle: 'white',
  90. })
  91. })
  92. it('ignores invalid theme values', () => {
  93. store.setTheme('invalid')
  94. expect(store.theme).toBe('dark') // default
  95. expect(uni.setStorageSync).not.toHaveBeenCalled()
  96. })
  97. it('ignores empty string', () => {
  98. store.setTheme('')
  99. expect(store.theme).toBe('dark')
  100. expect(uni.setStorageSync).not.toHaveBeenCalled()
  101. })
  102. })
  103. })