Skip to content
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

Merged
merged 4 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions src/components/SignIn/SignIn.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import EmailPasswordForm from '../EmailPasswordForm'
import FormLinks from '../FormLinks'
import { ErrorBoundary } from '../UI'
import './SignIn.css'

/**
Expand All @@ -25,18 +26,22 @@ export interface ISignInProps {
*
* @example
* <SignIn
* className='a6y-react-auth-sign-in-cmp'
* className='a6y-react-auth__sign-in'
* onClick={onClick}
* />
*/

const SignIn = ({
className = 'a6y-react-auth__sign-in',
onClick,
}: ISignInProps): JSX.Element => {
apiError,
}: Props): JSX.Element => {
return (
<div className={className}>
<EmailPasswordForm onClick={onClick} submitLabel='Sign In' />
<ErrorBoundary showError={apiError ? true : false}>
{apiError}
</ErrorBoundary>
<EmailPasswordForm onClick={onClick} />
<FormLinks />
</div>
)
Expand Down
12 changes: 8 additions & 4 deletions src/components/SignUp/SignUp.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import EmailPasswordForm from '../EmailPasswordForm'
import FormLinks from '../FormLinks'
import { ErrorBoundary } from '../UI'
import './SignUp.css'

/**
Expand All @@ -25,18 +26,21 @@ export interface ISignUpProps {
*
* @example
* <SignUp
* className='a6y-react-auth-sign-up-cmp'
* className='a6y-react-auth__sign-up'
* onClick={onClick}
* />
*/

const SignUp = ({
className = 'a6y-react-auth__sign-up',
onClick,
}: ISignUpProps): JSX.Element => {
apiError,
}: Props): JSX.Element => {
return (
<div className={className}>
<EmailPasswordForm onClick={onClick} submitLabel='Sign Up' />
<ErrorBoundary showError={apiError ? true : false}>
{apiError}
</ErrorBoundary>
<EmailPasswordForm />
<FormLinks />
</div>
)
Expand Down
34 changes: 34 additions & 0 deletions src/components/UI/ErrorBoundary/ErrorBoundary.css
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;
}
56 changes: 56 additions & 0 deletions src/components/UI/ErrorBoundary/ErrorBoundary.tsx
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
3 changes: 2 additions & 1 deletion src/components/UI/index.tsx
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 }
35 changes: 35 additions & 0 deletions src/tests/components/ErrorBoundary.test.tsx
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'
Copy link
Contributor

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 and errorChild.

The snake case, use with configurable variables, ie. API_KEY=xxxx

Copy link
Contributor Author

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

const ErrorChild = <div className='custom-class'>{ErrorText}</div>

describe(`ErrorBoundary component test`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename: Component:ErrorBoundary. Then we can use the same pattern with other tests, like:

Service:AuthService
Utilities:Validation

Copy link
Contributor Author

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

it('should component rendered properly with status [false]', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename: should render with status [false]

Copy link
Contributor Author

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

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
Copy link
Contributor

Choose a reason for hiding this comment

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

You can try to find a h1 element to make the test a bit more accurate.

For example:

const { container } = render(
      <ErrorBoundary showError={true}>
        <h1>{ErrorText}</h1>
      </ErrorBoundary>,
    )
    
    // Find by selector:
    const h1Element = container.querySelector('h1')
    expect(h1Element).toBeTruthy()
    
    // ...or find by text:
    const h1Element = container.getByText(ErrorText)
    expect(h1Element).toBeTruthy()

Copy link
Contributor Author

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

})
it('should component rendered properly with status [true] and with react child', () => {
const rendered = render(
<ErrorBoundary showError={true}>{ErrorChild}</ErrorBoundary>,
)
expect(rendered).toBeDefined
})
})
14 changes: 14 additions & 0 deletions stories/components/ErrorBoundary.stories.tsx
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