|
| 1 | +// @vitest-environment jsdom |
| 2 | +import React, { act } from 'react'; |
| 3 | +import { createRoot, type Root } from 'react-dom/client'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 5 | +import { DoomDecorations } from './doom-decorations'; |
| 6 | + |
| 7 | +let container: HTMLDivElement; |
| 8 | +let root: Root; |
| 9 | + |
| 10 | +beforeEach(() => { |
| 11 | + document.documentElement.className = 'dark'; |
| 12 | + container = document.createElement('div'); |
| 13 | + document.body.append(container); |
| 14 | + root = createRoot(container); |
| 15 | +}); |
| 16 | + |
| 17 | +afterEach(() => { |
| 18 | + act(() => root.unmount()); |
| 19 | + container.remove(); |
| 20 | + document.documentElement.className = ''; |
| 21 | +}); |
| 22 | + |
| 23 | +async function setTheme(theme: string) { |
| 24 | + await act(async () => { |
| 25 | + document.documentElement.className = theme; |
| 26 | + await Promise.resolve(); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +describe('DoomDecorations', () => { |
| 31 | + it('does not mount request-bearing images in the other themes', async () => { |
| 32 | + act(() => root.render(<DoomDecorations />)); |
| 33 | + for (const theme of ['light', 'dark', 'minecraft', 'csgo', 'gta', 'kart']) { |
| 34 | + await setTheme(theme); |
| 35 | + expect(container.querySelector('img')).toBeNull(); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it('loads on activation, stays decorative, and unmounts on exit', async () => { |
| 40 | + act(() => root.render(<DoomDecorations />)); |
| 41 | + await setTheme('doom'); |
| 42 | + expect(container.querySelector('[data-testid="doom-scene"]')?.getAttribute('aria-hidden')).toBe( |
| 43 | + 'true', |
| 44 | + ); |
| 45 | + expect(container.querySelector('img')?.getAttribute('src')).toBe( |
| 46 | + '/decorative/doom/doom-hell.webp', |
| 47 | + ); |
| 48 | + expect(container.querySelector('source')?.getAttribute('media')).toBe('(max-width: 640px)'); |
| 49 | + expect(container.querySelectorAll('button, a, audio, video, iframe, canvas')).toHaveLength(0); |
| 50 | + await setTheme('light'); |
| 51 | + expect(container.childElementCount).toBe(0); |
| 52 | + await setTheme('doom'); |
| 53 | + expect(container.querySelectorAll('img')).toHaveLength(1); |
| 54 | + }); |
| 55 | + |
| 56 | + it('honors a saved Doom theme on mount', () => { |
| 57 | + document.documentElement.className = 'doom'; |
| 58 | + act(() => root.render(<DoomDecorations />)); |
| 59 | + expect(container.querySelector('img')).not.toBeNull(); |
| 60 | + }); |
| 61 | +}); |
0 commit comments