Skip to content

Commit

Permalink
test(lib): add additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Jul 10, 2024
1 parent c6bc743 commit f7d8538
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/lib/util/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
calculateHeaderSize,
calculateInlineTitleSize,
isHeader,
isPx,
pxToRem,
} from './text';

describe('calculateFontTextSize', () => {
Expand Down Expand Up @@ -62,3 +64,33 @@ describe('calculateHeaderSize', () => {
expect(calculateHeaderSize('header-1')).toBe(32);
});
});

describe('pxToRem', () => {
it('should convert px to rem', () => {
expect(pxToRem(16)).toBe(1);
expect(pxToRem(24)).toBe(1.5);
expect(pxToRem(32)).toBe(2);
});

it('should convert px to rem with different base value', () => {
expect(pxToRem(16, 20)).toBe(0.8);
expect(pxToRem(24, 20)).toBe(1.2);
expect(pxToRem(32, 20)).toBe(1.6);
});
});

describe('isPx', () => {
it('should return `true` when value is in px format', () => {
expect(isPx('16px')).toBe(true);
expect(isPx('0px')).toBe(true);
expect(isPx('5903px')).toBe(true);
});

it('should return `false` when value is in not px format', () => {
expect(isPx('px')).toBe(false);
expect(isPx('')).toBe(false);
expect(isPx('16rem')).toBe(false);
expect(isPx('16em')).toBe(false);
expect(isPx('16ch')).toBe(false);
});
});
2 changes: 1 addition & 1 deletion src/lib/util/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const calculateHeaderSize = (header: HTMLHeader | HeaderToken): number => {
}

// If there is some `calc` operation going on, it has to be evaluated.
if (headerComputedStyle.contains('calc')) {
if (headerComputedStyle.includes('calc')) {
const temp = document.createElement('div');

temp.style.setProperty('font-size', `var(--${htmlHeader}-size)`);
Expand Down

0 comments on commit f7d8538

Please sign in to comment.