-
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
28dc492
commit 6c9825e
Showing
8 changed files
with
183 additions
and
4 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
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,62 @@ | ||
// | ||
// 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 { useTranslationProps } from '@ditus/react-translation'; | ||
import { Button } from '@mui/material'; | ||
|
||
/** | ||
* Represents a button that adds a product to the users shopping cart. | ||
* @param {*} props The properties of the component. | ||
* @returns {HTMLElement} An HTML element representing the component. | ||
*/ | ||
function AddToCart(props) { | ||
const { | ||
hidden, | ||
label, | ||
onClick, | ||
} = useTranslationProps({ name: 'AddToCart', props }); | ||
|
||
if (hidden) { | ||
return null; | ||
} | ||
|
||
const handleSelect = () => { | ||
if (onClick) { | ||
onClick(); | ||
} | ||
}; | ||
|
||
return ( | ||
<Button onClick={handleSelect}> | ||
{label} | ||
</Button> | ||
); | ||
} | ||
|
||
export default AddToCart; | ||
|
||
AddToCart.propTypes = { | ||
/** | ||
* Specifies whether the component is visible or hidden. | ||
*/ | ||
hidden: PropTypes.bool, | ||
|
||
/** | ||
* Specifies the label that appears. | ||
*/ | ||
label: PropTypes.string, | ||
|
||
/** | ||
* Specifies the event to invoke when the button is clicked. | ||
*/ | ||
onClick: PropTypes.func, | ||
}; | ||
|
||
AddToCart.defaultProps = { | ||
hidden: false, | ||
label: 'Add to 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,80 @@ | ||
/** | ||
* @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 AddToCart from './add-to-cart'; | ||
|
||
describe('AddToCart', () => { | ||
it('is not hidden by default.', async () => { | ||
const { container } = render( | ||
<AddToCart />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
expect(screen.queryByRole('button')).not.toBeNull(); | ||
}); | ||
|
||
it('displays when not hidden.', async () => { | ||
const { container } = render( | ||
<AddToCart hidden={false} />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('does not display when hidden.', async () => { | ||
const { container } = render( | ||
<AddToCart hidden />, | ||
); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('displays a button with a default label.', async () => { | ||
const { container } = render( | ||
<AddToCart />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
expect(screen.queryByRole('button', { name: 'Add to Cart' })).not.toBeNull(); | ||
}); | ||
|
||
it('displays a button with the the specified label.', async () => { | ||
const { container } = render( | ||
<AddToCart label="This is new" />, | ||
); | ||
|
||
expect(container).not.toBeEmptyDOMElement(); | ||
expect(screen.queryByRole('button', { name: 'This is new' })).not.toBeNull(); | ||
}); | ||
|
||
it('can be clicked.', async () => { | ||
const user = userEvent.setup(); | ||
|
||
const handleClick = jest.fn(); | ||
|
||
const { container, rerender } = render( | ||
<AddToCart 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( | ||
<AddToCart />, | ||
); | ||
|
||
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 AddToCart from '../components/add-to-cart/add-to-cart'; | ||
|
||
export default { | ||
title: 'Components/AddToCart', | ||
component: AddToCart, | ||
argTypes: { | ||
}, | ||
}; | ||
|
||
const theme = createTheme({ | ||
}); | ||
|
||
function Template(args) { | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<CssBaseline /> | ||
<AddToCart {...args} /> | ||
</ThemeProvider> | ||
); | ||
} | ||
|
||
export const Primary = Template.bind({}); | ||
Primary.args = { | ||
}; |