|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { createRef, type RefObject } from 'react'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { useAnimationsFinished } from './use-animations-finished'; |
| 6 | + |
| 7 | +function createMockElement( |
| 8 | + animations: Array<{ finished: Promise<void> }> = [], |
| 9 | + attributes: Record<string, string> = {}, |
| 10 | +): HTMLElement { |
| 11 | + const el = document.createElement('div'); |
| 12 | + Object.entries(attributes).forEach(([k, v]) => el.setAttribute(k, v)); |
| 13 | + el.getAnimations = vi.fn(() => animations as unknown as Animation[]); |
| 14 | + return el; |
| 15 | +} |
| 16 | + |
| 17 | +describe('useAnimationsFinished', () => { |
| 18 | + it('fires callback immediately when ref.current is null', () => { |
| 19 | + const ref = createRef<HTMLElement>() as RefObject<HTMLElement | null>; |
| 20 | + const { result } = renderHook(() => useAnimationsFinished(ref, false)); |
| 21 | + |
| 22 | + const callback = vi.fn(); |
| 23 | + act(() => result.current(callback)); |
| 24 | + expect(callback).toHaveBeenCalledTimes(1); |
| 25 | + }); |
| 26 | + |
| 27 | + it('fires callback immediately when getAnimations is not supported', () => { |
| 28 | + const ref = { current: document.createElement('div') } as RefObject<HTMLElement | null>; |
| 29 | + // Don't add getAnimations |
| 30 | + delete (ref.current as unknown as Record<string, unknown>).getAnimations; |
| 31 | + |
| 32 | + const { result } = renderHook(() => useAnimationsFinished(ref, false)); |
| 33 | + |
| 34 | + const callback = vi.fn(); |
| 35 | + act(() => result.current(callback)); |
| 36 | + expect(callback).toHaveBeenCalledTimes(1); |
| 37 | + }); |
| 38 | + |
| 39 | + it('fires callback immediately when no animations are running', () => { |
| 40 | + const el = createMockElement([]); |
| 41 | + const ref = { current: el } as RefObject<HTMLElement | null>; |
| 42 | + |
| 43 | + const { result } = renderHook(() => useAnimationsFinished(ref, false)); |
| 44 | + |
| 45 | + const callback = vi.fn(); |
| 46 | + act(() => result.current(callback)); |
| 47 | + expect(callback).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it('waits for all animations to finish before firing callback', async () => { |
| 51 | + let resolveAnim!: () => void; |
| 52 | + const animPromise = new Promise<void>(r => { |
| 53 | + resolveAnim = r; |
| 54 | + }); |
| 55 | + const el = createMockElement([{ finished: animPromise }]); |
| 56 | + const ref = { current: el } as RefObject<HTMLElement | null>; |
| 57 | + |
| 58 | + const { result } = renderHook(() => useAnimationsFinished(ref, false)); |
| 59 | + |
| 60 | + const callback = vi.fn(); |
| 61 | + act(() => result.current(callback)); |
| 62 | + |
| 63 | + expect(callback).not.toHaveBeenCalled(); |
| 64 | + |
| 65 | + // After animations finish, change getAnimations to return empty |
| 66 | + el.getAnimations = vi.fn(() => [] as unknown as Animation[]); |
| 67 | + await act(() => resolveAnim()); |
| 68 | + |
| 69 | + expect(callback).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it('aborts previous pending wait when called again', async () => { |
| 73 | + let resolveFirst!: () => void; |
| 74 | + const firstAnim = new Promise<void>(r => { |
| 75 | + resolveFirst = r; |
| 76 | + }); |
| 77 | + const el = createMockElement([{ finished: firstAnim }]); |
| 78 | + const ref = { current: el } as RefObject<HTMLElement | null>; |
| 79 | + |
| 80 | + const { result } = renderHook(() => useAnimationsFinished(ref, false)); |
| 81 | + |
| 82 | + const firstCallback = vi.fn(); |
| 83 | + act(() => result.current(firstCallback)); |
| 84 | + |
| 85 | + // Call again — should abort the first |
| 86 | + const secondCallback = vi.fn(); |
| 87 | + el.getAnimations = vi.fn(() => [] as unknown as Animation[]); |
| 88 | + act(() => result.current(secondCallback)); |
| 89 | + |
| 90 | + expect(secondCallback).toHaveBeenCalledTimes(1); |
| 91 | + |
| 92 | + // Resolve first animation — its callback should NOT fire |
| 93 | + await act(() => resolveFirst()); |
| 94 | + expect(firstCallback).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('re-checks animations when one is cancelled', async () => { |
| 98 | + let rejectAnim!: () => void; |
| 99 | + const cancelledAnim = new Promise<void>((_, reject) => { |
| 100 | + rejectAnim = reject; |
| 101 | + }); |
| 102 | + const el = createMockElement([{ finished: cancelledAnim }]); |
| 103 | + const ref = { current: el } as RefObject<HTMLElement | null>; |
| 104 | + |
| 105 | + const { result } = renderHook(() => useAnimationsFinished(ref, false)); |
| 106 | + |
| 107 | + const callback = vi.fn(); |
| 108 | + act(() => result.current(callback)); |
| 109 | + |
| 110 | + expect(callback).not.toHaveBeenCalled(); |
| 111 | + |
| 112 | + // Cancel the animation — hook should re-check and find no new animations |
| 113 | + el.getAnimations = vi.fn(() => [] as unknown as Animation[]); |
| 114 | + await act(async () => { |
| 115 | + rejectAnim(); |
| 116 | + // Let microtask queue flush |
| 117 | + await new Promise(r => setTimeout(r, 0)); |
| 118 | + }); |
| 119 | + |
| 120 | + expect(callback).toHaveBeenCalledTimes(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it('waits for starting-style attribute removal when open=true', async () => { |
| 124 | + const el = createMockElement([], { 'data-cl-starting-style': '' }); |
| 125 | + const ref = { current: el } as RefObject<HTMLElement | null>; |
| 126 | + |
| 127 | + const { result } = renderHook(() => useAnimationsFinished(ref, true)); |
| 128 | + |
| 129 | + const callback = vi.fn(); |
| 130 | + act(() => result.current(callback)); |
| 131 | + |
| 132 | + // Should not fire yet — waiting for attribute removal |
| 133 | + expect(callback).not.toHaveBeenCalled(); |
| 134 | + |
| 135 | + // Remove the attribute — MutationObserver should fire |
| 136 | + await act(async () => { |
| 137 | + el.removeAttribute('data-cl-starting-style'); |
| 138 | + // MutationObserver is async; wait a tick |
| 139 | + await new Promise(r => setTimeout(r, 0)); |
| 140 | + }); |
| 141 | + |
| 142 | + expect(callback).toHaveBeenCalledTimes(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('cleans up on unmount', () => { |
| 146 | + let resolveAnim!: () => void; |
| 147 | + const animPromise = new Promise<void>(r => { |
| 148 | + resolveAnim = r; |
| 149 | + }); |
| 150 | + const el = createMockElement([{ finished: animPromise }]); |
| 151 | + const ref = { current: el } as RefObject<HTMLElement | null>; |
| 152 | + |
| 153 | + const { result, unmount } = renderHook(() => useAnimationsFinished(ref, false)); |
| 154 | + |
| 155 | + const callback = vi.fn(); |
| 156 | + act(() => result.current(callback)); |
| 157 | + |
| 158 | + // Unmount should abort |
| 159 | + unmount(); |
| 160 | + |
| 161 | + // Resolve animation — callback should not fire because abort was called |
| 162 | + resolveAnim(); |
| 163 | + |
| 164 | + expect(callback).not.toHaveBeenCalled(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments