This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
418 additions
and
31 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
200 changes: 200 additions & 0 deletions
200
src/__tests__/lib/containers/dataaccess/AccessSubmissionDashboard.test.tsx
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,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(), | ||
), | ||
) | ||
}) | ||
}) |
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
Oops, something went wrong.