|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import themeSwitcher from './theme-switcher'; |
| 3 | + |
| 4 | +// Mocking localStorage and document.documentElement for the test environment |
| 5 | +const mockLocalStorage = (() => { |
| 6 | + let store = {}; |
| 7 | + return { |
| 8 | + getItem: (key) => store[key] || null, |
| 9 | + setItem: (key, value) => (store[key] = value), |
| 10 | + clear: () => (store = {}), |
| 11 | + }; |
| 12 | +})(); |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + vi.stubGlobal('localStorage', mockLocalStorage); |
| 16 | + document.documentElement.className = ''; // Reset class list on document element |
| 17 | +}); |
| 18 | + |
| 19 | +describe('themeSwitcher', () => { |
| 20 | + it('renders theme buttons correctly', () => { |
| 21 | + const hostComponent = document.createElement('div'); |
| 22 | + themeSwitcher(hostComponent); |
| 23 | + |
| 24 | + // Check that each theme button is rendered |
| 25 | + const buttons = hostComponent.querySelectorAll('button[data-theme]'); |
| 26 | + expect(buttons.length).toBe(4); // green, red, blue, grey |
| 27 | + |
| 28 | + const themes = ['Green', 'Red', 'Blue', 'Grey']; |
| 29 | + themes.forEach((theme, index) => { |
| 30 | + expect(buttons[index].textContent).toBe(theme); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('applies the theme class to document.documentElement on button click', () => { |
| 35 | + const hostComponent = document.createElement('div'); |
| 36 | + themeSwitcher(hostComponent); |
| 37 | + |
| 38 | + // Simulate clicking the 'red' theme button |
| 39 | + const redButton = hostComponent.querySelector('button[data-theme="red"]'); |
| 40 | + redButton.click(); |
| 41 | + |
| 42 | + expect(document.documentElement.classList.contains('red-theme')).toBe(true); |
| 43 | + |
| 44 | + // Ensure other themes are not active |
| 45 | + expect(document.documentElement.classList.contains('green-theme')).toBe(false); |
| 46 | + expect(document.documentElement.classList.contains('blue-theme')).toBe(false); |
| 47 | + expect(document.documentElement.classList.contains('grey-theme')).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('stores the selected theme in localStorage on button click', () => { |
| 51 | + const hostComponent = document.createElement('div'); |
| 52 | + themeSwitcher(hostComponent); |
| 53 | + |
| 54 | + // Click the 'blue' theme button |
| 55 | + const blueButton = hostComponent.querySelector('button[data-theme="blue"]'); |
| 56 | + blueButton.click(); |
| 57 | + |
| 58 | + expect(localStorage.getItem('theme')).toBe('blue'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('loads and applies the saved theme from localStorage on initialization', () => { |
| 62 | + localStorage.setItem('theme', 'grey'); // Simulate a previously selected theme |
| 63 | + |
| 64 | + const hostComponent = document.createElement('div'); |
| 65 | + themeSwitcher(hostComponent); |
| 66 | + |
| 67 | + // Ensure the saved theme is applied on load |
| 68 | + expect(document.documentElement.classList.contains('grey-theme')).toBe(true); |
| 69 | + }); |
| 70 | +}); |
0 commit comments