|
| 1 | +import { StrictMode } from 'react'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +// The global vitest setup mocks @formkit/auto-animate to a no-op stub. Restore |
| 5 | +// the real module for this file (and for Animated.tsx's transitive import) so |
| 6 | +// the production useSafeAutoAnimate wiring actually runs. |
| 7 | +vi.mock('@formkit/auto-animate', async importOriginal => await importOriginal()); |
| 8 | +vi.mock('@formkit/auto-animate/react', async importOriginal => await importOriginal()); |
| 9 | + |
| 10 | +import { bindCreateFixtures } from '@/test/create-fixtures'; |
| 11 | +import { render, screen } from '@/test/utils'; |
| 12 | + |
| 13 | +import { Animated } from '../../elements/Animated'; |
| 14 | + |
| 15 | +const { createFixtures } = bindCreateFixtures('UserProfile'); |
| 16 | + |
| 17 | +/** |
| 18 | + * Exercises the production <Animated> wrapper (which uses useSafeAutoAnimate) |
| 19 | + * under React StrictMode's mount → cleanup → remount cycle. StrictMode |
| 20 | + * double-invokes effects and re-runs ref callbacks; without the |
| 21 | + * destroy-previous-controller guard in useSafeAutoAnimate, a second |
| 22 | + * MutationObserver would linger on the same node and its remain() animation |
| 23 | + * would cancel the entrance (add) animation. We assert the user-facing outcome |
| 24 | + * (add fires, no remain) and that exactly one MutationObserver stays active on |
| 25 | + * the animated element after the cycle. |
| 26 | + */ |
| 27 | + |
| 28 | +function classifyAnimateCalls(calls: any[]) { |
| 29 | + const adds: any[] = []; |
| 30 | + const remains: any[] = []; |
| 31 | + for (const call of calls) { |
| 32 | + const keyframes = call[0]; |
| 33 | + if (!Array.isArray(keyframes)) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + if ( |
| 37 | + keyframes.some( |
| 38 | + (kf: any) => kf.opacity === 0 && typeof kf.transform === 'string' && kf.transform.includes('scale'), |
| 39 | + ) |
| 40 | + ) { |
| 41 | + adds.push(call); |
| 42 | + } |
| 43 | + if (keyframes.some((kf: any) => typeof kf.transform === 'string' && kf.transform.includes('translate'))) { |
| 44 | + remains.push(call); |
| 45 | + } |
| 46 | + } |
| 47 | + return { adds, remains }; |
| 48 | +} |
| 49 | + |
| 50 | +function AnimatedList({ showChild }: { showChild: boolean }) { |
| 51 | + return ( |
| 52 | + <Animated> |
| 53 | + <div>always here</div> |
| 54 | + {showChild ? <div data-testid='new-child'>new child</div> : null} |
| 55 | + </Animated> |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +const flush = () => new Promise(resolve => setTimeout(resolve, 50)); |
| 60 | + |
| 61 | +describe('Animated under React StrictMode', () => { |
| 62 | + let animateSpy: ReturnType<typeof vi.spyOn>; |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + animateSpy = vi |
| 66 | + .spyOn(Element.prototype, 'animate') |
| 67 | + .mockImplementation(() => ({ addEventListener: vi.fn(), cancel: vi.fn(), finished: Promise.resolve() }) as any); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + animateSpy.mockRestore(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('fires the add animation (and no remain) when a child is added', async () => { |
| 75 | + const { wrapper } = await createFixtures(f => { |
| 76 | + f.withUser({ email_addresses: ['test@clerk.com'] }); |
| 77 | + }); |
| 78 | + |
| 79 | + const { rerender } = render( |
| 80 | + <StrictMode> |
| 81 | + <AnimatedList showChild={false} /> |
| 82 | + </StrictMode>, |
| 83 | + { wrapper }, |
| 84 | + ); |
| 85 | + |
| 86 | + await flush(); |
| 87 | + animateSpy.mockClear(); |
| 88 | + |
| 89 | + rerender( |
| 90 | + <StrictMode> |
| 91 | + <AnimatedList showChild /> |
| 92 | + </StrictMode>, |
| 93 | + ); |
| 94 | + |
| 95 | + await flush(); |
| 96 | + |
| 97 | + const { adds, remains } = classifyAnimateCalls(animateSpy.mock.calls); |
| 98 | + expect(adds.length).toBeGreaterThanOrEqual(1); |
| 99 | + // remain() would only fire if a lingering second observer from the |
| 100 | + // StrictMode remount re-processed the mutation — the guard prevents it. |
| 101 | + expect(remains.length).toBe(0); |
| 102 | + }); |
| 103 | + |
| 104 | + it('keeps exactly one active MutationObserver on the element after the StrictMode cycle', async () => { |
| 105 | + const activeChildListObservers = new Set<MutationObserver>(); |
| 106 | + const targets = new WeakMap<MutationObserver, Node>(); |
| 107 | + const origObserve = MutationObserver.prototype.observe; |
| 108 | + const origDisconnect = MutationObserver.prototype.disconnect; |
| 109 | + |
| 110 | + MutationObserver.prototype.observe = function (target: Node, options?: MutationObserverInit) { |
| 111 | + if (options?.childList) { |
| 112 | + activeChildListObservers.add(this); |
| 113 | + targets.set(this, target); |
| 114 | + } |
| 115 | + return origObserve.call(this, target, options); |
| 116 | + }; |
| 117 | + MutationObserver.prototype.disconnect = function () { |
| 118 | + activeChildListObservers.delete(this); |
| 119 | + return origDisconnect.call(this); |
| 120 | + }; |
| 121 | + |
| 122 | + try { |
| 123 | + const { wrapper } = await createFixtures(f => { |
| 124 | + f.withUser({ email_addresses: ['test@clerk.com'] }); |
| 125 | + }); |
| 126 | + |
| 127 | + render( |
| 128 | + <StrictMode> |
| 129 | + <AnimatedList showChild={false} /> |
| 130 | + </StrictMode>, |
| 131 | + { wrapper }, |
| 132 | + ); |
| 133 | + |
| 134 | + await flush(); |
| 135 | + |
| 136 | + const el = screen.getByText('always here').parentElement; |
| 137 | + const activeOnEl = [...activeChildListObservers].filter(mo => targets.get(mo) === el).length; |
| 138 | + expect(activeOnEl).toBe(1); |
| 139 | + } finally { |
| 140 | + MutationObserver.prototype.observe = origObserve; |
| 141 | + MutationObserver.prototype.disconnect = origDisconnect; |
| 142 | + } |
| 143 | + }); |
| 144 | +}); |
0 commit comments