|
| 1 | +import * as stylex from '@stylexjs/stylex'; |
| 2 | +import { act, render, screen } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { Reveal } from './reveal'; |
| 7 | + |
| 8 | +const atoms = stylex.create({ |
| 9 | + spaced: { marginTop: '8px' }, |
| 10 | +}); |
| 11 | + |
| 12 | +const restores: Array<() => void> = []; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + restores.splice(0).forEach(restore => restore()); |
| 16 | +}); |
| 17 | + |
| 18 | +function holdAnimations() { |
| 19 | + let finish = () => undefined as void; |
| 20 | + const finished = new Promise<void>(resolve => { |
| 21 | + finish = resolve; |
| 22 | + }); |
| 23 | + const getAnimations = vi.fn(() => [{ finished }]); |
| 24 | + const original = Object.getOwnPropertyDescriptor(Element.prototype, 'getAnimations'); |
| 25 | + Object.defineProperty(Element.prototype, 'getAnimations', { configurable: true, value: getAnimations }); |
| 26 | + restores.push(() => { |
| 27 | + if (original) { |
| 28 | + Object.defineProperty(Element.prototype, 'getAnimations', original); |
| 29 | + } else { |
| 30 | + Reflect.deleteProperty(Element.prototype, 'getAnimations'); |
| 31 | + } |
| 32 | + }); |
| 33 | + return async () => { |
| 34 | + getAnimations.mockReturnValue([]); |
| 35 | + await act(async () => { |
| 36 | + finish(); |
| 37 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 38 | + }); |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +async function nextFrame() { |
| 43 | + await act(async () => { |
| 44 | + await new Promise(resolve => requestAnimationFrame(resolve)); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +describe('Mosaic Reveal', () => { |
| 49 | + it('renders no children while closed on mount', () => { |
| 50 | + render( |
| 51 | + <Reveal |
| 52 | + open={false} |
| 53 | + data-testid='reveal' |
| 54 | + > |
| 55 | + Details |
| 56 | + </Reveal>, |
| 57 | + ); |
| 58 | + expect(screen.getByTestId('reveal')).toBeInTheDocument(); |
| 59 | + expect(screen.queryByText('Details')).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('renders its children without an enter animation when mounted open', () => { |
| 63 | + render( |
| 64 | + <Reveal |
| 65 | + open |
| 66 | + data-testid='reveal' |
| 67 | + > |
| 68 | + Details |
| 69 | + </Reveal>, |
| 70 | + ); |
| 71 | + const root = screen.getByTestId('reveal'); |
| 72 | + expect(screen.getByText('Details')).toBeInTheDocument(); |
| 73 | + expect(root).toHaveAttribute('data-open'); |
| 74 | + expect(root).not.toHaveAttribute('data-starting-style'); |
| 75 | + expect(root.style.transition).toBe(''); |
| 76 | + }); |
| 77 | + |
| 78 | + it('enters from the starting style when opened after mount', () => { |
| 79 | + const { rerender } = render( |
| 80 | + <Reveal |
| 81 | + open={false} |
| 82 | + data-testid='reveal' |
| 83 | + > |
| 84 | + Details |
| 85 | + </Reveal>, |
| 86 | + ); |
| 87 | + rerender( |
| 88 | + <Reveal |
| 89 | + open |
| 90 | + data-testid='reveal' |
| 91 | + > |
| 92 | + Details |
| 93 | + </Reveal>, |
| 94 | + ); |
| 95 | + const root = screen.getByTestId('reveal'); |
| 96 | + expect(root).toHaveAttribute('data-open'); |
| 97 | + expect(root).toHaveAttribute('data-starting-style'); |
| 98 | + expect(screen.getByText('Details')).toBeInTheDocument(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('holds the last children, inert, through the exit, then unmounts them', async () => { |
| 102 | + const finish = holdAnimations(); |
| 103 | + const { rerender } = render( |
| 104 | + <Reveal |
| 105 | + open |
| 106 | + data-testid='reveal' |
| 107 | + > |
| 108 | + <span>Old</span> |
| 109 | + </Reveal>, |
| 110 | + ); |
| 111 | + await nextFrame(); |
| 112 | + const root = screen.getByTestId('reveal'); |
| 113 | + expect(screen.getByText('Old').closest('[inert]')).toBeNull(); |
| 114 | + |
| 115 | + rerender( |
| 116 | + <Reveal |
| 117 | + open={false} |
| 118 | + data-testid='reveal' |
| 119 | + > |
| 120 | + <span>New</span> |
| 121 | + </Reveal>, |
| 122 | + ); |
| 123 | + expect(root).toHaveAttribute('data-ending-style'); |
| 124 | + expect(root).not.toHaveAttribute('data-open'); |
| 125 | + expect(screen.getByText('Old').closest('[inert]')).not.toBeNull(); |
| 126 | + expect(screen.queryByText('New')).toBeNull(); |
| 127 | + |
| 128 | + await finish(); |
| 129 | + expect(screen.queryByText('Old')).toBeNull(); |
| 130 | + expect(screen.queryByText('New')).toBeNull(); |
| 131 | + expect(root).toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('carries the slot class, merges xstyle and forwards the ref', () => { |
| 135 | + const ref = React.createRef<HTMLDivElement>(); |
| 136 | + render( |
| 137 | + <Reveal |
| 138 | + ref={ref} |
| 139 | + open |
| 140 | + data-testid='reveal' |
| 141 | + xstyle={atoms.spaced} |
| 142 | + > |
| 143 | + Details |
| 144 | + </Reveal>, |
| 145 | + ); |
| 146 | + const root = screen.getByTestId('reveal'); |
| 147 | + expect(ref.current).toBe(root); |
| 148 | + expect(root).toHaveClass('cl-reveal', stylex.props(atoms.spaced).className ?? ''); |
| 149 | + }); |
| 150 | +}); |
0 commit comments