Skip to content
Snippets Groups Projects
Commit c2a923cd authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

refactor(feedback): simpler fb component

parent db9fe792
No related branches found
No related tags found
2 merge requests!9702.6 Release,!944refactor(feedback): simpler fb component
import FeedbackModal from 'components/Feedback/FeedbackModal' import FeedbackModal from 'components/Feedback/FeedbackModal'
import { ScreenType } from 'enum/screen.enum' import { ScreenType } from 'enum/screen.enum'
import React, { useCallback, useEffect } from 'react' import React, { useEffect } from 'react'
import { changeScreenType } from 'store/global/global.slice' import { changeScreenType } from 'store/global/global.slice'
import { useAppDispatch, useAppSelector } from 'store/hooks' import { useAppDispatch, useAppSelector } from 'store/hooks'
import { openFeedbackModal } from 'store/modal/modal.slice'
import './content.scss' import './content.scss'
interface ContentProps { interface ContentProps {
children: React.ReactNode children: React.ReactNode
...@@ -17,18 +16,11 @@ const Content = ({ ...@@ -17,18 +16,11 @@ const Content = ({
background = 'inherit', background = 'inherit',
}: ContentProps) => { }: ContentProps) => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const { const { screenType } = useAppSelector(state => state.ecolyo.global)
global: { screenType },
modal: { isFeedbacksOpen },
} = useAppSelector(state => state.ecolyo)
const cozyBarHeight = 48 const cozyBarHeight = 48
const cozyNavHeight = 56 const cozyNavHeight = 56
const handleFeedbackModalClose = useCallback(() => {
dispatch(openFeedbackModal(false))
}, [dispatch])
// Set listeners for scroll // Set listeners for scroll
useEffect(() => { useEffect(() => {
window.scrollTo(0, 0) window.scrollTo(0, 0)
...@@ -53,10 +45,7 @@ const Content = ({ ...@@ -53,10 +45,7 @@ const Content = ({
return ( return (
<> <>
<FeedbackModal <FeedbackModal />
open={isFeedbacksOpen}
handleCloseClick={handleFeedbackModalClose}
/>
<div <div
className={'content-view'} className={'content-view'}
style={{ style={{
......
import FeedbackModal from 'components/Feedback/FeedbackModal' import FeedbackModal from 'components/Feedback/FeedbackModal'
import { mount } from 'enzyme' import { mount } from 'enzyme'
import toJson from 'enzyme-to-json'
import React from 'react' import React from 'react'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import * as storeHooks from 'store/hooks'
import { createMockEcolyoStore } from '../../../tests/__mocks__/store' import { createMockEcolyoStore } from '../../../tests/__mocks__/store'
import { waitForComponentToPaint } from '../../../tests/__mocks__/testUtils'
// Value coming from jest.config // Value coming from jest.config
declare let __SAU_LINK__: string declare let __SAU_LINK__: string
...@@ -22,53 +23,51 @@ jest.mock('services/environment.service', () => { ...@@ -22,53 +23,51 @@ jest.mock('services/environment.service', () => {
}) })
}) })
const handleFeedbackModalClose = jest.fn() jest.mock('components/Hooks/useExploration', () => () => ['', jest.fn()])
const mockAppDispatch = jest.spyOn(storeHooks, 'useAppDispatch')
describe('FeedbackModal component', () => { describe('FeedbackModal component', () => {
const store = createMockEcolyoStore() const store = createMockEcolyoStore({ modal: { isFeedbacksOpen: true } })
beforeEach(() => { beforeEach(() => {
store.clearActions() jest.clearAllMocks()
}) })
it('should render the component', () => { it('should render the component', () => {
// TODO fix empty snapshot
const component = mount( const component = mount(
<Provider store={store}> <Provider store={store}>
<FeedbackModal <FeedbackModal />
open={true}
handleCloseClick={handleFeedbackModalClose}
/>
</Provider> </Provider>
).getElement() )
expect(component).toMatchSnapshot() expect(toJson(component)).toMatchSnapshot()
}) })
describe('FeedbackModal functionalities', () => { describe('FeedbackModal functionalities', () => {
it('should close the modal', async () => { it('should close modal with the "x" button', async () => {
const wrapper = mount( const wrapper = mount(
<Provider store={store}> <Provider store={store}>
<FeedbackModal <FeedbackModal />
open={true}
handleCloseClick={handleFeedbackModalClose}
/>
</Provider> </Provider>
) )
await waitForComponentToPaint(wrapper)
wrapper.find('.modal-paper-close-button').first().simulate('click') wrapper.find('.modal-paper-close-button').first().simulate('click')
expect(handleFeedbackModalClose).toHaveBeenCalledTimes(1) expect(mockAppDispatch).toHaveBeenCalledTimes(1)
})
it('should close modal with the "later" button', async () => {
const wrapper = mount(
<Provider store={store}>
<FeedbackModal />
</Provider>
)
wrapper.find('.btn-secondary-positive').first().simulate('click') wrapper.find('.btn-secondary-positive').first().simulate('click')
expect(handleFeedbackModalClose).toHaveBeenCalledTimes(2) expect(mockAppDispatch).toHaveBeenCalledTimes(1)
}) })
it('should open the SAU link', () => { it('should open the SAU link', () => {
global.open = jest.fn() global.open = jest.fn()
const wrapper = mount( const wrapper = mount(
<Provider store={store}> <Provider store={store}>
<FeedbackModal <FeedbackModal />
open={true}
handleCloseClick={handleFeedbackModalClose}
/>
</Provider> </Provider>
) )
wrapper.find('.btn-highlight').first().simulate('click') wrapper.find('.btn-highlight').first().simulate('click')
......
...@@ -11,22 +11,21 @@ import { IuseI18n, useI18n } from 'cozy-ui/transpiled/react/I18n' ...@@ -11,22 +11,21 @@ import { IuseI18n, useI18n } from 'cozy-ui/transpiled/react/I18n'
import Icon from 'cozy-ui/transpiled/react/Icon' import Icon from 'cozy-ui/transpiled/react/Icon'
import { UserExplorationID } from 'enum/userExploration.enum' import { UserExplorationID } from 'enum/userExploration.enum'
import React from 'react' import React from 'react'
import { useAppDispatch, useAppSelector } from 'store/hooks'
import { openFeedbackModal } from 'store/modal/modal.slice'
import './feedbackModal.scss' import './feedbackModal.scss'
declare let __SAU_LINK__: string declare let __SAU_LINK__: string
interface FeedbackModalProps { const FeedbackModal = () => {
open: boolean
handleCloseClick: () => void
}
const FeedbackModal = ({ open, handleCloseClick }: FeedbackModalProps) => {
const { t }: IuseI18n = useI18n() const { t }: IuseI18n = useI18n()
const client: Client = useClient() const client: Client = useClient()
const dispatch = useAppDispatch()
const { isFeedbacksOpen } = useAppSelector(state => state.ecolyo.modal)
const [, setValidExploration] = useExploration() const [, setValidExploration] = useExploration()
const closeModal = () => { const closeModal = () => {
handleCloseClick() dispatch(openFeedbackModal(false))
} }
const goToSAU = () => { const goToSAU = () => {
...@@ -36,11 +35,8 @@ const FeedbackModal = ({ open, handleCloseClick }: FeedbackModalProps) => { ...@@ -36,11 +35,8 @@ const FeedbackModal = ({ open, handleCloseClick }: FeedbackModalProps) => {
return ( return (
<Dialog <Dialog
open={open} open={isFeedbacksOpen}
onClose={(event, reason): void => { onClose={() => closeModal()}
event && reason !== 'backdropClick' && closeModal()
}}
disableEscapeKeyDown
aria-labelledby={'accessibility-title'} aria-labelledby={'accessibility-title'}
classes={{ classes={{
root: 'modal-root', root: 'modal-root',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment