Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1568 from yjyoo773/SWC-6120
Browse files Browse the repository at this point in the history
  • Loading branch information
yjyoo773 authored Jun 10, 2022
2 parents 14a1f9c + a1e770f commit 2f5c0f6
Show file tree
Hide file tree
Showing 10 changed files with 418 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Access Request Submission Table tests', () => {
const props = {
showSubmitter: true,
showStatus: true,
showRequestors: true,
showRequesters: true,
accessorId: accessorId,
accessRequirementId: accessRequirementId,
submissionState: submissionState,
Expand All @@ -127,7 +127,7 @@ describe('Access Request Submission Table tests', () => {
renderComponent({
showSubmitter: true,
showStatus: true,
showRequestors: true,
showRequesters: true,
})
await screen.findByRole('table')
await waitFor(() =>
Expand All @@ -140,7 +140,7 @@ describe('Access Request Submission Table tests', () => {
screen.getByRole('columnheader', { name: 'Access Requirement Name' })
screen.getByRole('columnheader', { name: 'Submitter' })
screen.getByRole('columnheader', { name: 'Status' })
screen.getByRole('columnheader', { name: 'Requestors' })
screen.getByRole('columnheader', { name: 'Requesters' })
screen.getByRole('columnheader', { name: 'Reviewer(s)' })
screen.getByRole('columnheader', { name: /^Created Date/ })

Expand Down Expand Up @@ -169,7 +169,7 @@ describe('Access Request Submission Table tests', () => {
renderComponent({
showSubmitter: true,
showStatus: true,
showRequestors: true,
showRequesters: true,
})
await screen.findByRole('table')
await waitFor(() =>
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Access Request Submission Table tests', () => {
renderComponent({
showSubmitter: true,
showStatus: true,
showRequestors: true,
showRequesters: true,
})

await waitFor(() => expect(onServiceRecievedRequest).toHaveBeenCalled())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { createMemoryHistory, MemoryHistory } from 'history'
import React from 'react'
import { Router } from 'react-router-dom'
import selectEvent from 'react-select-event'
import {
DataAccessSubmissionDashboard,
DataAccessSubmissionDashboardProps,
} from '../../../../lib/containers/dataaccess/AccessSubmissionDashboard'
import { createWrapper } from '../../../../lib/testutils/TestingLibraryUtils'
import { rest, server } from '../../../../mocks/msw/server'
import {
MOCK_USER_ID,
MOCK_USER_NAME,
} from '../../../../mocks/user/mock_user_profile'
import {
mockManagedACTAccessRequirement as mockAccessRequirement,
mockSearchResults,
} from '../../../../mocks/mockAccessRequirements'
import { getOptionLabel } from '../../../../lib/containers/dataaccess/AccessRequirementSearchBox'
import {
BackendDestinationEnum,
getEndpoint,
} from '../../../../lib/utils/functions/getEndpoint'
import {
ACCESS_REQUIREMENT_BY_ID,
ACCESS_REQUIREMENT_SEARCH,
} from '../../../../lib/utils/APIConstants'

const SUBMISSION_TABLE_TEST_ID = 'AccessSubmissionTableTestId'
const MOCK_AR_ID = '12321'

jest.mock('../../../../lib/containers/AccessRequestSubmissionTable', () => ({
AccessRequestSubmissionTable: jest.fn().mockImplementation(() => {
return <div data-testid={SUBMISSION_TABLE_TEST_ID}></div>
}),
}))

const {
AccessRequestSubmissionTable: mockAccessRequestSubmissionTable,
} = require('../../../../lib/containers/AccessRequestSubmissionTable')

const onServiceRecievedRequest = jest.fn()

function renderComponent(
props?: DataAccessSubmissionDashboardProps,
modifyHistory?: (history: MemoryHistory) => void,
) {
const history = createMemoryHistory()
if (modifyHistory) {
modifyHistory(history)
}
const renderResult = render(
<Router history={history}>
<DataAccessSubmissionDashboard {...props} />
</Router>,
{
wrapper: createWrapper(),
},
)
return { ...renderResult, history }
}

describe('AccessSubmissionDashboard tests', () => {
beforeAll(() => {
server.listen()

server.use(
rest.post(
`${getEndpoint(
BackendDestinationEnum.REPO_ENDPOINT,
)}${ACCESS_REQUIREMENT_SEARCH}`,

async (req, res, ctx) => {
onServiceRecievedRequest(req.body)
return res(ctx.status(200), ctx.json(mockSearchResults))
},
),
// Return an access requirement specified by ID
rest.get(
`${getEndpoint(
BackendDestinationEnum.REPO_ENDPOINT,
)}${ACCESS_REQUIREMENT_BY_ID(':id')}`,

async (req, res, ctx) => {
onServiceRecievedRequest(req.body)
return res(ctx.status(200), ctx.json(mockAccessRequirement))
},
),
)
})
afterEach(() => server.restoreHandlers())
afterAll(() => server.close())

it('Renders inputFields and the table component', async () => {
renderComponent()

expect(await screen.findAllByRole('textbox')).toHaveLength(3)
await screen.findByTestId(SUBMISSION_TABLE_TEST_ID)
expect(mockAccessRequestSubmissionTable).toHaveBeenCalledWith(
expect.objectContaining({
accessRequirementId: undefined,
accessorId: undefined,
reviewerId: undefined,
}),
expect.anything(),
)
})

it('Updates the passed props and URLSearchParams when updating arName', async () => {
const { history } = renderComponent()
const arNameInput = (await screen.findAllByRole('textbox'))[0]
userEvent.type(arNameInput, mockAccessRequirement.name)
selectEvent.select(
arNameInput,
getOptionLabel(mockAccessRequirement.id, mockAccessRequirement.name),
)

await waitFor(() =>
expect(
new URLSearchParams(history.location.search).get('accessRequirementId'),
).toEqual(mockAccessRequirement.id.toString()),
)

await waitFor(() =>
expect(mockAccessRequestSubmissionTable).toHaveBeenLastCalledWith(
expect.objectContaining({
accessRequirementId: mockAccessRequirement.id.toString(),
accessorId: undefined,
reviewerId: undefined,
}),
expect.anything(),
),
)
})

it('Updates the passed props and URLSearchParams when updating requesterId', async () => {
const { history } = renderComponent()
const requesterInput = (await screen.findAllByRole('textbox'))[1]
userEvent.type(requesterInput, MOCK_USER_NAME.substring(0, 1))
selectEvent.select(requesterInput, '@' + MOCK_USER_NAME)

await waitFor(() =>
expect(
new URLSearchParams(history.location.search).get('accessorId'),
).toEqual(MOCK_USER_ID.toString()),
)
expect(mockAccessRequestSubmissionTable).toHaveBeenCalledWith(
expect.objectContaining({
accessRequirementId: undefined,
accessorId: MOCK_USER_ID.toString(),
reviewerId: undefined,
}),
expect.anything(),
)
})

it('Updates the passed props and URLSearchParams when updating reviewerId', async () => {
const { history } = renderComponent()
const reviewerInput = (await screen.findAllByRole('textbox'))[2]
userEvent.type(reviewerInput, MOCK_USER_NAME.substring(0, 1))
selectEvent.select(reviewerInput, '@' + MOCK_USER_NAME)

await waitFor(() =>
expect(
new URLSearchParams(history.location.search).get('reviewerId'),
).toEqual(MOCK_USER_ID.toString()),
)
expect(mockAccessRequestSubmissionTable).toHaveBeenCalledWith(
expect.objectContaining({
accessRequirementId: undefined,
accessorId: undefined,
reviewerId: MOCK_USER_ID.toString(),
}),
expect.anything(),
)
})

it('Auto-fills the inputs with search parameter values', async () => {
renderComponent(undefined, history => {
const searchParams = new URLSearchParams('')
searchParams.set('accessRequirementId', MOCK_AR_ID.toString())
searchParams.set('accessorId', MOCK_USER_ID.toString())
searchParams.set('reviewerId', MOCK_USER_ID.toString())
history.push('?' + searchParams.toString())
})

await waitFor(() =>
expect(mockAccessRequestSubmissionTable).toHaveBeenLastCalledWith(
expect.objectContaining({
accessRequirementId: MOCK_AR_ID.toString(),
accessorId: MOCK_USER_ID.toString(),
reviewerId: MOCK_USER_ID.toString(),
}),
expect.anything(),
),
)
})
})
2 changes: 1 addition & 1 deletion src/lib/containers/AccessRequestSubmissionTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<AccessRequestSubmissionTable
showSubmitter={true}
showStatus={true}
showRequestors={true}
showRequesters={true}
/>
```
46 changes: 25 additions & 21 deletions src/lib/containers/AccessRequestSubmissionTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import { ACT_TEAM_ID, SMALL_USER_CARD } from '../utils/SynapseConstants'
import { PRODUCTION_ENDPOINT_CONFIG } from '../utils/functions/getEndpoint'
import { SynapseSpinner } from './LoadingScreen'
import UserCard from './UserCard'
import Typography from '../utils/typography/Typography'

export type AccessRequestSubmissionTableProps = {
showSubmitter: boolean
showStatus: boolean
showRequestors: boolean
showSubmitter?: boolean
showStatus?: boolean
showRequesters?: boolean
accessorId?: string
accessRequirementId?: string
reviewerId?: string
Expand All @@ -34,7 +35,7 @@ export const AccessRequestSubmissionTable: React.FunctionComponent<
> = ({
showSubmitter,
showStatus,
showRequestors,
showRequesters,
accessorId,
accessRequirementId,
reviewerId,
Expand Down Expand Up @@ -65,18 +66,18 @@ export const AccessRequestSubmissionTable: React.FunctionComponent<
],
)

const { data, hasNextPage, fetchNextPage, isFetching } =
const { data, hasNextPage, fetchNextPage, isLoading } =
useSearchAccessSubmissionsInfinite(searchRequest)

const accessSubmissions = data?.pages.flatMap(page => page.results) ?? []

const onSort = (field: SubmissionSortField) => {
if (sort.field === field) {
setSort({ field, direction: sort.direction === 'DESC' ? 'ASC' : 'DESC' })
} else {
setSort({ field, direction: 'DESC' })
}
}

return (
<div className="bootstrap-4-backport AccessSubmissionTable">
<Table striped borderless bordered={false}>
Expand All @@ -86,7 +87,7 @@ export const AccessRequestSubmissionTable: React.FunctionComponent<
<th>Access Requirement Name</th>
{showSubmitter && <th>Submitter</th>}
{showStatus && <th>Status</th>}
{showRequestors && <th>Requestors</th>}
{showRequesters && <th>Requesters</th>}
<th>Reviewer(s)</th>
<th>
Created Date
Expand Down Expand Up @@ -127,17 +128,17 @@ export const AccessRequestSubmissionTable: React.FunctionComponent<
{showStatus && (
<td>{upperFirst(item.state.toLocaleLowerCase())}</td>
)}
{showRequestors && (
{showRequesters && (
<td>
<UserOrTeamBadge principalId={item.submitterId} />
{item.accessorChanges
.filter(user => item.submitterId !== user.userId)
.map(requestor => (
<li key={requestor.userId}>
.map(requester => (
<li key={requester.userId}>
<UserCard
size={SMALL_USER_CARD}
ownerId={requestor.userId}
className="requestor"
ownerId={requester.userId}
className="requester"
/>
</li>
))}
Expand All @@ -161,17 +162,20 @@ export const AccessRequestSubmissionTable: React.FunctionComponent<
})}
</tbody>
</Table>
{isLoading && (
<div className="SRC-center-text">
<SynapseSpinner size={40} />
</div>
)}
{!isLoading && accessSubmissions.length == 0 && (
<Typography className="SRC-center-text" variant="body1">
No Results
</Typography>
)}
{!hasNextPage ? (
<></>
) : isFetching ? (
<SynapseSpinner size={40} />
''
) : (
<Button
variant="outline"
onClick={() => {
fetchNextPage()
}}
>
<Button variant="outline" onClick={() => fetchNextPage()}>
Show More
</Button>
)}
Expand Down
Loading

0 comments on commit 2f5c0f6

Please sign in to comment.