Skip to content

Commit

Permalink
chore(Upload): adds tests when uploading file without extension (#4157)
Browse files Browse the repository at this point in the history
  • Loading branch information
langz authored Oct 22, 2024
1 parent 8087bb6 commit 94d6a5f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,52 @@ describe('Upload', () => {
).toEqual(expect.arrayContaining(['dnb-upload__file-cell--highlight']))
})

it('will return error when dropping a file with extension that is not accepted', async () => {
const id = 'not-supported-extension'

renderHook(useUpload, { initialProps: id })

render(
<Upload {...defaultProps} id={id} acceptedFileTypes={['jpg']} />
)

const getRootElement = () => document.querySelector('.dnb-upload')

const element = getRootElement()
const file1 = createMockFile('fileName-1.png', 100, 'image/png')

await waitFor(() =>
fireEvent.drop(element, {
dataTransfer: { files: [file1] },
})
)

expect(screen.queryByText(nb.errorUnsupportedFile)).toBeInTheDocument()
})

it('will return error when dropping a file without extension', async () => {
const id = 'no-extension'

renderHook(useUpload, { initialProps: id })

render(
<Upload {...defaultProps} id={id} acceptedFileTypes={['jpg']} />
)

const getRootElement = () => document.querySelector('.dnb-upload')

const element = getRootElement()
const file1 = createMockFile('fileName-1', 100, '')

await waitFor(() =>
fireEvent.drop(element, {
dataTransfer: { files: [file1] },
})
)

expect(screen.queryByText(nb.errorUnsupportedFile)).toBeInTheDocument()
})

describe('useUpload', () => {
it('calls uses the useUpload hook to store files', async () => {
const validationFunction = jest.fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ describe('verifyFiles', () => {
})
})

it('returns error message when uploading a file without a file extension', () => {
const file1 = createMockFile('fileName1', 100, '')

const rawFiles = [{ file: file1 }]
const acceptedFileTypes = ['png']

const files = verifyFiles(rawFiles, {
acceptedFileTypes: acceptedFileTypes,
errorUnsupportedFile: 'error unsupported file',
errorLargeFile: 'error 2',
})

expect(files[0]).toEqual({
file: file1,
errorMessage: 'error unsupported file',
})
})

describe('when providing max size to file type', () => {
it('returns file size error', () => {
const file1 = createMockFile('fileName1.png', 100000000, 'image/png')
Expand Down

0 comments on commit 94d6a5f

Please sign in to comment.