Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewBemis committed Feb 12, 2025
1 parent ee3ea56 commit 6b22745
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
79 changes: 76 additions & 3 deletions ui-participant/src/hub/documents/DocumentLibrary.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { asMockedFn, MockI18nProvider, setupRouterTest, mockParticipantFile } from '@juniper/ui-core'
import { usePortalEnv } from 'providers/PortalProvider'
import { mockUsePortalEnv } from 'test-utils/test-portal-factory'
import { render, screen, waitFor } from '@testing-library/react'
import { mockPortal, mockUsePortalEnv } from 'test-utils/test-portal-factory'
import { act, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import DocumentLibrary from './DocumentLibrary'
import { useActiveUser } from 'providers/ActiveUserProvider'
Expand All @@ -16,7 +16,8 @@ jest.mock('providers/ActiveUserProvider', () => ({
}))

jest.mock('api/api', () => ({
listParticipantFiles: jest.fn()
listParticipantFiles: jest.fn(),
getPortal: jest.fn()
}))

beforeEach(() => {
Expand Down Expand Up @@ -58,6 +59,11 @@ describe('DocumentLibrary', () => {
expect(screen.getByText('file1.pdf')).toBeInTheDocument()
})

await act(async () => {
screen.getByText('Options').click()
})

expect(screen.getByText('Delete')).toBeInTheDocument()
expect(screen.getByText('{documentDownloadButton}')).toBeInTheDocument()
})

Expand Down Expand Up @@ -106,4 +112,71 @@ describe('DocumentLibrary', () => {
//note: this is looking at the i18n key for the task name
expect(screen.getByText('{researchSurvey1:1}')).toBeInTheDocument()
})

it('allows deleting documents that dont have any associated answers', async () => {
asMockedFn(Api.listParticipantFiles).mockResolvedValue([
mockParticipantFile('file1.pdf')
])

asMockedFn(Api.getPortal).mockResolvedValue(mockPortal())

const { RoutedComponent } = setupRouterTest(
<MockI18nProvider><DocumentLibrary/></MockI18nProvider>
)
render(RoutedComponent)

await waitFor(() => {
expect(screen.getByText('file1.pdf')).toBeInTheDocument()
})

await act(async () => {
screen.getByText('Options').click()
})

await act(async () => {
screen.getByText('Delete').click()
})

await waitFor(() => {
expect(screen.queryByText('Are you sure you want to delete this document?', { exact: false })).toBeInTheDocument()
})
})

it('shows a warning message when trying to delete a document that has associated answers', async () => {
asMockedFn(Api.listParticipantFiles).mockResolvedValue([
mockParticipantFile('file1.pdf', [{
format: 'FILE_NAME',
surveyVersion: 1,
stringValue: 'file1.pdf',
questionStableId: 'question1',
surveyResponseId: 'taskId1'
}])
])

asMockedFn(Api.getPortal).mockResolvedValue(mockPortal())

const { RoutedComponent } = setupRouterTest(
<MockI18nProvider><DocumentLibrary/></MockI18nProvider>
)
render(RoutedComponent)

await waitFor(() => {
expect(screen.getByText('file1.pdf')).toBeInTheDocument()
})

await act(async () => {
screen.getByText('Options').click()
})

await act(async () => {
screen.getByText('Delete').click()
})

await waitFor(() => {
expect(screen.queryByText('' +
'This document is currently shared in response to at least one survey. ' +
'Please remove it from the survey response(s) before deleting it.')
).toBeInTheDocument()
})
})
})
4 changes: 3 additions & 1 deletion ui-participant/src/hub/documents/DocumentLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ const FileOptionsDropdown = ({ studyEnvParams, participantFile, enrollee, loadDo
onHide={() => setShowConfirmDelete(false)} size={'lg'} animation={true}>
<Modal.Header>
<Modal.Title>
<h2 className="fw-bold pb-0 mb-0">Are you sure?</h2>
<h2 className="fw-bold pb-0 mb-0">
{participantFile.associatedAnswers.length === 0 ? 'Are you sure?' : 'This document is in use'}
</h2>
</Modal.Title>
</Modal.Header>
<Modal.Body>
Expand Down
3 changes: 2 additions & 1 deletion ui-participant/src/test-utils/test-portal-factory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export const mockLocalSiteContent = (): LocalSiteContent => {
landingPage: mockHtmlPage(),
navLogoCleanFileName: 'navLogo.png',
navLogoVersion: 1,
languageTextOverrides: []
languageTextOverrides: [],
primaryBrandColor: '#000000'
}
}

Expand Down

0 comments on commit 6b22745

Please sign in to comment.