Test Failed
Push — master ( 207748...5f3e3e )
by Jesús
03:19
created

test/integration/services/theme.test.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 114
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 87
mnd 1
bc 1
fnc 0
dl 0
loc 114
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { describe, it, expect, beforeEach, vi } from 'vitest';
2
import { initTheme, toggleTheme } from '../../../src/services/theme';
3
4
const mockLocalStorage = {
5
  getItem: vi.fn(),
6
  setItem: vi.fn(),
7
  removeItem: vi.fn(),
8
  clear: vi.fn(),
9
};
10
11
Object.defineProperty(window, 'localStorage', {
12
  value: mockLocalStorage,
13
});
14
15
describe('Theme Service', () => {
16
  beforeEach(() => {
17
    vi.clearAllMocks();
18
    document.documentElement.removeAttribute('data-theme');
19
  });
20
21
  describe('initTheme', () => {
22
    it('should set OS theme as default when no saved theme', () => {
23
      mockLocalStorage.getItem.mockReturnValue(null);
24
      
25
      // Mock matchMedia to return light theme
26
      Object.defineProperty(window, 'matchMedia', {
27
        writable: true,
28
        value: vi.fn().mockImplementation(query => ({
29
          matches: query === '(prefers-color-scheme: dark)' ? false : false,
30
          media: query,
31
          onchange: null,
32
          addListener: vi.fn(),
33
          removeListener: vi.fn(),
34
          addEventListener: vi.fn(),
35
          removeEventListener: vi.fn(),
36
          dispatchEvent: vi.fn(),
37
        })),
38
      });
39
40
      initTheme();
41
42
      expect(document.documentElement.getAttribute('data-theme')).toBe('light');
43
    });
44
45
    it('should set saved theme from localStorage', () => {
46
      mockLocalStorage.getItem.mockReturnValue('light');
47
48
      initTheme();
49
50
      expect(document.documentElement.getAttribute('data-theme')).toBe('light');
51
    });
52
53
    it('should set dark theme when saved theme is dark', () => {
54
      mockLocalStorage.getItem.mockReturnValue('dark');
55
56
      initTheme();
57
58
      expect(document.documentElement.getAttribute('data-theme')).toBe('dark');
59
    });
60
61
    it('should set dark theme when OS prefers dark', () => {
62
      mockLocalStorage.getItem.mockReturnValue(null);
63
      
64
      // Mock matchMedia to return dark theme
65
      Object.defineProperty(window, 'matchMedia', {
66
        writable: true,
67
        value: vi.fn().mockImplementation(query => ({
68
          matches: query === '(prefers-color-scheme: dark)',
69
          media: query,
70
          onchange: null,
71
          addListener: vi.fn(),
72
          removeListener: vi.fn(),
73
          addEventListener: vi.fn(),
74
          removeEventListener: vi.fn(),
75
          dispatchEvent: vi.fn(),
76
        })),
77
      });
78
79
      initTheme();
80
81
      expect(document.documentElement.getAttribute('data-theme')).toBe('dark');
82
    });
83
  });
84
85
  describe('toggleTheme', () => {
86
    it('should toggle from dark to light', () => {
87
      document.documentElement.setAttribute('data-theme', 'dark');
88
89
      toggleTheme();
90
91
      expect(document.documentElement.getAttribute('data-theme')).toBe('light');
92
      expect(mockLocalStorage.setItem).toHaveBeenCalledWith('theme', 'light');
93
    });
94
95
    it('should toggle from light to dark', () => {
96
      document.documentElement.setAttribute('data-theme', 'light');
97
98
      toggleTheme();
99
100
      expect(document.documentElement.getAttribute('data-theme')).toBe('dark');
101
      expect(mockLocalStorage.setItem).toHaveBeenCalledWith('theme', 'dark');
102
    });
103
104
    it('should handle undefined theme by setting to light', () => {
105
      document.documentElement.removeAttribute('data-theme');
106
107
      toggleTheme();
108
109
      expect(document.documentElement.getAttribute('data-theme')).toBe('dark');
110
      expect(mockLocalStorage.setItem).toHaveBeenCalledWith('theme', 'dark');
111
    });
112
  });
113
});
114