Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/extension-chakra-storefront/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ module.exports = {
createTestGlob('pages/home'),
createTestGlob('pages/cart'),
createTestGlob('pages/product-list'),
createTestGlob('components/offline-banner'),
createTestGlob('components/offline-boundary'),
// createTestGlob('pages/login'), // TODO: enable after Account page has been migrated
createTestGlob('pages/login-redirect'),
createTestGlob('pages/social-login-redirect'),
Expand Down Expand Up @@ -95,6 +97,7 @@ module.exports = {
'non-pwa/**/*.{js,jsx}',
'worker/**/*.{js,jsx}',
'scripts/generator/*.{js,jsx}',
'src/**/*.{js,jsx}',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add unit test coverage report for extension-chakra-storefront folder

'!app/pages/test-container/**/*.{js,jsx}',
'!app/utils/test-utils.js',
'!app/mocks/*.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react'
import {useIntl} from 'react-intl'

// Components
import {Alert, AlertDescription, Flex} from '@chakra-ui/react'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In chakra v3, everything imported from Alert alone, AlertDescription is not needed anymore

import {Alert, Flex} from '@chakra-ui/react'

// Icons
import {AlertIcon} from '../../components/icons'
Expand All @@ -20,17 +20,17 @@ import {AlertIcon} from '../../components/icons'
const OfflineBanner = ({...props}) => {
const intl = useIntl()
return (
<Alert status="warning" {...props}>
<Alert.Root status="warning" colorPalette="blue" {...props}>
<Flex align="center">
<AlertIcon mr={2} />
<AlertDescription>
<Alert.Title>
{intl.formatMessage({
id: 'offline_banner.description.browsing_offline_mode',
defaultMessage: "You're currently browsing in offline mode"
})}
</AlertDescription>
</Alert.Title>
</Flex>
</Alert>
</Alert.Root>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,13 @@ class OfflineBoundary extends React.Component {
return (
<React.Fragment>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every component in offline-boundary is v3 compatible already.
Removed legacy classnames

{chunkLoadError ? (
<div className="c-offline-boundary u-direction-column u-text-align-center u-padding-top u-padding-bottom">
<div>
<AlertIcon />

<h1 className="u-margin-bottom-md u-text-family">
You are currently offline
</h1>

<p className="u-margin-bottom-lg">
<h1>You are currently offline</h1>
<p>
{"We couldn't load the next page on this connection. Please try again."}
</p>

<Button
className="u-width-block-full pw--primary qa-retry-button"
onClick={() => this.clearError()}
>
Retry Connection
</Button>
<Button onClick={() => this.clearError()}>Retry Connection</Button>
</div>
) : (
children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
*/
import React from 'react'
import {screen} from '@testing-library/react'
// import userEvent from '@testing-library/user-event'
import userEvent from '@testing-library/user-event'

import OfflineBoundary from '../../components/offline-boundary/index'
import {renderWithRouter} from '../../utils/test-utils'
import OfflineBoundary, {UnwrappedOfflineBoundary} from '../../components/offline-boundary/index'
import {renderWithProviders} from '../../utils/test-utils'

// class ChunkLoadError extends Error {
// constructor(...params) {
// // Pass remaining arguments (including vendor specific ones) to parent constructor
// super(...params)
// this.name = 'ChunkLoadError'
// }
// }
class ChunkLoadError extends Error {
constructor(...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params)
this.name = 'ChunkLoadError'
}
}

describe('The OfflineBoundary', () => {
beforeEach(() => {
Expand All @@ -31,7 +31,7 @@ describe('The OfflineBoundary', () => {
})

test('should render its children', () => {
renderWithRouter(
renderWithProviders(
<OfflineBoundary isOnline={true}>
<div id="child">child</div>
</OfflineBoundary>
Expand All @@ -40,84 +40,70 @@ describe('The OfflineBoundary', () => {
expect(screen.getByText(/child/i)).toBeInTheDocument()
})

// TODO: Fix flaky/broken test
// eslint-disable-next-line jest/no-commented-out-tests
// test('should render the error splash when a child throws a chunk load error', () => {
// const ThrowingComponent = () => {
// throw new ChunkLoadError()
// }
// renderWithRouter(
// <OfflineBoundary isOnline={true}>
// <div>
// <ThrowingComponent />
// <div id="child">child</div>
// </div>
// </OfflineBoundary>
// )
test('should render the error splash when a child throws a chunk load error', () => {
const ThrowingComponent = () => {
throw new ChunkLoadError()
}
renderWithProviders(
<OfflineBoundary isOnline={true}>
<div>
<ThrowingComponent />
<div id="child">child</div>
</div>
</OfflineBoundary>
)

// expect(screen.getByRole('img', {name: /offline cloud/i})).toBeInTheDocument()
// expect(
// screen.getByRole('heading', {name: /you are currently offline/i})
// ).toBeInTheDocument()
// expect(screen.queryByText(/child/i)).not.toBeInTheDocument()
// })
expect(screen.getByRole('img', {hidden: true})).toBeInTheDocument()
expect(
screen.getByRole('heading', {name: /you are currently offline/i})
).toBeInTheDocument()
expect(screen.queryByText(/child/i)).not.toBeInTheDocument()
})

// TODO: Fix flaky/broken test
// eslint-disable-next-line jest/no-commented-out-tests
// test('should re-throw errors that are not chunk load errors', () => {
// const ThrowingComponent = () => {
// throw new Error('Anything else')
// }
// expect(() => {
// renderWithRouter(
// <OfflineBoundary isOnline={true}>
// <div>
// <ThrowingComponent />
// <div id="child">child</div>
// </div>
// </OfflineBoundary>
// )
// }).toThrow()
// })
test('should re-throw errors that are not chunk load errors', () => {
const ThrowingComponent = () => {
throw new Error('Anything else')
}
expect(() => {
renderWithProviders(
<OfflineBoundary isOnline={true}>
<div>
<ThrowingComponent />
<div id="child">child</div>
</div>
</OfflineBoundary>
)
}).toThrow()
})

// TODO: Fix flaky/broken test
// eslint-disable-next-line jest/no-commented-out-tests
// test('should attempt to reload the page when the user clicks retry', () => {
// let firstRender = true
// const ThrowingOnceComponent = () => {
// if (firstRender) {
// firstRender = false
// throw new ChunkLoadError()
// } else {
// return <div id="child">child</div>
// }
// }
// renderWithRouter(
// <OfflineBoundary isOnline={true}>
// <ThrowingOnceComponent />
// </OfflineBoundary>
// )
test('should call clearError when retry button is clicked', async () => {
const user = userEvent.setup()
const ThrowingComponent = () => {
throw new ChunkLoadError()
}
const clearErrorSpy = jest.spyOn(UnwrappedOfflineBoundary.prototype, 'clearError')

// expect(screen.getByRole('img', {name: /offline cloud/i})).toBeInTheDocument()
// expect(
// screen.getByRole('heading', {name: /you are currently offline/i})
// ).toBeInTheDocument()
// expect(screen.queryByText(/child/i)).not.toBeInTheDocument()
renderWithProviders(
<OfflineBoundary isOnline={true}>
<ThrowingComponent />
</OfflineBoundary>
)

expect(screen.getByRole('img', {hidden: true})).toBeInTheDocument()
expect(
screen.getByRole('heading', {name: /you are currently offline/i})
).toBeInTheDocument()

await user.click(screen.getByRole('button', {name: /retry connection/i}))
expect(clearErrorSpy).toHaveBeenCalled()

// userEvent.click(screen.getByRole('button', {name: /retry connection/i}))
// expect(screen.getByText(/child/i)).toBeInTheDocument()
// expect(screen.queryByRole('img', {name: /offline cloud/i})).not.toBeInTheDocument()
// expect(
// screen.queryByRole('heading', {name: /you are currently offline/i})
// ).not.toBeInTheDocument()
// })
clearErrorSpy.mockRestore()
})

// TODO: Fix flaky/broken test
// eslint-disable-next-line jest/no-commented-out-tests
// test('should derive state from a chunk load error', () => {
// const derived = UnwrappedOfflineBoundary.getDerivedStateFromError(
// new ChunkLoadError('test')
// )
// expect(derived).toEqual({chunkLoadError: true})
// })
test('should derive state from a chunk load error', () => {
const derived = UnwrappedOfflineBoundary.getDerivedStateFromError(
new ChunkLoadError('test')
)
expect(derived).toEqual({chunkLoadError: true})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import CheckoutHeader from '../../pages/checkout/partials/checkout-header'
import CheckoutFooter from '../../pages/checkout/partials/checkout-footer'
import Footer from '../footer'
import Header from '../header'
// import OfflineBanner from '../offline-banner'
// import OfflineBoundary from '../offline-boundary'
import OfflineBanner from '../offline-banner'
import OfflineBoundary from '../offline-boundary'
import Seo from '../seo'
import ScrollToTop from '../scroll-to-top'

Expand Down Expand Up @@ -274,7 +274,6 @@ const withLayout = <P extends object>(WrappedComponent: React.ComponentType<P>)
</Seo>

<ScrollToTop />

<Box id="app" display="flex" flexDirection="column" flex={1}>
{/*TODO: recreating this component because @chakra-ui/skip-nav does not have V3 version*/}
{/*<SkipNavLink zIndex="skipLink">Skip to Content</SkipNavLink>*/}
Expand Down Expand Up @@ -314,7 +313,7 @@ const withLayout = <P extends object>(WrappedComponent: React.ComponentType<P>)
<CheckoutHeader />
)}
</Box>
{/*{!isOnline && <OfflineBanner />}*/}
{!isOnline && <OfflineBanner />}
<AddToCartModalProvider>
{/*TODO: recreating this component because @chakra-ui/skip-nav does not have V3 version*/}
{/*<SkipNavContent*/}
Expand All @@ -333,10 +332,9 @@ const withLayout = <P extends object>(WrappedComponent: React.ComponentType<P>)
flexDirection="column"
flex="1"
>
{/*<OfflineBoundary isOnline={false}>*/}

<WrappedComponent {...(props as P)} />
{/*</OfflineBoundary>*/}
<OfflineBoundary isOnline={isOnline}>
<WrappedComponent {...(props as P)} />
</OfflineBoundary>
</Box>
{/*</SkipNavContent>*/}

Expand Down
Loading