-
Notifications
You must be signed in to change notification settings - Fork 69
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 #2196 from flacial/2181-add-querystatemessage-to-o…
…ur-components-library feat: Create QueryInfo component
- Loading branch information
Showing
9 changed files
with
487 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React from 'react' | ||
import QueryInfo from './' | ||
import { render, screen } from '@testing-library/react' | ||
|
||
// Imported to be able to use .toBeInTheDocument() | ||
import '@testing-library/jest-dom' | ||
|
||
describe('QueryInfo component', () => { | ||
it('should display successful state', () => { | ||
expect.assertions(1) | ||
|
||
render( | ||
<QueryInfo | ||
hide={false} | ||
data={{ | ||
name: 'noob' | ||
}} | ||
loading={false} | ||
error={''} | ||
texts={{ | ||
loading: 'Loading message...', | ||
data: 'Submitted successfully' | ||
}} | ||
/> | ||
) | ||
|
||
expect(screen.getByText('Submitted successfully')).toBeInTheDocument() | ||
}) | ||
|
||
it('should display loading state', () => { | ||
expect.assertions(1) | ||
|
||
render( | ||
<QueryInfo | ||
hide={false} | ||
data={{ | ||
name: 'noob' | ||
}} | ||
loading={true} | ||
error={''} | ||
texts={{ | ||
loading: 'Loading message...', | ||
data: 'Submitted successfully' | ||
}} | ||
/> | ||
) | ||
|
||
expect(screen.getByText('Loading message...')).toBeInTheDocument() | ||
}) | ||
|
||
it('should display error state', () => { | ||
expect.assertions(1) | ||
|
||
render( | ||
<QueryInfo | ||
hide={false} | ||
data={{ | ||
name: 'noob' | ||
}} | ||
loading={false} | ||
error={'Missing arguments'} | ||
texts={{ | ||
loading: 'Loading message...', | ||
data: 'Submitted successfully' | ||
}} | ||
/> | ||
) | ||
|
||
expect( | ||
screen.getByText('An error occurred. Please try again.') | ||
).toBeInTheDocument() | ||
}) | ||
|
||
it('should render nothing when there is no data, error, and loading', () => { | ||
expect.assertions(2) | ||
|
||
render( | ||
<QueryInfo | ||
hide={false} | ||
data={undefined} | ||
loading={false} | ||
error={''} | ||
texts={{ | ||
loading: 'Loading message...', | ||
data: 'Submitted successfully' | ||
}} | ||
/> | ||
) | ||
|
||
expect( | ||
screen.queryByText('Submitted the item successfully!') | ||
).not.toBeInTheDocument() | ||
expect(screen.queryByText('Loading message...')).not.toBeInTheDocument() | ||
}) | ||
}) |
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,67 @@ | ||
import { get } from 'lodash' | ||
import React from 'react' | ||
import { Spinner } from 'react-bootstrap' | ||
import styles from '../../scss/queryInfo.module.scss' | ||
import Alert from '../Alert' | ||
|
||
type QueryInfo<T> = { | ||
data: T | ||
loading: boolean | ||
error: string | ||
texts: { | ||
loading: string | ||
data: string | ||
} | ||
dismiss?: { | ||
onDismissError?: (id: number) => void | ||
onDismissData?: (id: number) => void | ||
} | ||
} | ||
|
||
const QueryInfo = <T,>({ | ||
data, | ||
loading, | ||
error, | ||
texts, | ||
dismiss | ||
}: QueryInfo<T>) => { | ||
if (loading) { | ||
return ( | ||
<div className={styles.loading}> | ||
<Spinner animation="grow" size="sm" /> | ||
<span>{texts.loading}</span> | ||
</div> | ||
) | ||
} | ||
|
||
if (error) { | ||
return ( | ||
<Alert | ||
alert={{ | ||
text: 'An error occurred. Please try again.', | ||
type: 'urgent', | ||
id: 1 | ||
}} | ||
onDismiss={get(dismiss, 'onDismissError')} | ||
key={error} | ||
/> | ||
) | ||
} | ||
|
||
if (data) { | ||
return ( | ||
<Alert | ||
alert={{ | ||
text: texts.data, | ||
type: 'info', | ||
id: 2 | ||
}} | ||
onDismiss={get(dismiss, 'onDismissData')} | ||
/> | ||
) | ||
} | ||
|
||
return <></> | ||
} | ||
|
||
export default QueryInfo |
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 @@ | ||
export { default } from './QueryInfo' |
Oops, something went wrong.
33c2467
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.
Successfully deployed to the following URLs:
c0d3-app – ./
c0d3-app.vercel.app
www.c0d3.com
c0d3-app-c0d3-prod.vercel.app
c0d3-app-git-master-c0d3-prod.vercel.app
v2.c0d3.app