-
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.
Merge pull request #24 from aneurysmjs/connect-store-hook
Connect store hook
- Loading branch information
Showing
51 changed files
with
866 additions
and
281 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
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,9 @@ | ||
/** | ||
* @link https://medium.com/@kilgarenone/use-jest-to-test-redux-async-action-creator-with-axios-in-a-create-react-app-app-d9c9b52eba5e | ||
*/ | ||
const axiosMock = jest.genMockFromModule('axios'); | ||
|
||
// this is the key to fix the axios.create() undefined error! | ||
axiosMock.create = jest.fn(() => axiosMock); | ||
|
||
export default axiosMock; |
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 |
---|---|---|
@@ -1,64 +1,45 @@ | ||
// @flow strict | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
// $FlowIgnore | ||
import { connect } from 'react-redux'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { footerData } from '@/store/actions'; | ||
import { getFooter } from '@/store/reducers/footer'; | ||
import type { FooterType } from '@/store/types/FooterType'; | ||
|
||
import Icon from '@/components/base/Icon/Icon'; | ||
|
||
import './Footer.scss'; | ||
|
||
type PropsType = { | ||
footer: FooterType | ||
}; | ||
|
||
class Footer extends Component<PropsType> { | ||
render() { | ||
const { | ||
footer: { | ||
social, | ||
}, | ||
} = this.props; | ||
|
||
return ( | ||
<footer className="footer"> | ||
<div className="container"> | ||
<div className="footer__top"> | ||
<div className="row"> | ||
<div className="col"> | ||
<div data-testid="social"> | ||
{social && social.map((s) => ( | ||
<Icon | ||
key={s.id} | ||
path={`social/${s.icon}`} | ||
/> | ||
))} | ||
</div> | ||
const Footer = () => { | ||
const { social }: FooterType = useSelector(getFooter); | ||
|
||
return ( | ||
<footer className="footer"> | ||
<div className="container"> | ||
<div className="footer__top"> | ||
<div className="row"> | ||
<div className="col"> | ||
<div data-testid="social"> | ||
{social && social.map((s) => ( | ||
<Icon | ||
key={s.id} | ||
path={`social/${s.icon}`} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div className="footer__bottom"> | ||
<div className="footer__copyright"> | ||
<span data-testid="copyright">Copyright © {new Date().getFullYear()}. All Rights Reserved</span> | ||
</div> | ||
</div> | ||
<div className="footer__bottom"> | ||
<div className="footer__copyright"> | ||
<span data-testid="copyright"> | ||
Copyright © {new Date().getFullYear()}. All Rights Reserved | ||
</span> | ||
</div> | ||
</footer> | ||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = (state) => ({ | ||
footer: state.footer, | ||
}); | ||
|
||
const mapDispatchToProps = { | ||
footerData, | ||
</div> | ||
</footer> | ||
); | ||
}; | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(Footer); | ||
export default Footer; |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// @flow strict | ||
import React from 'react'; | ||
// $FlowFixMe | ||
import { cleanup } from '@testing-library/react'; | ||
|
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 |
---|---|---|
@@ -1,53 +1,15 @@ | ||
// @flow strict | ||
import React, { useState } from 'react'; | ||
import React from 'react'; | ||
|
||
import Icon from '@/components/base/Icon/Icon'; | ||
import Navigation from '@/components/core/Navigation/Navigation'; | ||
import { useLazy } from '@/hooks/useLazy'; | ||
import { UserMenu } from '@/components/core/UserMenu'; | ||
|
||
import './Header.scss'; | ||
|
||
const Header = () => { | ||
const [open, setOpen] = useState(false); | ||
|
||
const handleOpen = () => setOpen(!open); | ||
|
||
const Sidebar = useLazy( | ||
() => import(/* webpackChunkName: "Sidebar" */'@/components/shared/Sidebar/Sidebar'), | ||
open, | ||
); | ||
|
||
return ( | ||
<div className="header"> | ||
{Sidebar | ||
? ( | ||
<Sidebar | ||
title="Cart" | ||
side="right" | ||
isOpen={open} | ||
onClose={handleOpen} | ||
> | ||
<p className="lead"> | ||
You have nothing, let's shop! | ||
</p> | ||
</Sidebar>) | ||
: null} | ||
<Navigation /> | ||
<div className="header__user-menu"> | ||
<span | ||
tabIndex="-1" | ||
role="button" | ||
onKeyPress={() => {}} | ||
onClick={handleOpen} | ||
> | ||
<Icon | ||
size="20" | ||
path="icons/cart" | ||
/> | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const Header = () => ( | ||
<div className="header"> | ||
<Navigation /> | ||
<UserMenu /> | ||
</div> | ||
); | ||
export default Header; |
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 @@ | ||
// @flow strict | ||
import React, { useState } from 'react'; | ||
// $FlowFixMe | ||
import { useSelector } from 'react-redux'; | ||
|
||
import Icon from '@/components/base/Icon/Icon'; | ||
import { useLazy } from '@/hooks/useLazy'; | ||
import { getCart } from '@/store/reducers/cart'; | ||
|
||
import type { CartType } from '@/store/types/CartType'; | ||
|
||
import './UserMenu.scss'; | ||
|
||
const UserMenu = () => { | ||
const [open, setOpen] = useState(false); | ||
const cart: CartType = useSelector(getCart); | ||
|
||
const handleOpen = () => setOpen(!open); | ||
|
||
const Sidebar = useLazy( | ||
() => import(/* webpackChunkName: "Sidebar" */'@/components/shared/Sidebar/Sidebar'), | ||
open, | ||
); | ||
|
||
return ( | ||
<div className="user-menu"> | ||
{Sidebar | ||
? ( | ||
<Sidebar | ||
title="Cart" | ||
side="right" | ||
isOpen={open} | ||
onClose={handleOpen} | ||
> | ||
<p className="lead"> | ||
You have nothing, let's shop! | ||
</p> | ||
</Sidebar>) | ||
: null} | ||
<span | ||
className="user-menu__cart-icon" | ||
tabIndex="-1" | ||
role="button" | ||
onKeyPress={() => {}} | ||
onClick={handleOpen} | ||
> | ||
<Icon | ||
size="20" | ||
path="icons/cart" | ||
/> | ||
<span className="user-menu__cart-quantity"> | ||
({ cart.quantity }) | ||
</span> | ||
</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default UserMenu; |
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,18 @@ | ||
@import '~styles/functions/px-to-rem'; | ||
@import '~styles/mixins'; | ||
@import '~styles/variables'; | ||
|
||
.user-menu { | ||
align-items: center; | ||
display: flex; | ||
padding: 0 px-to-rem(16); | ||
|
||
@include element(cart-icon) { | ||
align-items: center; | ||
display: flex; | ||
} | ||
|
||
@include element(cart-quantity) { | ||
margin-left: px-to-rem(5); | ||
} | ||
} |
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,53 @@ | ||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
// $FlowFixMe | ||
import { fireEvent } from '@testing-library/react'; | ||
|
||
import renderWithRedux from '@/utils/testing/renderWithRedux'; | ||
|
||
import UserMenu from './UserMenu'; | ||
|
||
describe('UserMenu', () => { | ||
it('should toggle <Sidebar /> when clicking icon', async () => { | ||
let testRenderer = {}; | ||
|
||
await act(async () => { | ||
testRenderer = renderWithRedux(<UserMenu />); | ||
}); | ||
|
||
const { queryByRole, queryByTestId } = testRenderer; | ||
const button = queryByRole('button'); | ||
const sidebar = queryByTestId('sidebar'); | ||
|
||
expect(sidebar).toBe(null); | ||
|
||
await act(async () => { | ||
fireEvent.click(button); | ||
}); | ||
|
||
const sidebarOpened = queryByTestId('sidebar'); | ||
|
||
expect(sidebarOpened).not.toBe(null); | ||
|
||
await act(async () => { | ||
fireEvent.click(button); | ||
}); | ||
|
||
const sidebarClosed = queryByTestId('sidebar'); | ||
|
||
expect(sidebarClosed).toBe(null); | ||
}); | ||
|
||
it('should display cart\'s quantity', async () => { | ||
let testRenderer = {}; | ||
|
||
await act(async () => { | ||
testRenderer = renderWithRedux(<UserMenu />); | ||
}); | ||
|
||
const { queryByRole } = testRenderer; | ||
const button = queryByRole('button'); | ||
|
||
expect(button.textContent).toBe('(0)'); | ||
}); | ||
}); |
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,3 @@ | ||
// @flow strict | ||
// eslint-disable-next-line import/prefer-default-export | ||
export { default as UserMenu } from './UserMenu'; |
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
Oops, something went wrong.