-
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.
- Loading branch information
1 parent
594d168
commit 3185bb6
Showing
9 changed files
with
2,095 additions
and
130 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.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
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
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,86 @@ | ||
// | ||
// 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 { Badge, IconButton } from '@mui/material'; | ||
import CartOutline from 'react-ionicons/lib/CartOutline'; | ||
import { useTranslationProps } from '@ditus/react-translation'; | ||
|
||
/** | ||
* Represents the icon used to display the shopping cart. | ||
* @param {*} props The properties of the component. | ||
* @returns {HTMLElement} An HTML element representing the component. | ||
*/ | ||
function CartIcon(props) { | ||
const { | ||
hidden, | ||
count, | ||
label, | ||
onClick, | ||
} = useTranslationProps({ name: 'CartIcon', props }); | ||
|
||
if (hidden) { | ||
return null; | ||
} | ||
|
||
const handleClick = () => { | ||
if (onClick) { | ||
onClick(); | ||
} | ||
}; | ||
|
||
// If the value is a string, showZero does not work in the badge component. | ||
const numericCount = parseInt(count, 10); | ||
|
||
return ( | ||
<IconButton | ||
aria-label={label} | ||
onClick={handleClick} | ||
sx={{ fontSize: 0 }} | ||
> | ||
<Badge | ||
badgeContent={numericCount} | ||
color="primary" | ||
showZero={false} | ||
> | ||
<CartOutline | ||
height="30px" | ||
width="30px" | ||
/> | ||
</Badge> | ||
</IconButton> | ||
); | ||
} | ||
|
||
export default CartIcon; | ||
|
||
CartIcon.propTypes = { | ||
/** | ||
* Specifies the number of items in the cart. | ||
*/ | ||
count: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
|
||
/** | ||
* Specifies whether the component is visible or hidden. | ||
*/ | ||
hidden: PropTypes.bool, | ||
|
||
/** | ||
* Specifies the label on the <button className=""></button> | ||
*/ | ||
label: PropTypes.string, | ||
|
||
/** | ||
* Specifies the callback function to invoke when the button is clicked. | ||
*/ | ||
onClick: PropTypes.func, | ||
}; | ||
|
||
CartIcon.defaultProps = { | ||
count: 0, | ||
hidden: false, | ||
label: 'Shopping Cart', | ||
onClick: null, | ||
}; |
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 @@ | ||
/** | ||
* @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, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import CartIcon from './cart-icon'; | ||
|
||
describe('CartIcon', () => { | ||
it('displays by default.', async () => { | ||
const { container } = render( | ||
<CartIcon />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('does not display when hidden.', async () => { | ||
const { container } = render( | ||
<CartIcon hidden />, | ||
); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('displays when not hidden.', async () => { | ||
const { container } = render( | ||
<CartIcon hidden={false} />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('displays a button with a default label.', async () => { | ||
const { container } = render( | ||
<CartIcon />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
expect(screen.queryByRole('button', { name: 'Shopping Cart' })).not.toBeNull(); | ||
}); | ||
|
||
it('displays a button with the the specified label.', async () => { | ||
const { container } = render( | ||
<CartIcon label="This is new" />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
expect(screen.queryByRole('button', { name: 'This is new' })).not.toBeNull(); | ||
}); | ||
|
||
it('displays a badge when the shopping cart contains items.', async () => { | ||
const { container } = render( | ||
<CartIcon count={3} />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
expect(screen.queryByText('3')).not.toBeNull(); | ||
}); | ||
|
||
it('can be clicked.', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const handleClick = jest.fn(); | ||
|
||
const { container, rerender } = render( | ||
<CartIcon onClick={handleClick} />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
|
||
await user.click(screen.getByRole('button')); | ||
expect(handleClick).toBeCalled(); | ||
|
||
// The component should not produce an error if the button is clicked and no | ||
// onClick callback is provided. | ||
rerender( | ||
<CartIcon />, | ||
); | ||
|
||
await user.click(screen.getByRole('button')); | ||
}); | ||
}); |
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,29 @@ | ||
/* 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 } from '@mui/material'; | ||
import CartIcon from '../components/cart-icon/cart-icon'; | ||
|
||
export default { | ||
title: 'Components/CartIcon', | ||
component: CartIcon, | ||
argTypes: { | ||
}, | ||
}; | ||
|
||
const theme = createTheme({ | ||
}); | ||
|
||
function Template(args) { | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
<CartIcon {...args} /> | ||
</ThemeProvider> | ||
); | ||
} | ||
|
||
export const Primary = Template.bind({}); | ||
Primary.args = { | ||
}; |