Skip to content

Commit

Permalink
Fix Grid prop types and cover it with better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkudrna committed Dec 11, 2020
1 parent 84248ce commit e743a72
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/lib/components/layout/Grid/Grid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const Grid = ({
id={id}
className={styles.root}
style={{
'--rui-local-auto-flow': autoFlow,
...(typeof autoFlow !== 'undefined' ? { '--rui-local-auto-flow': autoFlow } : {}),
...generateResponsiveCustomProperties(columns, 'columns'),
...generateResponsiveCustomProperties(columnGap, 'column-gap'),
...generateResponsiveCustomProperties(rows, 'rows'),
Expand Down Expand Up @@ -65,7 +65,7 @@ Grid.propTypes = {
* Content alignment. Accepts any valid value of `align-content` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-content) for more.
*/
alignContent: PropTypes.oneOf([
alignContent: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand All @@ -81,7 +81,7 @@ Grid.propTypes = {
* Items alignment. Accepts any valid value of `align-items` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-items) for more.
*/
alignItems: PropTypes.oneOf([
alignItems: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand All @@ -106,7 +106,7 @@ Grid.propTypes = {
* Gap between columns. Accepts any valid value of `grid-column-gap` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/column-gap) for more.
*/
columnGap: PropTypes.oneOf([
columnGap: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand All @@ -122,7 +122,7 @@ Grid.propTypes = {
* Grid columns. Accepts any valid value of `grid-template-columns` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns) for more.
*/
columns: PropTypes.oneOf([
columns: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand All @@ -142,7 +142,7 @@ Grid.propTypes = {
* Content justification. Accepts any valid value of `justify-content` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content) for more.
*/
justifyContent: PropTypes.oneOf([
justifyContent: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand All @@ -158,7 +158,7 @@ Grid.propTypes = {
* Items justification. Accepts any valid value of `justify-items` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-items) for more.
*/
justifyItems: PropTypes.oneOf([
justifyItems: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand All @@ -174,7 +174,7 @@ Grid.propTypes = {
* Gap between rows. Accepts any valid value of `grid-row-gap` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/row-gap) for more.
*/
rowGap: PropTypes.oneOf([
rowGap: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand All @@ -190,7 +190,7 @@ Grid.propTypes = {
* Grid rows. Accepts any valid value of `grid-template-rows` CSS property.
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-rows) for more.
*/
rows: PropTypes.oneOf([
rows: PropTypes.oneOfType([
PropTypes.string,
PropTypes.shape({
xs: PropTypes.string,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/layout/Grid/GridSpan.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ GridSpan.propTypes = {
/**
* Number of columns to span.
*/
columns: PropTypes.oneOf([
columns: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
xs: PropTypes.number,
Expand All @@ -66,7 +66,7 @@ GridSpan.propTypes = {
/**
* Number of rows to span.
*/
rows: PropTypes.oneOf([
rows: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
xs: PropTypes.number,
Expand Down
54 changes: 54 additions & 0 deletions src/lib/components/layout/Grid/__tests__/Grid.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,58 @@ describe('rendering', () => {

expect(tree).toMatchSnapshot();
});

it('renders correctly with simple props', () => {
const tree = shallow((
<Grid
alignContent="center"
alignItems="center"
autoFlow="dense"
columns="1fr 1fr"
columnGap="1rem"
id="my-grid"
justifyContent="center"
justifyItems="center"
rows="auto"
rowGap="1rem"
>
<div>content</div>
</Grid>
));

expect(tree).toMatchSnapshot();
});

it('renders correctly with complex props', () => {
const tree = shallow((
<Grid
alignContent="center"
alignItems="center"
autoFlow="dense"
columns={{
md: '1fr 2fr',
xs: '1fr',
}}
columnGap={{
lg: 'var(--rui-spacing-4)',
md: 'var(--rui-spacing-2)',
}}
id="my-grid"
justifyContent="center"
justifyItems="center"
rows={{
md: 'auto 200px auto',
xs: 'auto auto 200px 200px',
}}
rowGap={{
md: 'var(--rui-spacing-4)',
xs: 'var(--rui-spacing-3)',
}}
>
<div>content</div>
</Grid>
));

expect(tree).toMatchSnapshot();
});
});
34 changes: 34 additions & 0 deletions src/lib/components/layout/Grid/__tests__/GridSpan.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,38 @@ describe('rendering', () => {

expect(tree).toMatchSnapshot();
});

it('renders correctly with simple props', () => {
const tree = shallow((
<GridSpan
columns={2}
id="my-grid-span"
rows={3}
>
<div>content</div>
</GridSpan>
));

expect(tree).toMatchSnapshot();
});

it('renders correctly with complex props', () => {
const tree = shallow((
<GridSpan
columns={{
md: 1,
xs: 2,
}}
id="my-grid-span"
rows={{
md: 4,
xs: 3,
}}
>
<div>content</div>
</GridSpan>
));

expect(tree).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,33 @@
exports[`rendering renders correctly with a single child 1`] = `
<div
className="root"
style={Object {}}
>
<div>
content
</div>
</div>
`;

exports[`rendering renders correctly with complex props 1`] = `
<div
className="root"
id="my-grid"
style={
Object {
"--rui-local-auto-flow": undefined,
"--rui-local-align-content-xs": "center",
"--rui-local-align-items-xs": "center",
"--rui-local-auto-flow": "dense",
"--rui-local-column-gap-lg": "var(--rui-spacing-4)",
"--rui-local-column-gap-md": "var(--rui-spacing-2)",
"--rui-local-columns-md": "1fr 2fr",
"--rui-local-columns-xs": "1fr",
"--rui-local-justify-content-xs": "center",
"--rui-local-justify-items-xs": "center",
"--rui-local-row-gap-md": "var(--rui-spacing-4)",
"--rui-local-row-gap-xs": "var(--rui-spacing-3)",
"--rui-local-rows-md": "auto 200px auto",
"--rui-local-rows-xs": "auto auto 200px 200px",
}
}
>
Expand All @@ -18,11 +42,7 @@ exports[`rendering renders correctly with a single child 1`] = `
exports[`rendering renders correctly with multiple children 1`] = `
<div
className="root"
style={
Object {
"--rui-local-auto-flow": undefined,
}
}
style={Object {}}
>
<div>
content 1
Expand All @@ -37,3 +57,27 @@ exports[`rendering renders correctly with multiple children 1`] = `
`;

exports[`rendering renders correctly with no children 1`] = `""`;

exports[`rendering renders correctly with simple props 1`] = `
<div
className="root"
id="my-grid"
style={
Object {
"--rui-local-align-content-xs": "center",
"--rui-local-align-items-xs": "center",
"--rui-local-auto-flow": "dense",
"--rui-local-column-gap-xs": "1rem",
"--rui-local-columns-xs": "1fr 1fr",
"--rui-local-justify-content-xs": "center",
"--rui-local-justify-items-xs": "center",
"--rui-local-row-gap-xs": "1rem",
"--rui-local-rows-xs": "auto",
}
}
>
<div>
content
</div>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ exports[`rendering renders correctly with a single child 1`] = `
</div>
`;

exports[`rendering renders correctly with complex props 1`] = `
<div
className="span"
id="my-grid-span"
style={
Object {
"--rui-local-column-span-md": 1,
"--rui-local-column-span-xs": 2,
"--rui-local-row-span-md": 4,
"--rui-local-row-span-xs": 3,
}
}
>
<div>
content
</div>
</div>
`;

exports[`rendering renders correctly with multiple children 1`] = `
<div
className="span"
Expand All @@ -29,3 +48,20 @@ exports[`rendering renders correctly with multiple children 1`] = `
`;

exports[`rendering renders correctly with no children 1`] = `""`;

exports[`rendering renders correctly with simple props 1`] = `
<div
className="span"
id="my-grid-span"
style={
Object {
"--rui-local-column-span-xs": 2,
"--rui-local-row-span-xs": 3,
}
}
>
<div>
content
</div>
</div>
`;

0 comments on commit e743a72

Please sign in to comment.