|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act } from 'react'; |
| 3 | +import { createRoot, type Root } from 'react-dom/client'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +vi.mock('@/lib/analytics', () => ({ track: vi.fn() })); |
| 7 | + |
| 8 | +import { AutumnLeavesToggle } from './autumn-leaves-toggle'; |
| 9 | +import { |
| 10 | + AUTUMN_LEAVES_OFF_ATTRIBUTE, |
| 11 | + AUTUMN_LEAVES_STORAGE_KEY, |
| 12 | + autumnLeavesPrepaintScript, |
| 13 | +} from '@/lib/autumn-leaves'; |
| 14 | + |
| 15 | +(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 16 | + |
| 17 | +describe('AutumnLeavesToggle', () => { |
| 18 | + let container: HTMLDivElement; |
| 19 | + let root: Root; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + localStorage.clear(); |
| 23 | + document.documentElement.removeAttribute(AUTUMN_LEAVES_OFF_ATTRIBUTE); |
| 24 | + container = document.createElement('div'); |
| 25 | + document.body.append(container); |
| 26 | + root = createRoot(container); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + act(() => root.unmount()); |
| 31 | + container.remove(); |
| 32 | + }); |
| 33 | + |
| 34 | + const button = () => |
| 35 | + container.querySelector<HTMLButtonElement>('[data-testid="autumn-leaves-toggle"]')!; |
| 36 | + |
| 37 | + it('hides the leaves, persists the choice, and turns them back on', () => { |
| 38 | + act(() => root.render(<AutumnLeavesToggle />)); |
| 39 | + expect(button().getAttribute('aria-pressed')).toBe('true'); |
| 40 | + |
| 41 | + act(() => button().click()); |
| 42 | + expect(document.documentElement.hasAttribute(AUTUMN_LEAVES_OFF_ATTRIBUTE)).toBe(true); |
| 43 | + expect(localStorage.getItem(AUTUMN_LEAVES_STORAGE_KEY)).toBe('off'); |
| 44 | + expect(button().getAttribute('aria-pressed')).toBe('false'); |
| 45 | + expect(button().getAttribute('aria-label')).toBe('Show falling leaves'); |
| 46 | + |
| 47 | + act(() => button().click()); |
| 48 | + expect(document.documentElement.hasAttribute(AUTUMN_LEAVES_OFF_ATTRIBUTE)).toBe(false); |
| 49 | + expect(localStorage.getItem(AUTUMN_LEAVES_STORAGE_KEY)).toBeNull(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('keeps every mounted instance in sync', async () => { |
| 53 | + act(() => |
| 54 | + root.render( |
| 55 | + <> |
| 56 | + <AutumnLeavesToggle /> |
| 57 | + <AutumnLeavesToggle /> |
| 58 | + </>, |
| 59 | + ), |
| 60 | + ); |
| 61 | + const [first, second] = container.querySelectorAll<HTMLButtonElement>( |
| 62 | + '[data-testid="autumn-leaves-toggle"]', |
| 63 | + ); |
| 64 | + act(() => first.click()); |
| 65 | + // MutationObserver callbacks run as microtasks. |
| 66 | + await act(async () => { |
| 67 | + await Promise.resolve(); |
| 68 | + }); |
| 69 | + expect(second.getAttribute('aria-pressed')).toBe('false'); |
| 70 | + act(() => second.click()); |
| 71 | + expect(document.documentElement.hasAttribute(AUTUMN_LEAVES_OFF_ATTRIBUTE)).toBe(false); |
| 72 | + await act(async () => { |
| 73 | + await Promise.resolve(); |
| 74 | + }); |
| 75 | + expect(first.getAttribute('aria-pressed')).toBe('true'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('prepaint script keys off the saved opt-out', () => { |
| 79 | + expect(autumnLeavesPrepaintScript).toContain(JSON.stringify(AUTUMN_LEAVES_STORAGE_KEY)); |
| 80 | + expect(autumnLeavesPrepaintScript).toContain(JSON.stringify(AUTUMN_LEAVES_OFF_ATTRIBUTE)); |
| 81 | + }); |
| 82 | + |
| 83 | + it('reads the opt-out the prepaint script applied before hydration', () => { |
| 84 | + document.documentElement.setAttribute(AUTUMN_LEAVES_OFF_ATTRIBUTE, ''); |
| 85 | + |
| 86 | + act(() => root.render(<AutumnLeavesToggle isZh />)); |
| 87 | + expect(button().getAttribute('aria-pressed')).toBe('false'); |
| 88 | + expect(button().getAttribute('aria-label')).toBe('显示飘落的树叶'); |
| 89 | + }); |
| 90 | +}); |
0 commit comments