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

Add parseQuantityAndUnitFromRawValue tests #45260

Merged
merged 2 commits into from
Oct 26, 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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- `Context`: updated to ignore `react/exhaustive-deps` eslint rule ([#45044](https://github.com/WordPress/gutenberg/pull/45044))
- `Button`: Refactor Storybook to controls and align docs ([#44105](https://github.com/WordPress/gutenberg/pull/44105)).
- `TabPanel`: updated to satisfy `react/exhaustive-deps` eslint rule ([#44935](https://github.com/WordPress/gutenberg/pull/44935))
- `UnitControl`: Add tests ([#45260](https://github.com/WordPress/gutenberg/pull/45260)).

## 21.3.0 (2022-10-19)

Expand Down
43 changes: 43 additions & 0 deletions packages/components/src/unit-control/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useCustomUnits,
getValidParsedQuantityAndUnit,
getUnitsWithCurrentUnit,
parseQuantityAndUnitFromRawValue,
} from '../utils';
import type { WPUnitControlUnit } from '../types';

Expand Down Expand Up @@ -242,4 +243,46 @@ describe( 'UnitControl utils', () => {
expect( result ).toEqual( limitedUnits );
} );
} );

describe( 'parseQuantityAndUnitFromRawValue', () => {
const cases: [
number | string | undefined,
number | undefined,
string | undefined
][] = [
// Test undefined.
[ undefined, undefined, undefined ],
// Test integers and non-integers.
[ 1, 1, undefined ],
[ 1.25, 1.25, undefined ],
[ '123', 123, undefined ],
[ '1.5', 1.5, undefined ],
[ '0.75', 0.75, undefined ],
// Valid simple CSS values.
[ '20px', 20, 'px' ],
[ '0.8em', 0.8, 'em' ],
[ '2rem', 2, 'rem' ],
[ '1.4vw', 1.4, 'vw' ],
[ '0.4vh', 0.4, 'vh' ],
[ '-5px', -5, 'px' ],
// Complex CSS values that shouldn't parse.
[ 'abs(-15px)', undefined, undefined ],
[ 'calc(10px + 1)', undefined, undefined ],
[ 'clamp(2.5rem, 4vw, 3rem)', undefined, undefined ],
[ 'max(4.5em, 3vh)', undefined, undefined ],
[ 'min(10px, 1rem)', undefined, undefined ],
[ 'minmax(30px, auto)', undefined, undefined ],
[ 'var(--wp--font-size)', undefined, undefined ],
];

test.each( cases )(
'given %p as argument, returns value = %p and unit = %p',
( rawValue, expectedQuantity, expectedUnit ) => {
const [ quantity, unit ] =
parseQuantityAndUnitFromRawValue( rawValue );
expect( quantity ).toBe( expectedQuantity );
expect( unit ).toBe( expectedUnit );
}
);
} );
} );