| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import { setActivePinia, createPinia } from 'pinia'
- import { beforeEach, describe, it, expect, vi } from 'vitest'
- import { useThemeStore } from '../../stores/theme'
- describe('theme store', () => {
- let store
- beforeEach(() => {
- setActivePinia(createPinia())
- store = useThemeStore()
- })
- // ── init() ──────────────────────────────────────────────────────
- describe('init()', () => {
- it('loads saved theme from storage', () => {
- uni.__mockStorage.theme = 'light'
- store.init()
- expect(store.theme).toBe('light')
- })
- it('defaults to dark when no saved theme and no system preference', () => {
- // mockStorage is already empty (beforeEach cleanup)
- // getSystemInfoSync returns { theme: 'dark' } so "sysInfo.theme === 'light'" is false
- store.init()
- expect(store.theme).toBe('dark')
- })
- it('falls back to dark when getSystemInfoSync throws', () => {
- uni.getSystemInfoSync.mockImplementation(() => { throw new Error('no sys') })
- store.init()
- expect(store.theme).toBe('dark')
- })
- it('follows system light preference when no saved theme', () => {
- uni.getSystemInfoSync.mockReturnValue({ theme: 'light' })
- store.init()
- expect(store.theme).toBe('light')
- })
- it('calls setTabBarStyle with correct colors on init', () => {
- uni.__mockStorage.theme = 'light'
- store.init()
- expect(uni.setTabBarStyle).toHaveBeenCalledWith({
- color: '#4A5568',
- selectedColor: '#C9A84C',
- backgroundColor: '#FFFFFF',
- borderStyle: 'black',
- })
- })
- })
- // ── toggle() ────────────────────────────────────────────────────
- describe('toggle()', () => {
- it('switches from dark to light', () => {
- store.theme = 'dark'
- store.toggle()
- expect(store.theme).toBe('light')
- })
- it('switches from light to dark', () => {
- store.theme = 'light'
- store.toggle()
- expect(store.theme).toBe('dark')
- })
- it('persists toggled theme to storage', () => {
- store.theme = 'dark'
- store.toggle()
- expect(uni.setStorageSync).toHaveBeenCalledWith('theme', 'light')
- })
- it('updates tab bar on toggle', () => {
- store.theme = 'dark'
- store.toggle()
- expect(uni.setTabBarStyle).toHaveBeenCalled()
- })
- })
- // ── setTheme() ──────────────────────────────────────────────────
- describe('setTheme()', () => {
- it('sets theme to light and persists', () => {
- store.setTheme('light')
- expect(store.theme).toBe('light')
- expect(uni.setStorageSync).toHaveBeenCalledWith('theme', 'light')
- expect(uni.setTabBarStyle).toHaveBeenCalledWith({
- color: '#4A5568',
- selectedColor: '#C9A84C',
- backgroundColor: '#FFFFFF',
- borderStyle: 'black',
- })
- })
- it('sets theme to dark and persists', () => {
- store.theme = 'light'
- store.setTheme('dark')
- expect(store.theme).toBe('dark')
- expect(uni.setStorageSync).toHaveBeenCalledWith('theme', 'dark')
- expect(uni.setTabBarStyle).toHaveBeenCalledWith({
- color: '#A0AEC0',
- selectedColor: '#C9A84C',
- backgroundColor: '#1E3A5F',
- borderStyle: 'white',
- })
- })
- it('ignores invalid theme values', () => {
- store.setTheme('invalid')
- expect(store.theme).toBe('dark') // default
- expect(uni.setStorageSync).not.toHaveBeenCalled()
- })
- it('ignores empty string', () => {
- store.setTheme('')
- expect(store.theme).toBe('dark')
- expect(uni.setStorageSync).not.toHaveBeenCalled()
- })
- })
- })
|