Skip to content

Commit

Permalink
Merge pull request #2196 from flacial/2181-add-querystatemessage-to-o…
Browse files Browse the repository at this point in the history
…ur-components-library

feat: Create QueryInfo component
  • Loading branch information
flacial authored Sep 3, 2022
2 parents c9f5205 + eda5b8e commit 33c2467
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 86 deletions.
117 changes: 117 additions & 0 deletions __tests__/__snapshots__/storyshots.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,122 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Component/QueryInfo Basic 1`] = `
<div
className="fade alert alert-primary show"
role="alert"
>
<div
className="d-flex align-items-center gap-3"
>
<div
className="info_image"
>
<img
className="img-fluid"
height={24}
src="/assets/curriculum/icons/icon-tip.svg"
width={24}
/>
</div>
Update the module successfully!
</div>
</div>
`;

exports[`Storyshots Component/QueryInfo Basic With Dismiss 1`] = `
<div
className="fade alert alert-primary alert-dismissible show"
role="alert"
>
<button
aria-label="Close alert"
className="btn-close"
onClick={[Function]}
type="button"
/>
<div
className="d-flex align-items-center gap-3"
>
<div
className="info_image"
>
<img
className="img-fluid"
height={24}
src="/assets/curriculum/icons/icon-tip.svg"
width={24}
/>
</div>
Update the module successfully!
</div>
</div>
`;

exports[`Storyshots Component/QueryInfo Error 1`] = `
<div
className="fade alert alert-danger show"
role="alert"
>
<div
className="d-flex align-items-center gap-3"
>
<div
className=""
>
<img
className="img-fluid"
height={24}
src="/assets/curriculum/icons/exclamation.svg"
width={24}
/>
</div>
An error occurred. Please try again.
</div>
</div>
`;

exports[`Storyshots Component/QueryInfo Error With Dismiss 1`] = `
<div
className="fade alert alert-danger alert-dismissible show"
role="alert"
>
<button
aria-label="Close alert"
className="btn-close"
onClick={[Function]}
type="button"
/>
<div
className="d-flex align-items-center gap-3"
>
<div
className=""
>
<img
className="img-fluid"
height={24}
src="/assets/curriculum/icons/exclamation.svg"
width={24}
/>
</div>
An error occurred. Please try again.
</div>
</div>
`;

exports[`Storyshots Component/QueryInfo With Loading 1`] = `
<div
className="loading"
>
<div
className="spinner-grow spinner-grow-sm"
/>
<span>
Sending the request...
</span>
</div>
`;

exports[`Storyshots Components/AdditionalResources Basic 1`] = `
<div
className="additional-resources__container ms-4 d-flex flex-column justify-content-around"
Expand Down
95 changes: 95 additions & 0 deletions components/QueryInfo/QueryInfo.test.js
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()
})
})
67 changes: 67 additions & 0 deletions components/QueryInfo/QueryInfo.tsx
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
1 change: 1 addition & 0 deletions components/QueryInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './QueryInfo'
Loading

1 comment on commit 33c2467

@vercel
Copy link

@vercel vercel bot commented on 33c2467 Sep 3, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.