-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payment): add checkout welcome component
- Loading branch information
1 parent
bd9902c
commit 9ba4589
Showing
8 changed files
with
134 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@use '../../styles/variables'; | ||
@use '../../styles/theme'; | ||
|
||
.welcome { | ||
h2 { | ||
font-family: theme.$body-font-family; | ||
font-weight: 700; | ||
font-size: 24px; | ||
} | ||
|
||
p { | ||
font-family: theme.$body-font-family; | ||
} | ||
} |
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,38 @@ | ||
import React from 'react'; | ||
import { act, fireEvent, render } from '@testing-library/react'; | ||
|
||
import Welcome from './Welcome'; | ||
|
||
describe('<Welcome>', () => { | ||
test('renders and matches snapshot', () => { | ||
const { container } = render(<Welcome />); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
test('calls the onCloseButtonClick callback when clicking the close button', () => { | ||
const onCloseButtonClick = jest.fn(); | ||
const { getByText } = render(<Welcome onCloseButtonClick={onCloseButtonClick} />); | ||
|
||
fireEvent.click(getByText('checkout.start_watching')); | ||
|
||
expect(onCloseButtonClick).toBeCalled(); | ||
}); | ||
|
||
test('calls the onCloseButtonClick callback when clicking the close button', () => { | ||
jest.useFakeTimers(); | ||
const onCountdownCompleted = jest.fn(); | ||
|
||
render(<Welcome onCountdownCompleted={onCountdownCompleted} />); | ||
|
||
let i = 10; | ||
while(i--) { | ||
act(() => { | ||
jest.runAllTimers() | ||
}); | ||
} | ||
|
||
expect(onCountdownCompleted).toBeCalled(); | ||
jest.useRealTimers(); | ||
}); | ||
}); |
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 @@ | ||
import React from 'react'; | ||
|
||
import Button from '../Button/Button'; | ||
import useCountdown from '../../hooks/useCountdown'; | ||
import { ConfigStore } from '../../stores/ConfigStore'; | ||
|
||
import styles from './Welcome.module.scss'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
type Props = { | ||
onCloseButtonClick?: () => void; | ||
onCountdownCompleted?: () => void; | ||
}; | ||
|
||
const Welcome: React.FC<Props> = ({ onCloseButtonClick, onCountdownCompleted }) => { | ||
const { t } = useTranslation('account'); | ||
const config = ConfigStore.useState(s => s.config); | ||
const countdown = useCountdown(10, 1, onCountdownCompleted); | ||
|
||
return ( | ||
<div className={styles.welcome}> | ||
<h2>{t('checkout.welcome_title', { siteName: config.siteName })}</h2> | ||
<p>{t('checkout.welcome_description', { siteName: config.siteName })}</p> | ||
<Button variant="contained" color="primary" label={t('checkout.start_watching', { countdown })} onClick={onCloseButtonClick} size="large" fullWidth /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Welcome; |
23 changes: 23 additions & 0 deletions
23
src/components/Welcome/__snapshots__/Welcome.test.tsx.snap
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,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<Welcome> renders and matches snapshot 1`] = ` | ||
<div> | ||
<div | ||
class="welcome" | ||
> | ||
<h2> | ||
checkout.welcome_title | ||
</h2> | ||
<p> | ||
checkout.welcome_description | ||
</p> | ||
<button | ||
class="button primary fullWidth large" | ||
> | ||
<span> | ||
checkout.start_watching | ||
</span> | ||
</button> | ||
</div> | ||
</div> | ||
`; |
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,22 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
const useCountdown = (durationSeconds: number, intervalSeconds: number = 1, completeHandler?: () => void) => { | ||
const timerRef = useRef<number>(); | ||
const [countdown, setCountdown] = useState(durationSeconds); | ||
|
||
useEffect(() => { | ||
window.clearTimeout(timerRef.current); | ||
if (countdown === 0) { | ||
if (completeHandler) completeHandler(); | ||
return; | ||
} | ||
|
||
timerRef.current = window.setTimeout(() => { | ||
setCountdown(count => count - intervalSeconds); | ||
}, intervalSeconds * 1000); | ||
}, [countdown]); | ||
|
||
return countdown; | ||
}; | ||
|
||
export default useCountdown; |
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