-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds FeaturedProductAttributes component
- Loading branch information
1 parent
b19bf91
commit 341f535
Showing
8 changed files
with
197 additions
and
3 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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
46 changes: 46 additions & 0 deletions
46
src/components/featured-product-attributes/featured-product-attributes.jsx
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,46 @@ | ||
// | ||
// Copyright (c) DITUS INC. All rights reserved. See LICENSE file in the project | ||
// root for details. | ||
// | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ProductAttributes from '../product-attributes/product-attributes'; | ||
|
||
/** | ||
* Represents a list of attributes for a product that are designated as featured | ||
* attributes, that is a select number of attributes that should be highlighted | ||
* to a customer. | ||
* @param {*} props The properties of the component. | ||
* @returns {HTMLElement} An HTML element representing the component. | ||
*/ | ||
function FeaturedProductAttributes(props) { | ||
const { | ||
attributes, | ||
} = props; | ||
|
||
if (!attributes || attributes.length === 0) { | ||
return null; | ||
} | ||
|
||
const featured = attributes.filter((x) => x.isFeatured); | ||
if (!featured || featured.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ProductAttributes attributes={featured} /> | ||
); | ||
} | ||
|
||
export default FeaturedProductAttributes; | ||
|
||
FeaturedProductAttributes.propTypes = { | ||
/** | ||
* Specifies an array of attributes. | ||
*/ | ||
attributes: PropTypes.arrayOf(PropTypes.shape), | ||
}; | ||
|
||
FeaturedProductAttributes.defaultProps = { | ||
attributes: [], | ||
}; |
94 changes: 94 additions & 0 deletions
94
src/components/featured-product-attributes/featured-product-attributes.test.jsx
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,94 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
// | ||
// Copyright (c) DITUS INC. All rights reserved. See LICENSE file in the project | ||
// root for details. | ||
// | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import FeaturedProductAttributes from './featured-product-attributes'; | ||
import ProductAttributes from '../product-attributes/product-attributes'; | ||
|
||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
jest.mock('../product-attributes/product-attributes', () => jest.fn((props) => (<div {...props} />))); | ||
|
||
describe('FeaturedProductAttributes', () => { | ||
it('does not display if no attributes are specified.', async () => { | ||
const { container, rerender } = render( | ||
<FeaturedProductAttributes />, | ||
); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
|
||
rerender( | ||
<FeaturedProductAttributes attributes={[]} />, | ||
); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('does not display if no attributes are marked as featured.', async () => { | ||
const { container } = render( | ||
<FeaturedProductAttributes | ||
attributes={ | ||
[ | ||
{ | ||
isFeatured: false, | ||
name: 'Color', | ||
value: 'Red', | ||
}, | ||
] | ||
} | ||
/>, | ||
); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('displays featured attributes.', async () => { | ||
const attributes = [ | ||
{ | ||
isFeatured: true, | ||
name: 'Color', | ||
value: 'Red', | ||
}, | ||
]; | ||
|
||
const { container } = render( | ||
<FeaturedProductAttributes | ||
attributes={attributes} | ||
/>, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
|
||
expect(ProductAttributes).toHaveBeenCalledTimes(1); | ||
expect(ProductAttributes).toHaveBeenCalledWith({ attributes: [{ isFeatured: true, name: 'Color', value: 'Red' }] }, {}); | ||
}); | ||
|
||
it('does not display non-featured attributes.', async () => { | ||
const attributes = [ | ||
{ | ||
isFeatured: false, | ||
name: 'Material', | ||
value: 'Cotton', | ||
}, | ||
{ | ||
isFeatured: true, | ||
name: 'Color', | ||
value: 'Red', | ||
}, | ||
]; | ||
|
||
const { container } = render( | ||
<FeaturedProductAttributes | ||
attributes={attributes} | ||
/>, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
expect(ProductAttributes).toHaveBeenCalledWith({ attributes: [{ isFeatured: true, name: 'Color', value: 'Red' }] }, {}); | ||
}); | ||
}); |
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,46 @@ | ||
/* eslint-disable jsdoc/require-jsdoc */ | ||
/* eslint-disable react/jsx-props-no-spreading */ | ||
import React from 'react'; | ||
import { createTheme, ThemeProvider } from '@mui/material/styles'; | ||
import { CssBaseline, Grid } from '@mui/material'; | ||
import FeaturedProductAttributes from '../components/featured-product-attributes/featured-product-attributes'; | ||
|
||
export default { | ||
title: 'Components/FeaturedProductAttributes', | ||
component: FeaturedProductAttributes, | ||
argTypes: { | ||
attributes: { | ||
control: 'object', | ||
}, | ||
}, | ||
}; | ||
|
||
const theme = createTheme({ | ||
}); | ||
|
||
function Template(args) { | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
<Grid container> | ||
<FeaturedProductAttributes {...args} /> | ||
</Grid> | ||
</ThemeProvider> | ||
); | ||
} | ||
|
||
export const Primary = Template.bind({}); | ||
Primary.args = { | ||
attributes: [ | ||
{ | ||
isFeatured: false, | ||
name: 'Color', | ||
value: 'Red', | ||
}, | ||
{ | ||
isFeatured: true, | ||
name: 'Material', | ||
value: '100% Cotton', | ||
}, | ||
], | ||
}; |