forked from garageScript/c0d3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
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 garageScript#2196 from flacial/2181-add-querystate…
…message-to-our-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.