File size: 2,400 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
import { Interpolation } from './Interpolation'
import { Globals } from '@react-spring/shared'

Globals.assign({
  to: (...args) => new Interpolation(...args),
})

import { assert, _ } from 'spec.ts'
import { SpringValue } from './SpringValue'
import { to } from './interpolate'

function spring<T>(value: T): SpringValue<T> {
  return new SpringValue(value!)
}

describe('AnimatedValue interpolation options', () => {
  it('accepts an AnimatedValue and a range shortcut config', () => {
    const value = to(spring(1), [0, 1, 2], [4, 5, 6])
    expect(value.get()).toBe(5)
  })

  it('accepts a config object with extrapolate extend', () => {
    const value = to(spring(2), {
      range: [0, 1],
      output: [10, 20],
      extrapolate: 'extend',
    })
    expect(value.get()).toBe(30)
  })

  it('accepts a config object with extrapolate clamp', () => {
    const value = to(spring(100), {
      range: [0, 1],
      output: [10, 20],
      extrapolate: 'clamp',
    })
    expect(value.get()).toBe(20)
  })

  it('accepts a config object with extrapolate identity', () => {
    const value = to(spring(100), {
      output: [10, 20],
      extrapolate: 'identity',
    })
    expect(value.get()).toBe(100)
  })

  it('accepts an AnimatedValueArray and a range shortcut config', () => {
    const value = to(spring([1, 2]), [1, 2], [4, 5])
    expect(value.get()).toBe(4)
  })

  it('accepts multiple AnimatedValues and a range shortcut config', () => {
    const value = to(
      [spring(2), spring(4)],
      [0, 2, 4, 6, 8],
      [10, 20, 30, 40, 50]
    )
    assert(value, _ as Interpolation<number>)
    expect(value.get()).toBe(20)
  })

  it('accepts multiple AnimatedValues and an interpolation function', () => {
    const value = to([spring(5), spring('text')] as const, (a, b) => {
      assert(a, _ as number)
      assert(b, _ as string)
      return `t(${a}, ${b})`
    })
    assert(value, _ as Interpolation<string>)
    expect(value.get()).toBe('t(5, text)')
  })

  it('accepts an AnimatedValueArray and an interpolation function', () => {
    const value = to(spring([1, 2, 3]), (r, g, b) => `rgb(${r}, ${g}, ${b})`)
    expect(value.get()).toBe('rgba(1, 2, 3, 1)')
  })

  it('chains interpolations', () => {
    const value = to(spring(1), [0, 1], [1, 2])
      .to(x => x * 2)
      .to([3, 4], [30, 40])
      .to(x => x / 2)
    expect(value.get()).toBe(20)
  })
})