Skip to content
Snippets Groups Projects
Commit c04dca45 authored by Guilhem CARRON's avatar Guilhem CARRON
Browse files

Finished action tests

parent 817eafaf
Branches
Tags
1 merge request!282Features/us385 test actions
Showing
with 496 additions and 41 deletions
import React from 'react'
import { mount } from 'enzyme'
import { Provider } from 'react-redux'
import { globalStateData } from '../../../test/__mocks__/globalStateData.mock'
import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock'
import ActionModal from './ActionModal'
import {
AllEcogestureData,
defaultEcogestureData,
} from '../../../test/__mocks__/actionData.mock'
import { profileData } from '../../../test/__mocks__/profile.mock'
import configureStore from 'redux-mock-store'
import { Button } from '@material-ui/core'
import ActionBegin from './ActionBegin'
import { act } from '@testing-library/react'
jest.mock('cozy-ui/transpiled/react/I18n', () => {
return {
useI18n: jest.fn(() => {
return {
t: (str: string) => str,
}
}),
}
})
const mockgetCustomActions = jest.fn()
const mockgetDefaultActions = jest.fn()
jest.mock('services/action.service', () => {
return jest.fn(() => {
return {
getCustomActions: mockgetCustomActions,
getDefaultActions: mockgetDefaultActions,
}
})
})
const mockStore = configureStore([])
describe('ActionBegin component', () => {
it('should render correctly', async () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionBegin
action={defaultEcogestureData[1]}
setShowList={jest.fn()}
userChallenge={userChallengeData[1]}
/>
</Provider>
)
await act(async () => {
await new Promise(resolve => setTimeout(resolve))
wrapper.update()
})
expect(wrapper).toMatchSnapshot()
})
it('should render correctly with custom action', async () => {
mockgetCustomActions.mockResolvedValue([
AllEcogestureData[0],
AllEcogestureData[5],
AllEcogestureData[2],
])
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: { ...profileData, isProfileTypeCompleted: true },
},
})
const wrapper = mount(
<Provider store={store}>
<ActionBegin
setShowList={jest.fn()}
userChallenge={userChallengeData[1]}
/>
</Provider>
)
await act(async () => {
await new Promise(resolve => setTimeout(resolve))
wrapper.update()
})
expect(wrapper.find('.action-title').text()).toBe('Bonhomme de neige')
})
it('should render chosen action ', async () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionBegin
action={defaultEcogestureData[1]}
setShowList={jest.fn()}
userChallenge={userChallengeData[1]}
/>
</Provider>
)
await act(async () => {
await new Promise(resolve => setTimeout(resolve))
wrapper.update()
})
expect(wrapper.find(ActionBegin).exists()).toBeTruthy()
})
it('should open launch Modal ', async () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionBegin
action={defaultEcogestureData[1]}
setShowList={jest.fn()}
userChallenge={userChallengeData[1]}
/>
</Provider>
)
await act(async () => {
await new Promise(resolve => setTimeout(resolve))
wrapper.update()
})
wrapper
.find(Button)
.first()
.simulate('click')
expect(wrapper.find(ActionModal).exists()).toBeTruthy()
})
it('should go to the list ', async () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionBegin
action={defaultEcogestureData[1]}
setShowList={jest.fn()}
userChallenge={userChallengeData[1]}
/>
</Provider>
)
await act(async () => {
await new Promise(resolve => setTimeout(resolve))
wrapper.update()
})
wrapper
.find(Button)
.at(1)
.simulate('click')
})
})
import React from 'react' import React from 'react'
import { mount } from 'enzyme' import { mount } from 'enzyme'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import * as reactRedux from 'react-redux'
import { globalStateData } from '../../../test/__mocks__/globalStateData.mock' import { globalStateData } from '../../../test/__mocks__/globalStateData.mock'
import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock' import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock'
import {
createMockStore,
mockInitialEcolyoState,
} from '../../../test/__mocks__/store'
import ActionCard from './ActionCard' import ActionCard from './ActionCard'
import { profileData } from '../../../test/__mocks__/profile.mock'
import configureStore from 'redux-mock-store'
import { defaultEcogestureData } from '../../../test/__mocks__/actionData.mock'
import { Button } from '@material-ui/core'
import EcogestureModal from 'components/Ecogesture/EcogestureModal'
const useSelectorSpy = jest.spyOn(reactRedux, 'useSelector') const mockStore = configureStore([])
describe('ActionView component', () => { jest.mock('cozy-ui/transpiled/react/I18n', () => {
let store: any return {
beforeEach(() => { useI18n: jest.fn(() => {
store = createMockStore(mockInitialEcolyoState) return {
useSelectorSpy.mockClear() t: (str: string) => str,
}) }
}),
}
})
describe('ActionCard component', () => {
it('should be rendered correctly', () => { it('should be rendered correctly', () => {
useSelectorSpy.mockReturnValue({ const store = mockStore({
global: globalStateData, ecolyo: {
challenge: userChallengeData[1], challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionCard
setShowList={jest.fn()}
setSelectedAction={jest.fn()}
action={defaultEcogestureData[1]}
/>
</Provider>
)
expect(wrapper).toMatchSnapshot()
})
it('should open modal', () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
}) })
// const wrapper = mount( const wrapper = mount(
// <Provider store={store}> <Provider store={store}>
// <ActionCard /> <ActionCard
// </Provider> setShowList={jest.fn()}
// ) setSelectedAction={jest.fn()}
action={defaultEcogestureData[1]}
/>
</Provider>
)
wrapper
.find(Button)
.first()
.simulate('click')
expect(wrapper.find(EcogestureModal).exists()).toBeTruthy()
}) })
}) })
import React from 'react'
import { mount } from 'enzyme'
import { Provider } from 'react-redux'
import { globalStateData } from '../../../test/__mocks__/globalStateData.mock'
import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock'
import { profileData } from '../../../test/__mocks__/profile.mock'
import configureStore from 'redux-mock-store'
import ActionChoose from './ActionChoose'
import ActionBegin from './ActionBegin'
jest.mock('cozy-ui/transpiled/react/I18n', () => {
return {
useI18n: jest.fn(() => {
return {
t: (str: string) => str,
}
}),
}
})
const mockStore = configureStore([])
describe('ActionChoose component', () => {
it('should render correctly', () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionChoose userChallenge={userChallengeData[1]} />
</Provider>
)
expect(wrapper).toMatchSnapshot()
})
it('should render ActionBegin component', async () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionChoose userChallenge={userChallengeData[1]} />
</Provider>
)
expect(wrapper.find(ActionBegin).exists()).toBeTruthy()
})
})
...@@ -7,6 +7,9 @@ import { userChallengeData } from '../../../test/__mocks__/userChallengeData.moc ...@@ -7,6 +7,9 @@ import { userChallengeData } from '../../../test/__mocks__/userChallengeData.moc
import { profileData } from '../../../test/__mocks__/profile.mock' import { profileData } from '../../../test/__mocks__/profile.mock'
import ActionDone from './ActionDone' import ActionDone from './ActionDone'
import { Button } from '@material-ui/core' import { Button } from '@material-ui/core'
import * as reactRedux from 'react-redux'
import * as challengeActions from 'store/challenge/challenge.actions'
import { act } from '@testing-library/react'
const mockStore = configureStore([]) const mockStore = configureStore([])
jest.mock('cozy-ui/transpiled/react/I18n', () => { jest.mock('cozy-ui/transpiled/react/I18n', () => {
...@@ -25,6 +28,14 @@ jest.mock('react-router-dom', () => ({ ...@@ -25,6 +28,14 @@ jest.mock('react-router-dom', () => ({
goBack: mockHistoryGoBack, goBack: mockHistoryGoBack,
}), }),
})) }))
const mockupdateUserChallenge = jest.fn()
jest.mock('services/challenge.service', () => {
return jest.fn(() => {
return {
updateUserChallenge: mockupdateUserChallenge,
}
})
})
describe('ActionDone component', () => { describe('ActionDone component', () => {
it('should be rendered correctly', () => { it('should be rendered correctly', () => {
const store = mockStore({ const store = mockStore({
...@@ -42,6 +53,11 @@ describe('ActionDone component', () => { ...@@ -42,6 +53,11 @@ describe('ActionDone component', () => {
expect(wrapper).toMatchSnapshot() expect(wrapper).toMatchSnapshot()
}) })
it('should click on button and update action to done', async () => { it('should click on button and update action to done', async () => {
const useDispatchSpy = jest.spyOn(reactRedux, 'useDispatch')
const updateChallengeSpy = jest.spyOn(
challengeActions,
'updateUserChallengeList'
)
const store = mockStore({ const store = mockStore({
ecolyo: { ecolyo: {
challenge: userChallengeData[1], challenge: userChallengeData[1],
...@@ -49,6 +65,8 @@ describe('ActionDone component', () => { ...@@ -49,6 +65,8 @@ describe('ActionDone component', () => {
profile: profileData, profile: profileData,
}, },
}) })
mockupdateUserChallenge.mockResolvedValueOnce(userChallengeData[1])
useDispatchSpy.mockReturnValue(jest.fn())
const wrapper = mount( const wrapper = mount(
<Provider store={store}> <Provider store={store}>
<ActionDone currentChallenge={userChallengeData[1]} /> <ActionDone currentChallenge={userChallengeData[1]} />
...@@ -58,5 +76,10 @@ describe('ActionDone component', () => { ...@@ -58,5 +76,10 @@ describe('ActionDone component', () => {
.find(Button) .find(Button)
.first() .first()
.simulate('click') .simulate('click')
await act(async () => {
await new Promise(resolve => setTimeout(resolve))
wrapper.update()
})
expect(updateChallengeSpy).toBeCalledTimes(1)
}) })
}) })
import React from 'react'
import { mount } from 'enzyme'
import { Provider } from 'react-redux'
import { globalStateData } from '../../../test/__mocks__/globalStateData.mock'
import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock'
import ActionModal from './ActionModal'
import { defaultEcogestureData } from '../../../test/__mocks__/actionData.mock'
import { profileData } from '../../../test/__mocks__/profile.mock'
import configureStore from 'redux-mock-store'
import { Button } from '@material-ui/core'
import * as reactRedux from 'react-redux'
import * as challengeActions from 'store/challenge/challenge.actions'
import { act } from '@testing-library/react'
jest.mock('cozy-ui/transpiled/react/I18n', () => {
return {
useI18n: jest.fn(() => {
return {
t: (str: string) => str,
}
}),
}
})
const mockupdateUserChallenge = jest.fn()
jest.mock('services/challenge.service', () => {
return jest.fn(() => {
return {
updateUserChallenge: mockupdateUserChallenge,
}
})
})
const mockStore = configureStore([])
describe('ActionModal component', () => {
it('should render correctly', () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionModal
handleCloseClick={jest.fn()}
action={defaultEcogestureData[1]}
userChallenge={userChallengeData[1]}
/>
</Provider>
)
expect(wrapper).toMatchSnapshot()
})
it('should click on button and update action to ongoing', async () => {
const useDispatchSpy = jest.spyOn(reactRedux, 'useDispatch')
const updateChallengeSpy = jest.spyOn(
challengeActions,
'updateUserChallengeList'
)
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
mockupdateUserChallenge.mockResolvedValueOnce(userChallengeData[1])
useDispatchSpy.mockReturnValue(jest.fn())
const wrapper = mount(
<Provider store={store}>
<ActionModal
handleCloseClick={jest.fn()}
action={defaultEcogestureData[1]}
userChallenge={userChallengeData[1]}
/>
</Provider>
)
wrapper
.find(Button)
.first()
.simulate('click')
await act(async () => {
await new Promise(resolve => setTimeout(resolve))
wrapper.update()
})
expect(updateChallengeSpy).toBeCalledTimes(1)
})
})
import React from 'react'
import { mount } from 'enzyme'
import { Provider } from 'react-redux'
import { globalStateData } from '../../../test/__mocks__/globalStateData.mock'
import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock'
import { defaultEcogestureData } from '../../../test/__mocks__/actionData.mock'
import { profileData } from '../../../test/__mocks__/profile.mock'
import configureStore from 'redux-mock-store'
import { Button } from '@material-ui/core'
import ActionOnGoing from './ActionOnGoing'
import { UserActionState } from 'enum/userAction.enum'
import { DateTime } from 'luxon'
import EcogestureModal from 'components/Ecogesture/EcogestureModal'
jest.mock('cozy-ui/transpiled/react/I18n', () => {
return {
useI18n: jest.fn(() => {
return {
t: (str: string) => str,
}
}),
}
})
const mockStore = configureStore([])
describe('ActionOnGoing component', () => {
const userAction = {
ecogesture: defaultEcogestureData[1],
startDate: DateTime.local().setZone('utc', {
keepLocalTime: true,
}),
state: UserActionState.ONGOING,
}
it('should render correctly', () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionOnGoing userAction={userAction} />
</Provider>
)
expect(wrapper).toMatchSnapshot()
})
it('should click on button onpen ecogesture Modal', async () => {
const store = mockStore({
ecolyo: {
challenge: userChallengeData[1],
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
},
})
const wrapper = mount(
<Provider store={store}>
<ActionOnGoing userAction={userAction} />
</Provider>
)
wrapper
.find(Button)
.first()
.simulate('click')
expect(wrapper.find(EcogestureModal).exists()).toBeTruthy()
})
})
import React from 'react' import React from 'react'
import { mount } from 'enzyme' import { mount } from 'enzyme'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import * as reactRedux from 'react-redux'
import { globalStateData } from '../../../test/__mocks__/globalStateData.mock' import { globalStateData } from '../../../test/__mocks__/globalStateData.mock'
import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock' import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock'
import ActionView from 'components/Action/ActionView' import ActionView from 'components/Action/ActionView'
import { import configureStore from 'redux-mock-store'
createMockStore,
mockInitialEcolyoState,
} from '../../../test/__mocks__/store'
import { UserActionState } from 'enum/userAction.enum' import { UserActionState } from 'enum/userAction.enum'
import ActionChoose from './ActionChoose' import ActionChoose from './ActionChoose'
import ActionOnGoing from './ActionOnGoing' import ActionOnGoing from './ActionOnGoing'
import ActionDone from './ActionDone' import ActionDone from './ActionDone'
import { profileData } from '../../../test/__mocks__/profile.mock'
import { modalStateData } from '../../../test/__mocks__/modalStateData.mock'
jest.mock('cozy-ui/transpiled/react/I18n', () => { jest.mock('cozy-ui/transpiled/react/I18n', () => {
return { return {
...@@ -23,15 +21,9 @@ jest.mock('cozy-ui/transpiled/react/I18n', () => { ...@@ -23,15 +21,9 @@ jest.mock('cozy-ui/transpiled/react/I18n', () => {
}), }),
} }
}) })
const useSelectorSpy = jest.spyOn(reactRedux, 'useSelector')
const mockStore = configureStore([])
describe('ActionView component', () => { describe('ActionView component', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let store: any
beforeEach(() => {
store = createMockStore(mockInitialEcolyoState)
useSelectorSpy.mockClear()
})
it('should render ActionChoose component', () => { it('should render ActionChoose component', () => {
const userChallenge = { const userChallenge = {
...userChallengeData[1], ...userChallengeData[1],
...@@ -40,9 +32,13 @@ describe('ActionView component', () => { ...@@ -40,9 +32,13 @@ describe('ActionView component', () => {
state: UserActionState.UNSTARTED, state: UserActionState.UNSTARTED,
}, },
} }
useSelectorSpy.mockReturnValue({ const store = mockStore({
global: globalStateData, ecolyo: {
challenge: userChallenge, challenge: userChallenge,
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
modal: modalStateData,
},
}) })
const wrapper = mount( const wrapper = mount(
<Provider store={store}> <Provider store={store}>
...@@ -60,9 +56,13 @@ describe('ActionView component', () => { ...@@ -60,9 +56,13 @@ describe('ActionView component', () => {
state: UserActionState.NOTIFICATION, state: UserActionState.NOTIFICATION,
}, },
} }
useSelectorSpy.mockReturnValue({ const store = mockStore({
global: globalStateData, ecolyo: {
challenge: userChallenge, challenge: userChallenge,
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
profile: profileData,
modal: modalStateData,
},
}) })
const wrapper = mount( const wrapper = mount(
<Provider store={store}> <Provider store={store}>
...@@ -79,9 +79,13 @@ describe('ActionView component', () => { ...@@ -79,9 +79,13 @@ describe('ActionView component', () => {
state: UserActionState.ONGOING, state: UserActionState.ONGOING,
}, },
} }
useSelectorSpy.mockReturnValue({ const store = mockStore({
global: globalStateData, ecolyo: {
challenge: userChallenge, challenge: userChallenge,
global: { ...globalStateData, fluidTypes: [0, 1, 2] },
modal: modalStateData,
profile: profileData,
},
}) })
const wrapper = mount( const wrapper = mount(
<Provider store={store}> <Provider store={store}>
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ActionBegin component should render correctly 1`] = `ReactWrapper {}`;
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ActionCard component should be rendered correctly 1`] = `ReactWrapper {}`;
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ActionChoose component should render correctly 1`] = `ReactWrapper {}`;
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ActionModal component should render correctly 1`] = `ReactWrapper {}`;
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ActionOnGoing component should render correctly 1`] = `ReactWrapper {}`;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment