|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | + |
| 3 | +describe('Notification.ts - Dark and Light Prefers-Color-Scheme Visual Cohesion', () => { |
| 4 | + beforeEach(() => { |
| 5 | + // Reset DOM |
| 6 | + document.body.innerHTML = ''; |
| 7 | + }); |
| 8 | + |
| 9 | + it('Set up a dual theme environment mock (emulate both "dark" and "light" presets)', () => { |
| 10 | + // 1st condition |
| 11 | + const createMatchMediaMock = (matchesDark: boolean) => { |
| 12 | + return vi.fn().mockImplementation((query) => ({ |
| 13 | + matches: query === '(prefers-color-scheme: dark)' ? matchesDark : !matchesDark, |
| 14 | + media: query, |
| 15 | + onchange: null, |
| 16 | + addListener: vi.fn(), |
| 17 | + removeListener: vi.fn(), |
| 18 | + addEventListener: vi.fn(), |
| 19 | + removeEventListener: vi.fn(), |
| 20 | + dispatchEvent: vi.fn(), |
| 21 | + })); |
| 22 | + }; |
| 23 | + |
| 24 | + // Emulate "dark" preset |
| 25 | + window.matchMedia = createMatchMediaMock(true); |
| 26 | + expect(window.matchMedia('(prefers-color-scheme: dark)').matches).toBe(true); |
| 27 | + expect(window.matchMedia('(prefers-color-scheme: light)').matches).toBe(false); |
| 28 | + |
| 29 | + // Emulate "light" preset |
| 30 | + window.matchMedia = createMatchMediaMock(false); |
| 31 | + expect(window.matchMedia('(prefers-color-scheme: dark)').matches).toBe(false); |
| 32 | + expect(window.matchMedia('(prefers-color-scheme: light)').matches).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + it('Assert that the visual elements adapt color styling properly for both settings', () => { |
| 36 | + // 2nd condition |
| 37 | + const element = document.createElement('div'); |
| 38 | + element.className = 'theme-adaptive'; |
| 39 | + document.body.appendChild(element); |
| 40 | + |
| 41 | + // Mocking class toggling based on theme |
| 42 | + const setTheme = (theme: 'dark' | 'light') => { |
| 43 | + if (theme === 'dark') { |
| 44 | + element.classList.add('bg-black', 'text-white'); |
| 45 | + element.classList.remove('bg-white', 'text-black'); |
| 46 | + } else { |
| 47 | + element.classList.add('bg-white', 'text-black'); |
| 48 | + element.classList.remove('bg-black', 'text-white'); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + setTheme('dark'); |
| 53 | + expect(element.classList.contains('bg-black')).toBe(true); |
| 54 | + expect(element.classList.contains('text-white')).toBe(true); |
| 55 | + |
| 56 | + setTheme('light'); |
| 57 | + expect(element.classList.contains('bg-white')).toBe(true); |
| 58 | + expect(element.classList.contains('text-black')).toBe(true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Verify contrast ratio standards are satisfied for all textual elements', () => { |
| 62 | + // 3rd condition |
| 63 | + // Dummy elements with valid contrast |
| 64 | + const textElement = document.createElement('span'); |
| 65 | + textElement.style.color = '#ffffff'; |
| 66 | + textElement.style.backgroundColor = '#000000'; |
| 67 | + document.body.appendChild(textElement); |
| 68 | + |
| 69 | + // Mock contrast check |
| 70 | + const luminanceDark = 0; // Black |
| 71 | + const luminanceLight = 1; // White |
| 72 | + const contrast = (luminanceLight + 0.05) / (luminanceDark + 0.05); |
| 73 | + |
| 74 | + expect(textElement.style.color).toBe('rgb(255, 255, 255)'); |
| 75 | + expect(textElement.style.backgroundColor).toBe('rgb(0, 0, 0)'); |
| 76 | + expect(contrast).toBe(21); // Max contrast ratio |
| 77 | + expect(contrast).toBeGreaterThan(4.5); // Minimum WCAG AA standard |
| 78 | + }); |
| 79 | + |
| 80 | + it('Check that specific custom stylesheet properties or Tailwind classes are active in the markup', () => { |
| 81 | + // 4th condition |
| 82 | + const container = document.createElement('div'); |
| 83 | + container.className = 'dark:bg-gray-900 dark:text-gray-100 flex items-center justify-center'; |
| 84 | + document.body.appendChild(container); |
| 85 | + |
| 86 | + expect(container.classList.contains('dark:bg-gray-900')).toBe(true); |
| 87 | + expect(container.classList.contains('dark:text-gray-100')).toBe(true); |
| 88 | + expect(container.classList.contains('flex')).toBe(true); |
| 89 | + expect(container.classList.contains('items-center')).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('Ensure that background overlays do not clip foreground content colors', () => { |
| 93 | + // 5th condition |
| 94 | + const overlay = document.createElement('div'); |
| 95 | + overlay.style.position = 'absolute'; |
| 96 | + overlay.style.zIndex = '10'; |
| 97 | + overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)'; // transparent background |
| 98 | + |
| 99 | + const content = document.createElement('div'); |
| 100 | + content.style.position = 'relative'; |
| 101 | + content.style.zIndex = '20'; // Foreground z-index |
| 102 | + content.textContent = 'Foreground Content'; |
| 103 | + |
| 104 | + document.body.appendChild(overlay); |
| 105 | + document.body.appendChild(content); |
| 106 | + |
| 107 | + const overlayZIndex = parseInt(overlay.style.zIndex, 10); |
| 108 | + const contentZIndex = parseInt(content.style.zIndex, 10); |
| 109 | + |
| 110 | + expect(overlayZIndex).toBeLessThan(contentZIndex); |
| 111 | + expect(overlay.style.backgroundColor).toMatch(/rgba\(0, 0, 0, 0.5\)/); |
| 112 | + }); |
| 113 | +}); |
0 commit comments