2021-05-24 10:25:02 +00:00
|
|
|
import { calculateSize } from '../lib/svg/size';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
describe('Testing calcSize', () => {
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Simple size', () => {
|
2020-04-28 09:47:35 +00:00
|
|
|
const width = 36;
|
|
|
|
const height = 48;
|
|
|
|
|
|
|
|
// Get width from height and height from width
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize('48', width / height)).toEqual('36');
|
|
|
|
expect(calculateSize('36', height / width)).toEqual('48');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize(48, width / height)).toEqual(36);
|
|
|
|
expect(calculateSize(36, height / width)).toEqual(48);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Numbers', () => {
|
2020-04-28 09:47:35 +00:00
|
|
|
const width = 36;
|
|
|
|
const height = 48;
|
|
|
|
|
|
|
|
// Simple numbers
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize(24, width / height)).toEqual(18);
|
|
|
|
expect(calculateSize(30, width / height)).toEqual(22.5);
|
|
|
|
expect(calculateSize(99, width / height)).toEqual(74.25);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Rounding numbers
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize(100 / 3, height / width)).toEqual(44.45);
|
|
|
|
expect(calculateSize(11.1111111, width / height)).toEqual(8.34);
|
|
|
|
expect(calculateSize(11.1111111, width / height, 1000)).toEqual(8.334);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
2021-09-20 20:53:49 +00:00
|
|
|
test('Strings', () => {
|
2020-04-28 09:47:35 +00:00
|
|
|
const width = 36;
|
|
|
|
const height = 48;
|
|
|
|
|
|
|
|
// Simple units
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize('48px', width / height)).toEqual('36px');
|
|
|
|
expect(calculateSize('24%', width / height)).toEqual('18%');
|
|
|
|
expect(calculateSize('1em', width / height)).toEqual('0.75em');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add space
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize('24 Pixels', width / height)).toEqual('18 Pixels');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Multiple sets of numbers
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize('48% + 5em', width / height)).toEqual(
|
2020-04-28 09:47:35 +00:00
|
|
|
'36% + 3.75em'
|
|
|
|
);
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize('calc(1em + 8px)', height / width)).toEqual(
|
2020-04-28 09:47:35 +00:00
|
|
|
'calc(1.34em + 10.67px)'
|
|
|
|
);
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(
|
|
|
|
calculateSize('-webkit-calc(1em + 8px)', width / height)
|
2021-09-20 20:53:49 +00:00
|
|
|
).toEqual('-webkit-calc(0.75em + 6px)');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Invalid strings
|
2021-09-20 20:53:49 +00:00
|
|
|
expect(calculateSize('-.', width / height)).toEqual('-.');
|
|
|
|
expect(calculateSize('@foo', width / height)).toEqual('@foo');
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
});
|