2020-04-28 09:47:35 +00:00
|
|
|
import 'mocha';
|
|
|
|
import { expect } from 'chai';
|
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', () => {
|
|
|
|
it('Simple size', () => {
|
|
|
|
const width = 36;
|
|
|
|
const height = 48;
|
|
|
|
|
|
|
|
// Get width from height and height from width
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize('48', width / height)).to.be.equal('36');
|
|
|
|
expect(calculateSize('36', height / width)).to.be.equal('48');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize(48, width / height)).to.be.equal(36);
|
|
|
|
expect(calculateSize(36, height / width)).to.be.equal(48);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Numbers', () => {
|
|
|
|
const width = 36;
|
|
|
|
const height = 48;
|
|
|
|
|
|
|
|
// Simple numbers
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize(24, width / height)).to.be.equal(18);
|
|
|
|
expect(calculateSize(30, width / height)).to.be.equal(22.5);
|
|
|
|
expect(calculateSize(99, width / height)).to.be.equal(74.25);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Rounding numbers
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize(100 / 3, height / width)).to.be.equal(44.45);
|
|
|
|
expect(calculateSize(11.1111111, width / height)).to.be.equal(8.34);
|
|
|
|
expect(calculateSize(11.1111111, width / height, 1000)).to.be.equal(
|
|
|
|
8.334
|
|
|
|
);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Strings', () => {
|
|
|
|
const width = 36;
|
|
|
|
const height = 48;
|
|
|
|
|
|
|
|
// Simple units
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize('48px', width / height)).to.be.equal('36px');
|
|
|
|
expect(calculateSize('24%', width / height)).to.be.equal('18%');
|
|
|
|
expect(calculateSize('1em', width / height)).to.be.equal('0.75em');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Add space
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize('24 Pixels', width / height)).to.be.equal(
|
|
|
|
'18 Pixels'
|
|
|
|
);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Multiple sets of numbers
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize('48% + 5em', width / height)).to.be.equal(
|
2020-04-28 09:47:35 +00:00
|
|
|
'36% + 3.75em'
|
|
|
|
);
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize('calc(1em + 8px)', height / width)).to.be.equal(
|
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)
|
|
|
|
).to.be.equal('-webkit-calc(0.75em + 6px)');
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Invalid strings
|
2020-12-22 12:49:02 +00:00
|
|
|
expect(calculateSize('-.', width / height)).to.be.equal('-.');
|
|
|
|
expect(calculateSize('@foo', width / height)).to.be.equal('@foo');
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
|
|
|
});
|