Skip to content

Commit

Permalink
fix: Rename files after upload
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lohse committed Jul 31, 2020
1 parent 8f56189 commit 1233a29
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/drive/web/modules/drive/RenameInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { abortRenaming } from './rename'
// If we set the _rev then CozyClient tries to update. Else
// it tries to create
const updateFileNameQuery = async (client, file, newName) => {
return client.save({ ...file, name: newName, _rev: file.meta.rev })
return client.save({
...file,
name: newName,
_rev: file._rev || file.meta.rev
})
}

export const RenameInput = ({ onAbort, file, refreshFolderContent }) => {
Expand Down
47 changes: 39 additions & 8 deletions src/drive/web/modules/drive/RenameInput.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ import { generateFile } from 'test/generate'
import { RenameInput } from './RenameInput'

describe('RenameInput', () => {
it('test the component', async () => {
const client = createMockClient({})
const file = generateFile({ i: '10', type: 'file' })
const onAbort = jest.fn()
const { getByText } = render(
const client = createMockClient({})
const onAbort = jest.fn()

const setup = ({ file }) => {
return render(
<AppLike client={client}>
<RenameInput file={{ ...file, meta: { rev: '1' } }} onAbort={onAbort} />
<RenameInput file={file} onAbort={onAbort} />
</AppLike>
)
}

it('tests the component', async () => {
const file = {
...generateFile({ i: '10', type: 'file' }),
meta: { rev: '1' }
}

const { getByText } = setup({ file })
const inputNode = document.getElementsByTagName('input')[0]

fireEvent.change(inputNode, { target: { value: 'new Name.pdf' } })
expect(inputNode.value).toBe('new Name.pdf')
fireEvent.keyDown(inputNode, { key: 'Enter', code: 'Enter', keyCode: 13 })
expect(client.save).toHaveBeenCalledWith(
expect.objectContaining({
name: 'new Name.pdf'
name: 'new Name.pdf',
_rev: '1'
})
)
await waitFor(() => expect(onAbort).toHaveBeenCalled())
Expand All @@ -39,9 +49,30 @@ describe('RenameInput', () => {
fireEvent.click(getByText('Continue'))
expect(client.save).toHaveBeenCalledWith(
expect.objectContaining({
name: 'new Name.txt'
name: 'new Name.txt',
_rev: '1'
})
)
await waitFor(() => expect(onAbort).toHaveBeenCalled())
})

it('works without meta rev', async () => {
// files that have just been uploaded don't have a meta.rev field, they just have a normal _rev
const file = {
...generateFile({ i: '10', type: 'file' }),
_rev: 1
}

setup({ file })
const inputNode = document.getElementsByTagName('input')[0]

fireEvent.change(inputNode, { target: { value: 'new Name.pdf' } })
fireEvent.keyDown(inputNode, { key: 'Enter', code: 'Enter', keyCode: 13 })
expect(client.save).toHaveBeenCalledWith(
expect.objectContaining({
name: 'new Name.pdf',
_rev: '1'
})
)
})
})
3 changes: 2 additions & 1 deletion src/drive/web/modules/filelist/FilenameInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class FilenameInput extends Component {
//Since we're mounting the component and focusing it at the same time
// let's add a small timeout to be sure the ref is populated
setTimeout(() => {
this.textInput.current.setSelectionRange(0, filename.length)
if (this.textInput.current)
this.textInput.current.setSelectionRange(0, filename.length)
}, 5)
}
render() {
Expand Down

0 comments on commit 1233a29

Please sign in to comment.