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

fix(between): added error case when to/from unit is differs from mix/max #511

Merged
merged 16 commits into from May 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/internalHelpers/errors.md
Expand Up @@ -302,3 +302,7 @@ CSS variable not found and no default was provided.
## 75

important requires a valid style object, got a %s instead.

## 76

fromSize and toSize must be provided as stringified numbers with the same units as minScreen and maxScreen.
4 changes: 4 additions & 0 deletions src/mixins/between.js
Expand Up @@ -54,6 +54,10 @@ export default function between(
throw new PolishedError(48)
}

if (fromSizeUnit !== minScreenUnit || toSizeUnit !== maxScreenUnit) {
throw new PolishedError(76)
}

const slope = (unitlessFromSize - unitlessToSize)
/ (unitlessMinScreen - unitlessMaxScreen)
const base = unitlessToSize - slope * unitlessMaxScreen
Expand Down
4 changes: 0 additions & 4 deletions src/mixins/test/__snapshots__/between.test.js.snap
Expand Up @@ -3,7 +3,3 @@
exports[`between should return a valid calc formula when not passed min/max screen sizes 1`] = `"calc(-9.09px + 9.09vw)"`;

exports[`between should return a valid calc formula when passed min/max screen sizes 1`] = `"calc(-33.33px + 13.33vw)"`;

exports[`between should return a valid calc formula when passed unitless to/from values as numbers 1`] = `"calc(-9.09 + 9.09vw)"`;

exports[`between should return a valid calc formula when passed unitless to/from values as strings 1`] = `"calc(-9.09 + 9.09vw)"`;
25 changes: 18 additions & 7 deletions src/mixins/test/between.test.js
Expand Up @@ -10,13 +10,6 @@ describe('between', () => {
expect(between('20px', '100px')).toMatchSnapshot()
})

it('should return a valid calc formula when passed unitless to/from values as numbers', () => {
expect(between(20, 100)).toMatchSnapshot()
})

it('should return a valid calc formula when passed unitless to/from values as strings', () => {
expect(between('20', '100')).toMatchSnapshot()
})
// Errors
it('should throw an error when not passed min/max screen size as a string', () => {
expect(() => {
Expand Down Expand Up @@ -44,4 +37,22 @@ describe('between', () => {
'fromSize and toSize must be provided as stringified numbers with the same units.',
)
})

it('should throw an error when passed to/from size with different units than mix/max screen', () => {
expect(() => {
// $FlowFixMe
between('1em', '100em', '400px', '1000px')
}).toThrow(
'fromSize and toSize must be provided as stringified numbers with the same units as minScreen and maxScreen.',
)
})

it('should throw an error when passed to/from size with no units but mix/max with units', () => {
expect(() => {
// $FlowFixMe
between(20, 100)
}).toThrow(
'fromSize and toSize must be provided as stringified numbers with the same units as minScreen and maxScreen.',
)
})
})