Skip to content

Commit c1f5e89

Browse files
authored
Merge pull request #869 from IQSS/866-improve-unit-coverage
Improve component/unit tests "Branches" coverage
2 parents 768110c + 808c989 commit c1f5e89

File tree

20 files changed

+949
-110
lines changed

20 files changed

+949
-110
lines changed

public/locales/en/dataset.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
"access": "Access",
250250
"needTwoVersions": "Please select two versions to view the differences.",
251251
"viewDetails": "View Details",
252-
"replace": "File Replace",
252+
"fileReplaced": "File Replaced",
253253
"termsOfUseandAccess": "Terms of Use/Access",
254254
"deaccessionedReason": "Deaccessioned Reason",
255255
"error": "Error on loading dataset versions"

src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,21 @@ export class DatasetJSDataverseRepository implements DatasetRepository {
411411
.get(
412412
`${DatasetJSDataverseRepository.DATAVERSE_BACKEND_URL}/api/datasets/${datasetId}/storageDriver`
413413
)
414-
.then((res: AxiosResponse<{ data: { message: string } }>) => {
415-
return res.data.data.message
416-
})
414+
.then(
415+
(
416+
res: AxiosResponse<{
417+
data: {
418+
name: string
419+
label: string
420+
type: string
421+
directDownload: boolean
422+
directUpload: boolean
423+
}
424+
}>
425+
) => {
426+
return res.data.data.name
427+
}
428+
)
417429
.catch(() => {
418430
return undefined
419431
})

src/sections/dataset/dataset-action-buttons/DatasetToolsOptions.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react'
1+
import { useRef, useState } from 'react'
22
import { toast } from 'react-toastify'
33
import { useTranslation } from 'react-i18next'
44
import { BarChartFill as BarChartFillIcon, GearFill } from 'react-bootstrap-icons'
@@ -59,10 +59,12 @@ const ToolOption = ({
5959
}: ToolOptionProps) => {
6060
const [isOpening, setIsOpening] = useState(false)
6161
const { t, i18n } = useTranslation('shared')
62+
const openingRef = useRef(false)
6263

6364
const handleClick = async () => {
6465
// If already opening, do nothing
65-
if (isOpening) return
66+
if (openingRef.current) return
67+
openingRef.current = true
6668
setIsOpening(true)
6769

6870
// Open a blank window immediately to avoid popup blockers
@@ -71,8 +73,8 @@ const ToolOption = ({
7173
// If the window didn't open, likely due to a popup blocker
7274
if (!newWindow || newWindow.closed || typeof newWindow.closed === 'undefined') {
7375
toast.info(t('allowPopups'))
76+
openingRef.current = false
7477
setIsOpening(false)
75-
newWindow?.close()
7678
return
7779
}
7880

@@ -94,6 +96,7 @@ const ToolOption = ({
9496
toast.error(t('externalToolOpeningFailed'))
9597
if (!newWindow?.closed) newWindow.close()
9698
} finally {
99+
openingRef.current = false
97100
setIsOpening(false)
98101
}
99102
}

src/sections/dataset/dataset-files/files-table/file-actions/file-actions-cell/file-action-buttons/file-options-menu/FileOptionsMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function FileOptionsMenu({ file, fileRepository, datasetRepository }: Fil
3030

3131
const datasetInfo: EditFilesMenuDatasetInfo = {
3232
persistentId: dataset.persistentId,
33-
releasedVersionExists: dataset.version.someDatasetVersionHasBeenReleased || false,
33+
releasedVersionExists: dataset.version.someDatasetVersionHasBeenReleased,
3434
versionNumber: dataset.version.number.toSearchParam(),
3535
requestAccess: dataset.termsOfUse?.termsOfAccess?.fileAccessRequest,
3636
termsOfAccessForRestrictedFiles:

src/sections/dataset/dataset-versions/view-difference/DatasetVersionsDifferenceTable.tsx

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export const DatasetVersionsDifferenceTable = ({
2525

2626
const citationMetadata = metadataChanges?.find((m) => m.blockName === 'Citation Metadata')
2727

28+
const areFilesAdded = filesAdded && filesAdded.length > 0
29+
const areFilesRemoved = filesRemoved && filesRemoved.length > 0
30+
const areFileChanges = fileChanges && fileChanges.length > 0
31+
2832
return (
2933
<div className={styles['dataset-versions-difference-table']}>
3034
<Table>
@@ -66,55 +70,57 @@ export const DatasetVersionsDifferenceTable = ({
6670
</Table>
6771
)}
6872

69-
{(filesAdded || filesRemoved || fileChanges || filesRemoved) && (
73+
{(areFilesAdded || areFilesRemoved || areFileChanges) && (
7074
<Table bordered>
7175
<thead>
7276
<tr>
7377
<th>{t('versions.files')}</th>
7478
</tr>
7579
</thead>
7680
<tbody>
77-
{filesRemoved?.map((file) => (
78-
<tr key={`removed-${file.fileId}`}>
79-
<td>
80-
{t('versions.fileID')} {file.fileId}
81-
<br />
82-
{t('versions.MD5')} {file.MD5}
83-
</td>
84-
<td>
85-
{t('versions.name')}: {file.fileName}
86-
<br />
87-
{t('versions.type')}: {file.type || ''}
88-
<br />
89-
{t('versions.description')}: {file.description || ''}
90-
<br />
91-
{t('versions.access')}: {file.isRestricted ? 'Restricted' : 'Public'}
92-
</td>
93-
<td></td>
94-
</tr>
95-
))}
96-
{filesAdded?.map((file) => (
97-
<tr key={`added-${file.fileId}`}>
98-
<td>
99-
{t('versions.fileID')} {file.fileId}
100-
<br />
101-
{t('versions.MD5')} {file.MD5}
102-
</td>
103-
<td></td>
104-
<td>
105-
{t('versions.name')}: {file.fileName}
106-
<br />
107-
{t('versions.type')}: {file.type || ''}
108-
<br />
109-
{t('versions.description')}: {file.description || ''}
110-
<br />
111-
{t('versions.access')}: {file.isRestricted ? 'Restricted' : 'Public'}
112-
</td>
113-
</tr>
114-
))}
115-
{fileChanges &&
81+
{areFilesRemoved &&
82+
filesRemoved.map((file) => (
83+
<tr key={`removed-${file.fileId}`} data-testid={`file-removed-row-${file.fileId}`}>
84+
<td>
85+
{t('versions.fileID')} {file.fileId}
86+
<br />
87+
{t('versions.MD5')} {file.MD5}
88+
</td>
89+
<td>
90+
{t('versions.name')}: {file.fileName}
91+
<br />
92+
{t('versions.type')}: {file.type || ''}
93+
<br />
94+
{t('versions.description')}: {file.description || ''}
95+
<br />
96+
{t('versions.access')}: {file.isRestricted ? 'Restricted' : 'Public'}
97+
</td>
98+
<td></td>
99+
</tr>
100+
))}
101+
{areFilesAdded &&
102+
filesAdded.map((file) => (
103+
<tr key={`added-${file.fileId}`} data-testid={`file-added-row-${file.fileId}`}>
104+
<td>
105+
{t('versions.fileID')} {file.fileId}
106+
<br />
107+
{t('versions.MD5')} {file.MD5}
108+
</td>
109+
<td></td>
110+
<td>
111+
{t('versions.name')}: {file.fileName}
112+
<br />
113+
{t('versions.type')}: {file.type || ''}
114+
<br />
115+
{t('versions.description')}: {file.description || ''}
116+
<br />
117+
{t('versions.access')}: {file.isRestricted ? 'Restricted' : 'Public'}
118+
</td>
119+
</tr>
120+
))}
121+
{areFileChanges &&
116122
fileChanges.map((file) => (
117-
<tr key={`changed-${file.fileId}`}>
123+
<tr key={`changed-${file.fileId}`} data-testid={`file-changed-row-${file.fileId}`}>
118124
<td>
119125
{t('versions.fileID')} {file.fileId}
120126
<br />
@@ -145,19 +151,19 @@ export const DatasetVersionsDifferenceTable = ({
145151
<tbody>
146152
{filesReplaced.map(({ oldFile, newFile }) => (
147153
<tr key={`replaced-${oldFile.fileId}-${newFile.fileId}`}>
148-
<td>{t('versions.replace')}</td>
154+
<td>{t('versions.fileReplaced')}</td>
149155
<td>
150-
{t('versions.name')}: {oldFile.fileName}
156+
{t('versions.fileID')} {oldFile.fileId}
151157
<br />
152-
{t('versions.type')}: {oldFile.type || ''}
153-
<br />
154-
{t('versions.description')}: {oldFile.description || ''}
158+
{t('versions.MD5')} {oldFile.MD5}
155159
<br />
160+
{t('versions.name')}: {oldFile.fileName}
156161
</td>
157162
<td>
158163
{t('versions.fileID')} {newFile.fileId}
159164
<br />
160-
{t('versions.MD5')} {newFile.MD5} <br />
165+
{t('versions.MD5')} {newFile.MD5}
166+
<br />
161167
{t('versions.name')}: {newFile.fileName}
162168
</td>
163169
</tr>

src/sections/edit-collection-featured-items/featured-items-form/featured-item-field/dv-object-form-item/DvObjectFormItem.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import styles from './DvObjectFormItem.module.scss'
77

88
// This is only to avoid difference snapshots in Chromatic builds, the real display origin will be the current window location
99
const locationOrigin =
10-
import.meta.env.STORYBOOK_CHROMATIC_BUILD === 'true' ? 'https://foo.com' : window.location.origin
10+
import.meta.env.STORYBOOK_CHROMATIC_BUILD === 'true'
11+
? /* istanbul ignore next */ 'https://foo.com'
12+
: window.location.origin
1113

1214
const BASENAME_URL = import.meta.env.BASE_URL ?? ''
1315

src/sections/file/FilesContext.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@ export const FilesContext = createContext<FilesContextProps>({
1313
refreshFiles: async () => {}
1414
})
1515

16-
export const useFilesContext = () => useContext(FilesContext)
16+
export const useFilesContext = () => {
17+
const context = useContext(FilesContext)
18+
if (!context) {
19+
/* istanbul ignore next */ throw new Error(
20+
'useFilesContext must be used within a FilesContext Provider'
21+
)
22+
}
23+
return context
24+
}

src/sections/file/file-action-buttons/access-file-menu/FileToolOptions.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react'
1+
import { useRef, useState } from 'react'
22
import { toast } from 'react-toastify'
33
import { useTranslation } from 'react-i18next'
44
import {
@@ -83,10 +83,12 @@ const ToolOption = ({
8383
}: ToolOptionProps) => {
8484
const [isOpening, setIsOpening] = useState(false)
8585
const { t, i18n } = useTranslation('shared')
86+
const openingRef = useRef(false)
8687

8788
const handleClick = async () => {
8889
// If already opening, do nothing
89-
if (isOpening) return
90+
if (openingRef.current) return
91+
openingRef.current = true
9092
setIsOpening(true)
9193

9294
// Open a blank window immediately to avoid popup blockers
@@ -95,8 +97,8 @@ const ToolOption = ({
9597
// If the window didn't open, likely due to a popup blocker
9698
if (!newWindow || newWindow.closed || typeof newWindow.closed === 'undefined') {
9799
toast.info(t('allowPopups'))
100+
openingRef.current = false
98101
setIsOpening(false)
99-
newWindow?.close()
100102
return
101103
}
102104

@@ -117,6 +119,7 @@ const ToolOption = ({
117119
toast.error(t('externalToolOpeningFailed'))
118120
if (!newWindow?.closed) newWindow.close()
119121
} finally {
122+
openingRef.current = false
120123
setIsOpening(false)
121124
}
122125
}

src/sections/shared/pagination/PaginationControls.tsx

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,20 @@ import { PaginationInfo } from '../../../shared/pagination/domain/models/Paginat
66
import { useEffect, useState } from 'react'
77
import { FilePaginationInfo } from '../../../files/domain/models/FilePaginationInfo'
88
import { DatasetPaginationInfo } from '../../../dataset/domain/models/DatasetPaginationInfo'
9-
import { useSearchParams } from 'react-router-dom'
109

1110
interface PaginationProps {
1211
onPaginationInfoChange: (
1312
paginationInfo: PaginationInfo<DatasetPaginationInfo | FilePaginationInfo>
1413
) => void
1514
initialPaginationInfo: PaginationInfo<DatasetPaginationInfo | FilePaginationInfo>
1615
showPageSizeSelector?: boolean
17-
updateQueryParam?: boolean
1816
}
1917
const MINIMUM_NUMBER_OF_PAGES_TO_DISPLAY_PAGINATION = 2
2018
export function PaginationControls({
2119
onPaginationInfoChange,
2220
initialPaginationInfo,
23-
showPageSizeSelector = true,
24-
updateQueryParam = false
21+
showPageSizeSelector = true
2522
}: PaginationProps) {
26-
const [searchParams, setSearchParams] = useSearchParams()
2723
const [paginationInfo, setPaginationInfo] = useState<DatasetPaginationInfo | FilePaginationInfo>(
2824
initialPaginationInfo
2925
)
@@ -48,12 +44,6 @@ export function PaginationControls({
4844

4945
useEffect(() => {
5046
onPaginationInfoChange(paginationInfo)
51-
if (updateQueryParam) {
52-
if (searchParams.get('page') !== paginationInfo.page.toString()) {
53-
searchParams.set('page', paginationInfo.page.toString())
54-
setSearchParams(searchParams)
55-
}
56-
}
5747
// TODO: Not a priority as not used for inifinite scroll is used but the eslint disable should be removed and the dependency should be added
5848
// eslint-disable-next-line react-hooks/exhaustive-deps
5949
}, [paginationInfo.page])
@@ -64,19 +54,6 @@ export function PaginationControls({
6454
// eslint-disable-next-line react-hooks/exhaustive-deps
6555
}, [initialPaginationInfo.totalItems])
6656

67-
useEffect(() => {
68-
if (updateQueryParam) {
69-
if (searchParams.get('page') !== paginationInfo.page.toString()) {
70-
const page = searchParams.get('page') ? parseInt(searchParams.get('page') as string) : 1
71-
searchParams.set('page', page.toString())
72-
setSearchParams(searchParams, { replace: true })
73-
goToPage(page)
74-
}
75-
}
76-
// TODO: Not a priority as not used for inifinite scroll is used but the eslint disable should be removed and the dependency should be added
77-
// eslint-disable-next-line react-hooks/exhaustive-deps
78-
}, [searchParams])
79-
8057
if (paginationInfo.totalPages < MINIMUM_NUMBER_OF_PAGES_TO_DISPLAY_PAGINATION) {
8158
return <></>
8259
}

tests/component/externalTools/domain/models/ExternalToolsMother.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { ToolScope, ToolType } from '@/externalTools/domain/models/ExternalTool'
2-
import { ExternalTool } from '@iqss/dataverse-client-javascript'
1+
import { ExternalTool, ToolScope, ToolType } from '@/externalTools/domain/models/ExternalTool'
32

43
export class ExternalToolsMother {
54
static createList(props?: Partial<ExternalTool>): ExternalTool[] {

0 commit comments

Comments
 (0)