-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
182 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { makePropTypes, makeValidatingTransform } from "src/base/helpers/badge"; | ||
import { DEFAULTS } from "src/base/helpers/variables"; | ||
import { tuple } from "../../../utils"; | ||
|
||
import { | ||
validateBoolPropType, | ||
validateOneOfPropType, | ||
validateStringOrNumberPropType, | ||
} from "src/__tests__/testing"; | ||
import { | ||
testItShouldNotSetClassNameOnEmpty, | ||
testItShouldPreserveCustomClassName, | ||
testItShouldPreserveUnknown, | ||
testItShouldUseDefaultLocationProp, | ||
} from "./testing"; | ||
|
||
const CNAME = "foo"; | ||
const LOC = "prop"; | ||
|
||
describe("Badge helpers", () => { | ||
const propTypes = makePropTypes(); | ||
const vtfunc = makeValidatingTransform(); | ||
|
||
describe("propTypes", () => { | ||
validateStringOrNumberPropType(propTypes, "badge"); | ||
validateOneOfPropType(propTypes, "badgeColor", DEFAULTS.colors); | ||
validateBoolPropType(propTypes, "badgeOutlined"); | ||
validateBoolPropType(propTypes, "badgeRounded"); | ||
validateOneOfPropType(propTypes, "badgeSize", DEFAULTS.badgeSizes); | ||
testItShouldUseDefaultLocationProp(vtfunc, { | ||
badgeSize: "__UNKNOWN", | ||
}); | ||
|
||
describe("custom", () => { | ||
const customBadgeSizes = tuple("a", "b"); | ||
const customPropTypes = makePropTypes({ | ||
badgeSizes: customBadgeSizes, | ||
}); | ||
|
||
validateOneOfPropType(customPropTypes, "badgeSize", customBadgeSizes); | ||
}); | ||
}); | ||
|
||
describe("transform", () => { | ||
testItShouldPreserveUnknown(vtfunc); | ||
testItShouldNotSetClassNameOnEmpty(vtfunc); | ||
testItShouldPreserveCustomClassName(vtfunc); | ||
|
||
it("should should get badge className and data-badge", () => { | ||
expect(vtfunc({ badge: "foobar" }, CNAME, LOC)).toEqual({ | ||
className: "badge", | ||
"data-badge": "foobar", | ||
}); | ||
}); | ||
|
||
DEFAULTS.colors.map(color => { | ||
it(`should be ${color}`, () => { | ||
expect(vtfunc({ badgeColor: color }, CNAME, LOC)).toEqual({ | ||
className: `has-badge-${color}`, | ||
}); | ||
}); | ||
}); | ||
|
||
[false, true].map(badgeOutlined => { | ||
it(`should ${badgeOutlined ? "" : "not "}be badgeOutlined`, () => { | ||
expect(vtfunc({ badgeOutlined }, CNAME, LOC)).toEqual({ | ||
className: badgeOutlined ? "has-badge-outlined" : "", | ||
}); | ||
}); | ||
}); | ||
|
||
[false, true].map(badgeRounded => { | ||
it(`should ${badgeRounded ? "" : "not "}be badgeRounded`, () => { | ||
expect(vtfunc({ badgeRounded }, CNAME, LOC)).toEqual({ | ||
className: badgeRounded ? "has-badge-rounded" : "", | ||
}); | ||
}); | ||
}); | ||
|
||
DEFAULTS.badgeSizes.map(badgeSize => { | ||
it(`should be size ${badgeSize}`, () => { | ||
expect(vtfunc({ badgeSize }, CNAME, LOC)).toEqual({ | ||
className: `has-badge-${badgeSize}`, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import classNames from "classnames"; | ||
import PropTypes from "prop-types"; | ||
|
||
import { | ||
makePropTypesFactory, | ||
makeValidatingTransformFactory, | ||
TransformFunction, | ||
} from "./factory"; | ||
import { DEFAULTS, Variables } from "./variables"; | ||
|
||
export type BadgeHelpersProps = Partial<{ | ||
badge: number | string; | ||
badgeColor: Variables["colors"]; | ||
badgeOutlined: boolean; | ||
badgeRounded: boolean; | ||
badgeSize: (typeof DEFAULTS["badgeSizes"])[number]; | ||
}>; | ||
|
||
// Factories | ||
export const makePropTypes = makePropTypesFactory(vars => ({ | ||
badge: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
badgeColor: PropTypes.oneOf(vars.colors), | ||
badgeOutlined: PropTypes.bool, | ||
badgeRounded: PropTypes.bool, | ||
badgeSize: PropTypes.oneOf(vars.badgeSizes), | ||
})); | ||
|
||
export const transform: TransformFunction<BadgeHelpersProps> = props => { | ||
const { | ||
badge, | ||
badgeColor, | ||
badgeOutlined, | ||
badgeRounded, | ||
badgeSize, | ||
...rest | ||
} = props; | ||
|
||
rest.className = classNames( | ||
{ | ||
badge, | ||
[`has-badge-${badgeColor}`]: badgeColor, | ||
"has-badge-outlined": badgeOutlined, | ||
"has-badge-rounded": badgeRounded, | ||
[`has-badge-${badgeSize}`]: badgeSize, | ||
}, | ||
rest.className, | ||
); | ||
|
||
if (badge !== undefined) { | ||
rest["data-badge"] = badge; | ||
} | ||
|
||
return rest; | ||
}; | ||
|
||
export const makeValidatingTransform = makeValidatingTransformFactory( | ||
makePropTypes, | ||
transform, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.