File size: 7,481 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
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(
<AnimatedH1 title="Foo" style={{ color: 'red' }}>
Bar
</AnimatedH1>
)
expect(queryByTitle('Foo')).toBeTruthy()
})
it('wraps a component', () => {
const Name = forwardRef<
HTMLHeadingElement,
{ name: string; other: string; children: React.ReactNode }
>((props, ref) => (
<h2 title={props.name} ref={ref}>
{props.children}
</h2>
))
const AnimatedName = a(Name)
const child = spring('Animated Text')
const name = spring('name')
const { queryByTitle } = render(
<AnimatedName name={name} other="test">
{child}
</AnimatedName>
)
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(
<a.div style={{ opacity, color: 'red' }}>Text</a.div>
)
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) => (
<h2 ref={ref} style={props.style}>
{props.children}
</h2>
))
const AnimatedName = a(Name)
const opacity = spring(0.5)
const { queryByText } = render(
<AnimatedName
style={{
opacity: opacity,
color: 'red',
}}
>
Text
</AnimatedName>
)
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(
<a.div
scrollTop={scrollTop}
scrollLeft={0}
style={{ height: 100 }}
data-testid="wrapper"
>
<div style={{ height: 200 }} />
</a.div>
)
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(
<a.div className={className} data-testid="wrapper" />
)
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(
<a.div style={{ x: 10 }} data-testid="wrapper" />
)
const wrapper: any = queryByTestId('wrapper')!
expect(wrapper.style.transform).toBe('translate3d(10px,0,0)')
rerender(<a.div style={{ y: '10%' }} data-testid="wrapper" />)
expect(wrapper.style.transform).toBe('translate3d(0,10%,0)')
rerender(<a.div style={{ z: 0.3 }} data-testid="wrapper" />)
expect(wrapper.style.transform).toBe('translate3d(0,0,0.3px)')
rerender(
<a.div style={{ x: 10, y: '10%', z: 0.3 }} data-testid="wrapper" />
)
expect(wrapper.style.transform).toBe('translate3d(10px,10%,0.3px)')
})
it('accepts arrays for transform functions used as style keys', () => {
const { queryByTestId } = render(
<a.div style={{ scale: [1, 2] }} data-testid="wrapper" />
)
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(
<a.div style={{ scale, translate, translate3d }} data-testid="wrapper" />
)
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(
<a.div style={{ translate3d }} data-testid="wrapper" />
)
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(
<a.div
style={{
x: 10,
scale: [1, 2],
rotate: 30,
skewX: 10,
transform: 'translateX(10px)',
}}
data-testid="wrapper"
/>
)
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(
<a.div style={{ rotate3d: [1, 0, 0, 30] }} data-testid="wrapper" />
)
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(
<a.div
style={{
x: 0,
y: '0px',
z,
scale: 1,
skewX: 0,
transformOrigin: 'bottom center',
rotate3d: [1, 0, 0, '0deg'],
}}
data-testid="wrapper"
/>
)
const wrapper: any = queryByTestId('wrapper')!
expect(wrapper.style.transform).toBe('none')
})
it('preserves transform-style and transform-origin properties', () => {
const { queryByTestId } = render(
<a.div
style={{
transformOrigin: 'bottom center',
transformStyle: 'preserve-3d',
transform: 'translateX(40px)',
scale: [1, 2],
}}
data-testid="wrapper"
/>
)
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<T>(value: T): SpringValue<Animatable<T>> {
return new SpringValue(value!)
}
|