Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const addDocumentToList = (doc: File) => {

const removeDocumentFromList = (uiDoc: UiDocument) => {
const index = documentList.value.findIndex(item => uiDoc.id === item.id)
documentList.value.splice(index, 1)
if (index !== -1) {
documentList.value.splice(index, 1)
}
}

const cancelDocumentsUpload = () => {
Expand Down
2 changes: 1 addition & 1 deletion strr-base-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "strr-base-web",
"private": true,
"type": "module",
"version": "0.0.63",
"version": "0.0.64",
"engines": {
"node": ">=24"
},
Expand Down
4 changes: 2 additions & 2 deletions strr-examiner-web/app/stores/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const useExaminerDocumentStore = defineStore('examiner/document', () => {
*
* @param {UiDocument} uiDoc - The document to upload, containing file data and metadata.
* @param {number} registrationId - The registration ID to which the document is being added.
* @returns {Promise<void>} A promise that resolves when the document has been added or rejects if an error occurs.
* @returns {Promise<void>} Always resolves; upload errors are shown in an error modal instead of being thrown.
*/
async function addDocumentToRegistration (uiDoc: UiDocument, registrationId: number): Promise<void> {
try {
Expand Down Expand Up @@ -140,7 +140,7 @@ export const useExaminerDocumentStore = defineStore('examiner/document', () => {
*
* @param {UiDocument} uiDoc - The document to upload, containing file data and metadata.
* @param {string} applicationNumber - The application number to which the document is being added.
* @returns {Promise<void>} A promise that resolves when the document has been added or rejects if an error occurs.
* @returns {Promise<void>} Always resolves; upload errors are shown in an error modal instead of being thrown.
*/
async function addDocumentToApplication (uiDoc: UiDocument, applicationNumber: string): Promise<void> {
try {
Expand Down
2 changes: 1 addition & 1 deletion strr-examiner-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "strr-examiner-web",
"private": true,
"type": "module",
"version": "0.2.72",
"version": "0.2.73",
"engines": {
"node": ">=24"
},
Expand Down
26 changes: 26 additions & 0 deletions strr-examiner-web/tests/unit/store-document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ describe('Document Store', () => {
expect(exStore.activeReg!.documents).toHaveLength(mockDocuments.length) // no doc appended
})

it('should show error modal and resolve when adding document to application fails', async () => {
const exStore = useExaminerStore()
exStore.activeRecord = { ...mockHostRegistration, documents: [...mockDocuments] }
const recordBefore = exStore.activeRecord

const docStore = useExaminerDocumentStore()
mockStrrApi.mockRejectedValueOnce(new Error('Upload failed'))

const uiDoc = {
file: new File(['content'], 'utility-bill.pdf', { type: 'application/pdf' }),
type: DocumentUploadType.UTILITY_BILL,
loading: false
} as UiDocument

await expect(docStore.addDocumentToApplication(uiDoc, 'APP12345')).resolves.toBeUndefined()

expect(mockOpenErrorModal).toHaveBeenCalledWith(
'error.docUpload.generic.title',
'error.docUpload.generic.description',
false
)
expect(uiDoc.loading).toBe(false)
expect(exStore.activeRecord).toBe(recordBefore) // record not replaced
expect(exStore.activeReg!.documents).toHaveLength(mockDocuments.length)
})

it('should append document to activeReg when adding document to registration', async () => {
const exStore = useExaminerStore()
exStore.activeRecord = { ...mockHostRegistration, documents: [...mockDocuments] }
Expand Down
4 changes: 2 additions & 2 deletions strr-host-pm-web/app/stores/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ export const useDocumentStore = defineStore('host/document', () => {
// Remove document from local list to ensure createApplicationBody uses updated list
const index = storedDocuments.value.findIndex(item => uiDoc.id === item.id)
if (index === -1) {
return // not in the list (e.g. upload failed before it was added), nothing to remove
// already removed (e.g. double click) or never added - splice(-1, 1) would drop the last document
return
}
Comment thread
severinbeauvais marked this conversation as resolved.
Resolved
storedDocuments.value.splice(index, 1)

Expand Down Expand Up @@ -436,7 +437,6 @@ export const useDocumentStore = defineStore('host/document', () => {
} catch (e) {
logFetchError(e, 'Error uploading document')
strrModal.openErrorModal(t('error.docUpload.generic.title'), t('error.docUpload.generic.description'), false)
await removeStoredDocument(uiDoc)
} finally {
// cleanup loading on ui object
uiDoc.loading = false
Expand Down
2 changes: 1 addition & 1 deletion strr-host-pm-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "strr-host-pm-web",
"private": true,
"type": "module",
"version": "1.3.38",
"version": "1.3.39",
"engines": {
"node": ">=24"
},
Expand Down
15 changes: 15 additions & 0 deletions strr-host-pm-web/tests/unit/store-document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,21 @@ describe('Document Removal', () => {
expect(mockSubmitApplication).not.toHaveBeenCalled()
})

it('should leave the list alone and not save or delete when the document is not in it', async () => {
const store = useDocumentStore()
const docA = makeUiDoc('1', DocumentUploadType.BC_DRIVERS_LICENSE)
const docB = makeUiDoc('2', DocumentUploadType.UTILITY_BILL)
store.storedDocuments = [docA, docB]
mockPermitStore.application = { header: { status: 'draft', applicationNumber: 'APP-123' } }

// e.g. second click on remove, or a doc removed while its upload was still running
await store.removeStoredDocument(makeUiDoc('3', DocumentUploadType.UTILITY_BILL))

expect(store.storedDocuments.map(d => d.id)).toEqual(['1', '2'])
expect(mockSubmitApplication).not.toHaveBeenCalled()
expect(mockStrrApi).not.toHaveBeenCalled()
})

it('should not remove any document if names do not match the given types', async () => {
const store = useDocumentStore()
store.storedDocuments = [makeUiDoc('1', DocumentUploadType.BC_DRIVERS_LICENSE)]
Expand Down
5 changes: 2 additions & 3 deletions strr-strata-web/app/stores/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const useDocumentStore = defineStore('strata/document', () => {

async function removeStoredDocument (uiDoc: UiDocument) {
const index = storedDocuments.value.findIndex(item => uiDoc.id === item.id)
if (index === -1) {
return // not in the list (e.g. upload failed before it was added), nothing to remove
if (index < 0) {
return // not in the list, nothing to remove or delete
}
storedDocuments.value.splice(index, 1)
if (uiDoc.apiDoc.fileKey) {
Expand Down Expand Up @@ -83,7 +83,6 @@ export const useDocumentStore = defineStore('strata/document', () => {
} catch (e) {
logFetchError(e, 'Error uploading document')
strrModal.openErrorModal(t('error.docUpload.generic.title'), t('error.docUpload.generic.description'), false)
await removeStoredDocument(uiDoc)
} finally {
// cleanup loading on ui object
uiDoc.loading = false
Expand Down
2 changes: 1 addition & 1 deletion strr-strata-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "strr-strata-web",
"private": true,
"type": "module",
"version": "1.2.14",
"version": "1.2.15",
"engines": {
"node": ">=24"
},
Expand Down
49 changes: 48 additions & 1 deletion strr-strata-web/tests/unit/store-document.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { describe, beforeEach, it, expect } from 'vitest'
import { describe, beforeEach, it, expect, vi } from 'vitest'
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
import { mockStoredDocument } from '../mocks/mockedData'

const mockStrrApi = vi.fn().mockResolvedValue(undefined)
const mockOpenErrorModal = vi.fn()

mockNuxtImport('useNuxtApp', original => () => Object.assign(Object.create(original()), {
$strrApi: mockStrrApi,
$i18n: { t: (key: string) => key }
}))

mockNuxtImport('useStrrModals', () => () => ({ openErrorModal: mockOpenErrorModal }))

describe('Document Store', () => {
beforeEach(() => {
useDocumentStore().$reset()
mockStrrApi.mockReset()
mockStrrApi.mockResolvedValue(undefined)
mockOpenErrorModal.mockClear()
})

it('reset function clears storedDocuments', () => {
Expand Down Expand Up @@ -70,4 +84,37 @@
await store.resetApiDocs()
expect(store.storedDocuments).toHaveLength(0)
})

it('does nothing when removing a document that is not in storedDocuments', async () => {
const store = useDocumentStore()
store.storedDocuments.push(mockStoredDocument)
const missingDoc: UiDocument = {
...mockStoredDocument,
id: 'doc-missing',
apiDoc: { ...mockStoredDocument.apiDoc, fileKey: 'missing-key' }
}

await store.removeStoredDocument(missingDoc)

expect(store.storedDocuments).toHaveLength(1)
expect(store.storedDocuments[0]!.id).toBe(mockStoredDocument.id)
expect(mockStrrApi).not.toHaveBeenCalled()
})

it('keeps existing documents and deletes nothing when a dashboard upload is rejected', async () => {
const store = useDocumentStore()
store.storedDocuments.push(mockStoredDocument)
mockStrrApi.mockRejectedValueOnce(new Error('File contents do not match the file type'))
// upload never succeeded, so the new doc has no apiDoc and was never added to storedDocuments
const rejectedDoc: UiDocument = { ...mockStoredDocument, id: 'doc-rejected', apiDoc: {} as ApiDocument }

await store.addDocumentToApplication(rejectedDoc, 'APP-123')

expect(mockOpenErrorModal).toHaveBeenCalled()
expect(store.storedDocuments).toHaveLength(1)
expect(store.storedDocuments[0]!.id).toBe(mockStoredDocument.id)
// only the failed PUT, no DELETE /documents/{fileKey}
expect(mockStrrApi).toHaveBeenCalledTimes(1)
expect(mockStrrApi).toHaveBeenCalledWith('/applications/APP-123/documents', expect.objectContaining({ method: 'PUT' }))

Check warning on line 118 in strr-strata-web/tests/unit/store-document.spec.ts

View workflow job for this annotation

GitHub Actions / strata-ui-ci / linting-pnpm (24, 11.9.0)

This line has a length of 123. Maximum allowed is 120
})
})
Loading