| 1 |
import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 |
|
| 3 |
import Swup from '../../src/index.js'; |
| 4 |
import { calculateTimeout, getStyleProperties, toMs } from '../../src/modules/awaitAnimations.js'; |
| 5 |
|
| 6 |
describe('awaitAnimations', () => { |
| 7 |
it('returns a Promise', () => { |
| 8 |
const swup = new Swup(); |
| 9 |
expect(swup.awaitAnimations({ selector: 'main' })).toBeInstanceOf(Promise); |
| 10 |
expect(swup.awaitAnimations({ selector: false })).toBeInstanceOf(Promise); |
| 11 |
expect(swup.awaitAnimations({ selector: false, elements: [] })).toBeInstanceOf(Promise); |
| 12 |
}); |
| 13 |
}); |
| 14 |
|
| 15 |
describe('getStyleProperties', () => { |
| 16 |
let element: HTMLElement; |
| 17 |
|
| 18 |
beforeEach(() => { |
| 19 |
element = document.createElement('div'); |
| 20 |
}); |
| 21 |
|
| 22 |
it('returns an array', () => { |
| 23 |
const style = window.getComputedStyle(element); |
| 24 |
expect(getStyleProperties(style, 'transitionDelay')).toBeInstanceOf(Array); |
| 25 |
}); |
| 26 |
|
| 27 |
it('gets correct element style', () => { |
| 28 |
element.style.transitionDuration = '1s'; |
| 29 |
const style = window.getComputedStyle(element); |
| 30 |
expect(getStyleProperties(style, 'transitionDuration')).toEqual(['1s']); |
| 31 |
expect(getStyleProperties(style, 'animationDuration')).toEqual(['']); |
| 32 |
}); |
| 33 |
|
| 34 |
it('splits style declaration by comma', () => { |
| 35 |
element.style.transitionDelay = '1s, 2s'; |
| 36 |
const style = window.getComputedStyle(element); |
| 37 |
expect(getStyleProperties(style, 'transitionDelay')).toEqual(['1s', '2s']); |
| 38 |
}); |
| 39 |
}); |
| 40 |
|
| 41 |
describe('calculateTimeout', () => { |
| 42 |
it('returns a number', () => { |
| 43 |
expect(typeof calculateTimeout(['1000ms'], ['0ms'])).toBe('number'); |
| 44 |
}); |
| 45 |
|
| 46 |
it('converts to milliseconds', () => { |
| 47 |
expect(calculateTimeout(['1s'], ['0s'])).toEqual(1000); |
| 48 |
}); |
| 49 |
|
| 50 |
it('adds delay to duration', () => { |
| 51 |
expect(calculateTimeout(['1s'], ['3s'])).toEqual(4000); |
| 52 |
}); |
| 53 |
|
| 54 |
it('uses highest value', () => { |
| 55 |
expect(calculateTimeout(['1s', '2s'], ['1s', '2s'])).toEqual(4000); |
| 56 |
expect(calculateTimeout(['1s', '2s'], ['4s', '1s'])).toEqual(5000); |
| 57 |
expect(calculateTimeout(['3s', '2s'], ['2s', '4s'])).toEqual(6000); |
| 58 |
}); |
| 59 |
}); |
| 60 |
|
| 61 |
describe('toMs', () => { |
| 62 |
it('returns a number', () => { |
| 63 |
expect(typeof toMs('1000s')).toBe('number'); |
| 64 |
}); |
| 65 |
|
| 66 |
it('converts to milliseconds', () => { |
| 67 |
expect(toMs('1s')).toBe(1000); |
| 68 |
expect(toMs('2s')).toBe(2000); |
| 69 |
expect(toMs('10s')).toBe(10000); |
| 70 |
}); |
| 71 |
|
| 72 |
it('ignores units', () => { |
| 73 |
expect(toMs('1ms')).toBe(1000); |
| 74 |
expect(toMs('2sec')).toBe(2000); |
| 75 |
expect(toMs(10 as any as string)).toBe(10000); |
| 76 |
}); |
| 77 |
}); |