|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { getRelativeValue, isRelativeValue, normalizeValue } from '../src/utilities.js' |
| 4 | + |
| 5 | +describe('isRelativeValue', () => { |
| 6 | + it('returns true for a percentage value', () => { |
| 7 | + expect(isRelativeValue('0%')).toBe(true) |
| 8 | + expect(isRelativeValue('100%')).toBe(true) |
| 9 | + expect(isRelativeValue('150%')).toBe(true) |
| 10 | + expect(isRelativeValue('20px')).toBe(false) |
| 11 | + expect(isRelativeValue('1.5rem')).toBe(false) |
| 12 | + }) |
| 13 | + it('returns true for a unitless value', () => { |
| 14 | + expect(isRelativeValue('')).toBe(false) |
| 15 | + expect(isRelativeValue('0')).toBe(true) |
| 16 | + expect(isRelativeValue('1.5')).toBe(true) |
| 17 | + expect(isRelativeValue(0)).toBe(true) |
| 18 | + expect(isRelativeValue(1.5)).toBe(true) |
| 19 | + }) |
| 20 | +}) |
| 21 | + |
| 22 | +describe('getRelativeValue', () => { |
| 23 | + it('returns a floating point number from a percentage value', () => { |
| 24 | + expect(getRelativeValue('0%')).toBe(0) |
| 25 | + expect(getRelativeValue('100%')).toBe(1) |
| 26 | + expect(getRelativeValue('150%')).toBe(1.5) |
| 27 | + expect(getRelativeValue('20px')).toBe(20) |
| 28 | + expect(getRelativeValue('1.5rem')).toBe(1.5) |
| 29 | + }) |
| 30 | + it('returns a floating point number from a unitless value', () => { |
| 31 | + expect(getRelativeValue('0')).toBe(0) |
| 32 | + expect(getRelativeValue('1')).toBe(1) |
| 33 | + expect(getRelativeValue('1.5')).toBe(1.5) |
| 34 | + expect(getRelativeValue('150')).toBe(150) |
| 35 | + expect(getRelativeValue(0)).toBe(0) |
| 36 | + expect(getRelativeValue(1)).toBe(1) |
| 37 | + expect(getRelativeValue(1.5)).toBe(1.5) |
| 38 | + expect(getRelativeValue(150)).toBe(150) |
| 39 | + expect(getRelativeValue('20px')).toBe(20) |
| 40 | + expect(getRelativeValue('1.5rem')).toBe(1.5) |
| 41 | + }) |
| 42 | +}) |
| 43 | + |
| 44 | +describe('normalizeValue', () => { |
| 45 | + it('returns a number from a pixel value', () => { |
| 46 | + expect(normalizeValue('0px', 16)).toBe(0) |
| 47 | + expect(normalizeValue('20px', 16)).toBe(20) |
| 48 | + }) |
| 49 | + it('returns a number from a rem value', () => { |
| 50 | + expect(normalizeValue('0rem', 16)).toBe(0) |
| 51 | + expect(normalizeValue('1rem', 16)).toBe(16) |
| 52 | + expect(normalizeValue('1.5rem', 16)).toBe(24) |
| 53 | + }) |
| 54 | + it('returns a number from a number value', () => { |
| 55 | + expect(normalizeValue(0, 16)).toBe(0) |
| 56 | + expect(normalizeValue(20, 16)).toBe(20) |
| 57 | + }) |
| 58 | +}) |
0 commit comments