|
| 1 | +import { cleanup, render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { Card } from '../card'; |
| 5 | +import { Field } from '../field'; |
| 6 | +import { Section } from '../section'; |
| 7 | + |
| 8 | +afterEach(() => cleanup()); |
| 9 | + |
| 10 | +const cases = [ |
| 11 | + { |
| 12 | + name: 'section error', |
| 13 | + property: 'height', |
| 14 | + renderMessage: (message: string) => <Section.Error>{message}</Section.Error>, |
| 15 | + }, |
| 16 | + { |
| 17 | + name: 'card banner', |
| 18 | + property: 'grid-template-rows', |
| 19 | + renderMessage: (message: string) => <Card.Banner role='alert'>{message}</Card.Banner>, |
| 20 | + }, |
| 21 | +]; |
| 22 | + |
| 23 | +describe.each(cases)('$name motion', ({ property, renderMessage }) => { |
| 24 | + it('shows an initial message without animating its layout', async () => { |
| 25 | + render(renderMessage('Something went wrong.')); |
| 26 | + const message = screen.getByRole('alert'); |
| 27 | + |
| 28 | + expect(getComputedStyle(message).transitionProperty).toBe(property); |
| 29 | + expect(getComputedStyle(message).transitionDuration).not.toBe('0s'); |
| 30 | + expect(message.getBoundingClientRect().height).toBeGreaterThan(0); |
| 31 | + expect( |
| 32 | + message |
| 33 | + .getAnimations({ subtree: true }) |
| 34 | + .map(animation => |
| 35 | + animation instanceof CSSTransition ? animation.transitionProperty : animation.constructor.name, |
| 36 | + ), |
| 37 | + ).toEqual([]); |
| 38 | + |
| 39 | + await new Promise<void>(resolve => requestAnimationFrame(() => resolve())); |
| 40 | + await new Promise<void>(resolve => requestAnimationFrame(() => resolve())); |
| 41 | + |
| 42 | + expect(message.getAnimations({ subtree: true })).toHaveLength(0); |
| 43 | + }); |
| 44 | + |
| 45 | + it('animates a message that arrives after mount', async () => { |
| 46 | + const { rerender } = render(renderMessage('')); |
| 47 | + |
| 48 | + await new Promise<void>(resolve => requestAnimationFrame(() => resolve())); |
| 49 | + rerender(renderMessage('Something went wrong.')); |
| 50 | + const message = screen.getByRole('alert'); |
| 51 | + expect(message.getBoundingClientRect().height).toBe(0); |
| 52 | + |
| 53 | + await new Promise<void>(resolve => requestAnimationFrame(() => resolve())); |
| 54 | + await new Promise<void>(resolve => requestAnimationFrame(() => resolve())); |
| 55 | + |
| 56 | + expect( |
| 57 | + message |
| 58 | + .getAnimations() |
| 59 | + .some(animation => animation instanceof CSSTransition && animation.transitionProperty === property), |
| 60 | + ).toBe(true); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +it('animates field feedback opening, resizing, closing, and reopening', async () => { |
| 65 | + const renderMessage = (text: string) => ( |
| 66 | + <div style={{ width: 180 }}> |
| 67 | + <Field.Message> |
| 68 | + <Field.Error>{text}</Field.Error> |
| 69 | + </Field.Message> |
| 70 | + </div> |
| 71 | + ); |
| 72 | + const { rerender } = render(renderMessage('')); |
| 73 | + const message = screen.getByRole('status'); |
| 74 | + expect(message.getBoundingClientRect().height).toBe(0); |
| 75 | + await new Promise<void>(resolve => requestAnimationFrame(() => resolve())); |
| 76 | + |
| 77 | + rerender(renderMessage('Invalid email.')); |
| 78 | + await waitFor(() => |
| 79 | + expect( |
| 80 | + message |
| 81 | + .getAnimations() |
| 82 | + .some(animation => animation instanceof CSSTransition && animation.transitionProperty === 'height'), |
| 83 | + ).toBe(true), |
| 84 | + ); |
| 85 | + await waitFor(() => expect(message.getAnimations({ subtree: true })).toHaveLength(0)); |
| 86 | + const shortHeight = message.getBoundingClientRect().height; |
| 87 | + expect(shortHeight).toBeGreaterThan(0); |
| 88 | + |
| 89 | + rerender(renderMessage('This email address is invalid. Enter a complete email address before continuing.')); |
| 90 | + await waitFor(() => |
| 91 | + expect( |
| 92 | + message |
| 93 | + .getAnimations() |
| 94 | + .some(animation => animation instanceof CSSTransition && animation.transitionProperty === 'height'), |
| 95 | + ).toBe(true), |
| 96 | + ); |
| 97 | + await waitFor(() => expect(message.getAnimations({ subtree: true })).toHaveLength(0)); |
| 98 | + expect(message.getBoundingClientRect().height).toBeGreaterThan(shortHeight); |
| 99 | + |
| 100 | + rerender(renderMessage('')); |
| 101 | + await waitFor(() => |
| 102 | + expect( |
| 103 | + message |
| 104 | + .getAnimations() |
| 105 | + .some(animation => animation instanceof CSSTransition && animation.transitionProperty === 'height'), |
| 106 | + ).toBe(true), |
| 107 | + ); |
| 108 | + await waitFor(() => expect(message.getBoundingClientRect().height).toBe(0)); |
| 109 | + await waitFor(() => expect(screen.queryByText(/This email address is invalid/)).toBeNull()); |
| 110 | + |
| 111 | + rerender(renderMessage('Invalid email.')); |
| 112 | + await waitFor(() => |
| 113 | + expect( |
| 114 | + message |
| 115 | + .getAnimations() |
| 116 | + .some(animation => animation instanceof CSSTransition && animation.transitionProperty === 'height'), |
| 117 | + ).toBe(true), |
| 118 | + ); |
| 119 | + await waitFor(() => expect(message.getAnimations({ subtree: true })).toHaveLength(0)); |
| 120 | + expect(message.getBoundingClientRect().height).toBe(shortHeight); |
| 121 | +}); |
0 commit comments