Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added remaning Tests #6

Merged
merged 5 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion src/Hooks/useConvertUnits.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,102 @@ const remToRem = useConvertUnits({
resultUnit: 'rem'
});

const pxToPx = useConvertUnits({
defaultSize: 12,
size: 16,
sizeUnit: 'px',
resultUnit: 'px'
});

const perToRem = useConvertUnits({
defaultSize: 16,
size: 200,
sizeUnit: '%',
resultUnit: 'rem'
});

const remToPer = useConvertUnits({
defaultSize: 16,
size: 2,
sizeUnit: 'rem',
resultUnit: '%'
});

const perToPer = useConvertUnits({
defaultSize: 16,
size: 200,
sizeUnit: '%',
resultUnit: '%'
});

const pxToPt = useConvertUnits({
defaultSize: 16,
size: 16,
sizeUnit: 'px',
resultUnit: 'pt'
});

const ptToPx = useConvertUnits({
defaultSize: 16,
size: 12,
sizeUnit: 'pt',
resultUnit: 'px'
});

const ptToRem = useConvertUnits({
defaultSize: 16,
size: 12,
sizeUnit: 'pt',
resultUnit: 'rem'
});

// test to check if it converts Px to Rem
test('It converts px to rem', () => {
expect(pxToRem).toBe(2);
})

// test to check if it converts Rem to Pc
test('It converts rem to px', () => {
expect(remToPX).toBe(46);
});

// test to check if it converts Rem to Rem
test('It converts rem to rem', () => {
expect(remToRem).toBe(2.75);
});
});

// test to check if it converts Px to Px
test('It converts px to rem', () => {
expect(pxToPx).toBe(16);
})

// test to check if it converts % to rem
test('It converts px to rem', () => {
expect(perToRem).toBe(2);
})

// test to check if it converts rem to %
test('It converts px to rem', () => {
expect(remToPer).toBe(200);
})

// test to check if it converts % to %
test('It converts px to rem', () => {
expect(perToPer).toBe(200);
})

// test to check if it converts px to pt
test('It converts px to pt', () => {
expect(pxToPt).toBe(12);
})

// test to check if it converts pt to px
test('It converts px to pt', () => {
expect(ptToPx).toBe(16);
})

// test to check if it converts pt to rem
test('It converts pt to rem', () => {
expect(ptToRem).toBe(1);
})

7 changes: 6 additions & 1 deletion src/Hooks/useConvertUnits.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type units = 'px' | '%' | 'rem';
export type units = 'px' | '%' | 'rem' | 'pt';

export interface convertUnitsProps {
defaultSize: number;
Expand All @@ -24,4 +24,9 @@ export default function useConvertUnits({
if ( sizeUnit === '%' && resultUnit === 'rem' ) return size / 100;
if ( sizeUnit === 'rem' && resultUnit === '%' ) return size * 100;

// Handle conversion between pt, px, % and rem
if ( sizeUnit ==='px' && resultUnit === 'pt') return Math.round(size * 72/96);
if (sizeUnit === 'pt' && resultUnit === 'px') return Math.round(size * 96/72) ;
if (sizeUnit === 'pt' && resultUnit === 'rem') return Math.round(size * 0.083333396325467) ;

Mario-Duarte marked this conversation as resolved.
Show resolved Hide resolved
Mario-Duarte marked this conversation as resolved.
Show resolved Hide resolved
}