Skip to content

Commit

Permalink
fix: Don't fail shortcut creation if callback is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lohse committed May 25, 2020
1 parent 3ff2008 commit 63b3b1b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const ShortcutCreationModal = ({ onClose, onCreated, displayedFolder }) => {
try {
await client.collection('io.cozy.files.shortcuts').create(data)
Alerter.success(t('Shortcut.created'))
onCreated()
if (onCreated) onCreated()
onClose()
} catch (e) {
Alerter.error(t('Shortcut.errored'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/* eslint-env jest */
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import { render, fireEvent, waitFor } from '@testing-library/react'
import mediaQuery from 'css-mediaquery'

import { createMockClient } from 'cozy-client'
import MuiCozyTheme from 'cozy-ui/transpiled/react/MuiCozyTheme'
import Alerter from 'cozy-ui/transpiled/react/Alerter'

import ShortcutCreationModal from './ShortcutCreationModal'
import AppLike from '../../../../../../../test/components/AppLike'
const tMock = jest.fn()

jest.mock('cozy-ui/transpiled/react/Alerter', () => ({
error: jest.fn(),
success: jest.fn()
}))

function createMatchMedia(width) {
Expand Down Expand Up @@ -50,8 +55,11 @@ describe('ShortcutCreationModal', () => {

fireEvent.click(submitButton)
expect(client.stackClient.fetchJSON).not.toHaveBeenCalled()
fireEvent.change(filenameInput, { target: { value: 'filename' } })
expect(Alerter.error).toHaveBeenCalledTimes(1)
expect(Alerter.success).not.toHaveBeenCalled()

client.stackClient.fetchJSON.mockResolvedValue({ data: {} })
fireEvent.change(filenameInput, { target: { value: 'filename' } })
fireEvent.click(submitButton)

expect(client.stackClient.fetchJSON).toHaveBeenCalledWith(
Expand All @@ -69,6 +77,8 @@ describe('ShortcutCreationModal', () => {
}
}
)
expect(Alerter.error).toHaveBeenCalledTimes(1)
await waitFor(() => expect(Alerter.success).toHaveBeenCalledTimes(1))

fireEvent.change(filenameInput, { target: { value: 'filename.url' } })
fireEvent.click(submitButton)
Expand All @@ -87,5 +97,6 @@ describe('ShortcutCreationModal', () => {
}
}
)
await waitFor(() => expect(Alerter.success).toHaveBeenCalledTimes(2))
})
})

0 comments on commit 63b3b1b

Please sign in to comment.