-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Features/error boundary #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
.a6y-react-auth__error-boundary, .a6y-react-auth__error-boundary * { | ||
font-size: 12px; | ||
letter-spacing: normal; | ||
line-height: 16px; | ||
font-weight: normal; | ||
} | ||
|
||
.a6y-react-auth__error-boundary--primary { | ||
padding: 10px 15px; | ||
} | ||
|
||
.a6y-react-auth__error-boundary--primary.a6y-react-auth__error-boundary--warning, | ||
.a6y-react-auth__error-boundary--primary.a6y-react-auth__error-boundary--warning * { | ||
color: #D97706; | ||
background-color: #FEF3C7; | ||
border: 1px solid #D97706; | ||
} | ||
|
||
.a6y-react-auth__error-boundary--primary.a6y-react-auth__error-boundary--error, | ||
.a6y-react-auth__error-boundary--primary.a6y-react-auth__error-boundary--error * { | ||
color: #DC2626; | ||
background-color: #FEE2E2; | ||
border: 1px solid #DC2626; | ||
} | ||
|
||
.a6y-react-auth__error-boundary--secondary.a6y-react-auth__error-boundary--warning, | ||
.a6y-react-auth__error-boundary--secondary.a6y-react-auth__error-boundary--warning * { | ||
color: #D97706; | ||
} | ||
|
||
.a6y-react-auth__error-boundary--secondary.a6y-react-auth__error-boundary--error, | ||
.a6y-react-auth__error-boundary--secondary.a6y-react-auth__error-boundary--error * { | ||
color: #DC2626; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react' | ||
import './ErrorBoundary.css' | ||
|
||
/** | ||
* @typedef IErrorBoundaryProps | ||
* | ||
* @props {string} [className] - the CSS classname | ||
* @props {boolean} showError - state which determines if an error message will be displayed | ||
* @props {React.ReactNode} children - the error message it can be string or react child with string | ||
* @props {string} [style] - the CSS style for error message 'primary' | 'secondary' | ||
* @props {string} [status] - the status of message 'warning' | 'error' | ||
*/ | ||
|
||
export interface IErrorBoundaryProps { | ||
className?: string | ||
showError: boolean | ||
children: React.ReactNode | ||
style?: 'primary' | 'secondary' | ||
status?: 'warning' | 'error' | ||
} | ||
|
||
/** | ||
* Renders component with API error or Web App error messages | ||
* | ||
* @param {string} [className] - the CSS classname | ||
* @param {boolean} showError - state which determines if an error message will be displayed | ||
* @param {React.ReactNode} children - the error message it can be string or react child with string | ||
* @param {string} [style] - the CSS style for error message 'primary' | 'secondary' | ||
* @param {string} [status] - the status of message 'warning' | 'error' | ||
* | ||
* @example | ||
* <ErrorBoundary className='a6y-react-auth__error-boundary' showError={true}> | ||
* Error message or React child with message | ||
* </ErrorBoundary> | ||
*/ | ||
|
||
const ErrorBoundary = ({ | ||
className = 'a6y-react-auth__error-boundary', | ||
showError = false, | ||
children = 'Ups! Something were wrong', | ||
style = 'primary', | ||
status = 'error', | ||
}: IErrorBoundaryProps): JSX.Element => { | ||
const classNames = require('classnames') | ||
const ErrorBoundaryClass = classNames({ | ||
[`${className}--${style}`]: showError && style ? true : false, | ||
[`${className}--${status}`]: showError && status ? true : false, | ||
}) | ||
return ( | ||
<div className={className + ' ' + ErrorBoundaryClass}> | ||
{showError && children} | ||
</div> | ||
) | ||
} | ||
|
||
export default ErrorBoundary |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import Button from './Button/Button' | ||
import Input from './Input/Input' | ||
import Link from './Link/Link' | ||
import ErrorBoundary from './ErrorBoundary/ErrorBoundary' | ||
|
||
export { Button, Input, Link } | ||
export { Button, Input, Link, ErrorBoundary } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { ErrorBoundary } from '../../components/UI' | ||
|
||
const ErrorText = 'Error message' | ||
const ErrorChild = <div className='custom-class'>{ErrorText}</div> | ||
|
||
describe(`ErrorBoundary component test`, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will change it after merge because there is more file which needs to be changed |
||
it('should component rendered properly with status [false]', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will change it after merge because there is more file which needs to be changed |
||
const rendered = render( | ||
<ErrorBoundary showError={false}>{ErrorText}</ErrorBoundary>, | ||
) | ||
expect(rendered).toBeDefined | ||
}) | ||
it('should component rendered properly with status [true]', () => { | ||
const rendered = render( | ||
<ErrorBoundary showError={true}>{ErrorText}</ErrorBoundary>, | ||
) | ||
expect(rendered).toBeDefined | ||
}) | ||
it('should component rendered properly with status [true] and with H1 tag', () => { | ||
const rendered = render( | ||
<ErrorBoundary showError={true}> | ||
<h1>{ErrorText}</h1> | ||
</ErrorBoundary>, | ||
) | ||
expect(rendered).toBeDefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can try to find a For example:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will change it after merge because there is more file which needs to be changed |
||
}) | ||
it('should component rendered properly with status [true] and with react child', () => { | ||
const rendered = render( | ||
<ErrorBoundary showError={true}>{ErrorChild}</ErrorBoundary>, | ||
) | ||
expect(rendered).toBeDefined | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react' | ||
import { Meta } from '@storybook/react' | ||
import ErrorBoundary from '../../src/components/UI/ErrorBoundary/ErrorBoundary' | ||
|
||
export const ErrorBoundaryComponent: React.VFC<unknown> = args => ( | ||
<ErrorBoundary showError={false} {...args}> | ||
Error message | ||
</ErrorBoundary> | ||
) | ||
|
||
export default { | ||
title: 'Components/ErrorBoundary', | ||
component: ErrorBoundary, | ||
} as Meta |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The capitalized names should be used with components, containers, services or models names.
For variables, use:
errorText
anderrorChild
.The snake case, use with configurable variables, ie.
API_KEY=xxxx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will change it after merge because there is more file which needs to be changed