|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 The Diffusion Studio Authors |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla |
| 5 | + * Public License, v. 2.0 that can be found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 9 | +import { Keyframe } from './keyframe'; |
| 10 | + |
| 11 | +import { AnimationBuilder as Builder, createAnimationBuilder } from './animation-builder'; |
| 12 | +import { EasingFunction } from './keyframe.types'; |
| 13 | + |
| 14 | +export interface AnimationBuilder extends Builder { |
| 15 | + height(value: number, delay?: number, easing?: EasingFunction): this; |
| 16 | + width(value: number, delay?: number, easing?: EasingFunction): this; |
| 17 | +} |
| 18 | + |
| 19 | +export class AnimationBuilder extends Builder { } |
| 20 | + |
| 21 | +class TestObject { |
| 22 | + height = 5; |
| 23 | + width = new Keyframe([0], [0]); |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +describe('The Animation Builder', () => { |
| 28 | + let testObject: TestObject; |
| 29 | + let animate: AnimationBuilder; |
| 30 | + |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + testObject = new TestObject(); |
| 34 | + animate = createAnimationBuilder(new AnimationBuilder(testObject)); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should create and assign a new Keyframe', () => { |
| 38 | + animate.height(20).to(100, 12).width(30).to(80, 18); |
| 39 | + |
| 40 | + expect(testObject.height).toBeInstanceOf(Keyframe); |
| 41 | + expect(testObject.width).toBeInstanceOf(Keyframe); |
| 42 | + |
| 43 | + const height = testObject.height as any as Keyframe<number>; |
| 44 | + |
| 45 | + expect(height.input.length).toBe(2); |
| 46 | + expect(height.output.length).toBe(2); |
| 47 | + |
| 48 | + expect(height.input[0]).toBe(0); |
| 49 | + expect(height.input[1]).toBe(12 / 30 * 1000); |
| 50 | + |
| 51 | + expect(height.output[0]).toBe(20); |
| 52 | + expect(height.output[1]).toBe(100); |
| 53 | + |
| 54 | + const width = testObject.width as any as Keyframe<number>; |
| 55 | + |
| 56 | + expect(width.input.length).toBe(2); |
| 57 | + expect(width.output.length).toBe(2); |
| 58 | + |
| 59 | + expect(width.input[0]).toBe(0); |
| 60 | + expect(width.input[1]).toBe(18 / 30 * 1000); |
| 61 | + |
| 62 | + expect(width.output[0]).toBe(30); |
| 63 | + expect(width.output[1]).toBe(80); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should be based on relative delays', () => { |
| 67 | + animate.height(20).to(90, 12).to(200, 6).to(280, 3); |
| 68 | + |
| 69 | + const height = testObject.height as any as Keyframe<number>; |
| 70 | + |
| 71 | + expect(height.input.length).toBe(4); |
| 72 | + expect(height.output.length).toBe(4); |
| 73 | + |
| 74 | + expect(height.input[0]).toBe(0); |
| 75 | + expect(height.input[1]).toBe(12 / 30 * 1000); |
| 76 | + expect(height.input[2]).toBe(18 / 30 * 1000); |
| 77 | + expect(height.input[3]).toBe(21 / 30 * 1000); |
| 78 | + |
| 79 | + expect(height.output[0]).toBe(20); |
| 80 | + expect(height.output[1]).toBe(90); |
| 81 | + expect(height.output[2]).toBe(200); |
| 82 | + expect(height.output[3]).toBe(280); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should set the easing function', () => { |
| 86 | + animate.height(20, 0, 'easeIn').to(100, 12).width(30, 0, 'easeOut').to(80, 18); |
| 87 | + |
| 88 | + const height = testObject.height as any as Keyframe<number>; |
| 89 | + const width = testObject.width as any as Keyframe<number>; |
| 90 | + |
| 91 | + expect(height.options.easing).toBe('easeIn'); |
| 92 | + expect(width.options.easing).toBe('easeOut'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should animate from the current value', () => { |
| 96 | + animate.height(20, 12); |
| 97 | + |
| 98 | + const height = testObject.height as any as Keyframe<number>; |
| 99 | + |
| 100 | + expect(height.input.length).toBe(2); |
| 101 | + expect(height.output.length).toBe(2); |
| 102 | + |
| 103 | + expect(height.input[0]).toBe(0); |
| 104 | + expect(height.input[1]).toBe(12 / 30 * 1000); |
| 105 | + |
| 106 | + expect(height.output[0]).toBe(5); |
| 107 | + expect(height.output[1]).toBe(20); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should not animate from the current value if it's a Keyframe", () => { |
| 111 | + animate.width(20, 12); |
| 112 | + |
| 113 | + const width = testObject.width as any as Keyframe<number>; |
| 114 | + |
| 115 | + expect(width.input.length).toBe(1); |
| 116 | + expect(width.output.length).toBe(1); |
| 117 | + |
| 118 | + expect(width.input[0]).toBe(12 / 30 * 1000); |
| 119 | + expect(width.output[0]).toBe(20); |
| 120 | + }); |
| 121 | +}); |
0 commit comments