import * as React from 'react' import { forwardRef } from 'react' import { render } from '@testing-library/react' import createMockRaf, { MockRaf } from 'mock-raf' import { Globals } from '@react-spring/shared' import { SpringValue, Animatable } from '@react-spring/core' import { a } from './index' let mockRaf: MockRaf beforeEach(() => { mockRaf = createMockRaf() Globals.assign({ now: mockRaf.now, requestAnimationFrame: mockRaf.raf, }) }) describe('animated component', () => { it('creates an HTML element from a tag name', () => { const AnimatedH1 = a('h1') const { queryByTitle } = render( Bar ) expect(queryByTitle('Foo')).toBeTruthy() }) it('wraps a component', () => { const Name = forwardRef< HTMLHeadingElement, { name: string; other: string; children: React.ReactNode } >((props, ref) => (

{props.children}

)) const AnimatedName = a(Name) const child = spring('Animated Text') const name = spring('name') const { queryByTitle } = render( {child} ) const el: any = queryByTitle('name')! expect(el).toBeTruthy() expect(el.textContent).toBe('Animated Text') }) it('accepts Animated values in style prop', () => { const opacity = spring(0) const { queryByText } = render( Text ) const div: any = queryByText('Text')! expect(div).toBeTruthy() expect(div.style.opacity).toBe('0') opacity.set(1) mockRaf.step() expect(div.style.opacity).toBe('1') }) it('accepts Animated values in custom style prop', () => { const Name = forwardRef< HTMLHeadingElement, { style: { color: string; opacity?: number }; children: React.ReactNode } >((props, ref) => (

{props.children}

)) const AnimatedName = a(Name) const opacity = spring(0.5) const { queryByText } = render( Text ) const div: any = queryByText('Text')! expect(div).toBeTruthy() expect(div.style.opacity).toBe('0.5') opacity.set(1) mockRaf.step() expect(div.style.opacity).toBe('1') }) it('accepts scrollTop and scrollLeft properties', () => { const scrollTop = spring(0) const { queryByTestId } = render(
) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.scrollTop).toBe(0) expect(wrapper.scrollLeft).toBe(0) scrollTop.set(20) mockRaf.step() expect(wrapper.scrollTop).toBe(20) }) it('accepts the className property', () => { const className = spring('test') const { getByTestId } = render( ) expect(getByTestId('wrapper').className).toBe('test') className.set('new') mockRaf.step() expect(getByTestId('wrapper').className).toBe('new') }) it('accepts x/y/z as style keys equivalent to `translate3d`transform function', () => { const { queryByTestId, rerender } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transform).toBe('translate3d(10px,0,0)') rerender() expect(wrapper.style.transform).toBe('translate3d(0,10%,0)') rerender() expect(wrapper.style.transform).toBe('translate3d(0,0,0.3px)') rerender( ) expect(wrapper.style.transform).toBe('translate3d(10px,10%,0.3px)') }) it('accepts arrays for transform functions used as style keys', () => { const { queryByTestId } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transform).toBe('scale(1,2)') }) it('accepts Animated values or Animated arrays as attributes', () => { const scale = spring(2) const translate = spring([10, 20] as const) const translate3d = [spring(30), spring(40), '50px'] as const const { queryByTestId } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transform).toBe( 'scale(2) translate(10px,20px) translate3d(30px,40px,50px)' ) }) it('updates all values of Animated arrays', () => { const translate3d = spring([10, 20, 30] as const) const { queryByTestId } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transform).toBe('translate3d(10px,20px,30px)') translate3d.set([11, 21, 31] as const) mockRaf.step() expect(wrapper.style.transform).toBe('translate3d(11px,21px,31px)') }) it('sets default units to unit-less values passed as transform functions', () => { const { queryByTestId } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transform).toBe( 'translate3d(10px,0,0) scale(1,2) rotate(30deg) skewX(10deg) translateX(10px)' ) }) it('only applies default units to the fourth value of `rotate3d`', () => { const { queryByTestId } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transform).toBe('rotate3d(1,0,0,30deg)') }) it('applies `transform:none` when identity transform is detected', () => { const z = spring(0) const { queryByTestId } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transform).toBe('none') }) it('preserves transform-style and transform-origin properties', () => { const { queryByTestId } = render( ) const wrapper: any = queryByTestId('wrapper')! expect(wrapper.style.transformOrigin).toBe('bottom center') expect(wrapper.style.transformStyle).toBe('preserve-3d') expect(wrapper.style.transform).toBe('translateX(40px) scale(1,2)') }) }) function spring(value: T): SpringValue> { return new SpringValue(value!) }