Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public view fixes #2021

Merged
merged 6 commits into from
May 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"^.+\\.webapp$": "<rootDir>/test/jestLib/json-transformer.js"
},
"transformIgnorePatterns": ["node_modules/(?!cozy-ui)/"],
"testEnvironment": "jest-environment-jsdom-sixteen",
"testMatch": ["**/(*.)(spec|test).js?(x)"],
"globals": {
"__APP_SLUG__": "drive"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"eslint-config-cozy-app": "1.1.4",
"husky": "0.14.3",
"identity-obj-proxy": "3.0.0",
"jest-environment-jsdom-sixteen": "^1.0.3",
"npm-run-all": "4.1.5",
"react-addons-test-utils": "15.6.2",
"react-test-renderer": "15.6.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ exports[`Unlink should render the Component 1`] = `
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": "mobile.settings.unlink.title",
},
Object {
"isThrow": false,
"type": "return",
"value": "mobile.settings.unlink.description",
},
Object {
"isThrow": false,
"type": "return",
"value": "mobile.settings.unlink.button",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ const ShortcutCreationModal = ({ onClose, onCreated, displayedFolder }) => {
}
try {
await client.collection('io.cozy.files.shortcuts').create(data)
Alerter.success('Shortcut.created')
onCreated()
Alerter.success(t('Shortcut.created'))
if (onCreated) onCreated()
onClose()
} catch (e) {
Alerter.error('Shortcut.errored')
Alerter.error(t('Shortcut.errored'))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +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/utils/color', () => ({
getCssVariableValue: () => '#fff'

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

function createMatchMedia(width) {
Expand All @@ -22,7 +25,7 @@ function createMatchMedia(width) {
}
const client = new createMockClient({})
const onCloseSpy = jest.fn()
const props = {
const defaultProps = {
displayedFolder: {
id: 'id'
},
Expand All @@ -32,11 +35,12 @@ const props = {

describe('ShortcutCreationModal', () => {
beforeEach(() => {
jest.resetAllMocks()
window.matchMedia = createMatchMedia(window.innerWidth)
tMock.mockImplementation(key => key)
})

it('should show the new account form', async () => {
const setup = props => {
const { getByLabelText, getByText } = render(
<AppLike client={client}>
<MuiCozyTheme>
Expand All @@ -48,12 +52,25 @@ describe('ShortcutCreationModal', () => {
const urlInput = getByLabelText('URL')
const filenameInput = getByLabelText('Filename')
const submitButton = getByText('Create')

return {
urlInput,
filenameInput,
submitButton
}
}

it('should show the new shortcut form', async () => {
const { urlInput, filenameInput, submitButton } = setup(defaultProps)
fireEvent.change(urlInput, { target: { value: 'https://cozy.io' } })

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 @@ -71,6 +88,8 @@ describe('ShortcutCreationModal', () => {
}
}
)
expect(Alerter.error).toHaveBeenCalledTimes(1)
await waitFor(() => expect(Alerter.success).toHaveBeenCalledTimes(1))
y-lohse marked this conversation as resolved.
Show resolved Hide resolved

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

it('should call the optional onCreated prop', async () => {
const onCreatedMock = jest.fn()
const { urlInput, filenameInput, submitButton } = setup({
...defaultProps,
onCreated: onCreatedMock
})

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

await waitFor(() => expect(Alerter.success).toHaveBeenCalledTimes(1))
expect(onCreatedMock).toHaveBeenCalled()
})
})
11 changes: 1 addition & 10 deletions src/drive/web/modules/public/Actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import React from 'react'
import { showModal } from 'react-cozy-helpers'
import { downloadFiles, trashFiles } from 'drive/web/modules/navigation/duck'
import DeleteConfirm from 'drive/web/modules/drive/DeleteConfirm'
import {
isRenaming,
getRenamingFile,
startRenamingAsync
} from 'drive/web/modules/drive/rename'
import { startRenamingAsync } from 'drive/web/modules/drive/rename'
import { isFile, isReferencedByAlbum } from 'drive/web/modules/drive/files'

const isAnyFileReferencedByAlbum = files => {
Expand All @@ -16,11 +12,6 @@ const isAnyFileReferencedByAlbum = files => {
return false
}

export const mapStateToProps = state => ({
isRenaming: isRenaming(state),
renamingFile: getRenamingFile(state)
})

export const mapDispatchToProps = (dispatch, ownProps) => {
const { onFileDelete, hasWriteAccess } = ownProps
return {
Expand Down
24 changes: 19 additions & 5 deletions src/drive/web/modules/public/LightFileList.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import React from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router'

import FileList from 'drive/web/modules/filelist/FileList'
import SelectionBar from 'drive/web/modules/selection/SelectionBar'
import { mapStateToProps, mapDispatchToProps } from './Actions'
import { mapDispatchToProps } from './Actions'
import { isRenaming, getRenamingFile } from 'drive/web/modules/drive/rename'

const LightFileList = ({
actions,
onFileOpen,
onFolderOpen,
isRenaming,
renamingFile,
fileListProps
}) => (
<>
Expand All @@ -18,12 +22,22 @@ const LightFileList = ({
onFolderOpen={onFolderOpen}
withSelectionCheckbox={true}
fileActions={actions}
isRenaming={isRenaming}
renamingFile={renamingFile}
{...fileListProps}
/>
</>
)

export default connect(
mapStateToProps,
mapDispatchToProps
)(LightFileList)
const mapStateToProps = state => ({
isRenaming: isRenaming(state),
renamingFile: getRenamingFile(state)
})

export default withRouter(
//router is used in mapDispatchToProps
connect(
mapStateToProps,
mapDispatchToProps
)(LightFileList)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this change. It seems we don't use router in this component and we don't pass it neither to its children. Can you explain why this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used here, in the mapDispatchToProps of he component. But I agree, it's not very obvious. Do you think it would be better to include the Actions file in this one to make it more explicit? Otherwise we can add a comment imo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best solution is to not rely on ownprops.something (since it can be dangerous for the perf) but I don't know how to handle that.

24 changes: 12 additions & 12 deletions src/photos/targets/public/__snapshots__/App.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,35 @@ exports[`Public view should render children when they are present 1`] = `
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
],
Expand Down Expand Up @@ -200,19 +200,19 @@ exports[`Public view should render the album 1`] = `
],
"results": Array [
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
Object {
"isThrow": false,
"type": "return",
"value": undefined,
},
],
Expand Down