diff --git a/src/components/Action/ActionBegin.spec.tsx b/src/components/Action/ActionBegin.spec.tsx new file mode 100644 index 0000000000000000000000000000000000000000..edb03a293da304b93979c7fe279db1e42bfb997a --- /dev/null +++ b/src/components/Action/ActionBegin.spec.tsx @@ -0,0 +1,166 @@ +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') + }) +}) diff --git a/src/components/Action/ActionCard.spec.tsx b/src/components/Action/ActionCard.spec.tsx index 364ca6ce8e94af000905ef40711278dbdf94fdab..894d5e0b54311448c8ff261236cec0fcc870ca2b 100644 --- a/src/components/Action/ActionCard.spec.tsx +++ b/src/components/Action/ActionCard.spec.tsx @@ -1,32 +1,67 @@ import React from 'react' import { mount } from 'enzyme' import { Provider } from 'react-redux' -import * as reactRedux from 'react-redux' import { globalStateData } from '../../../test/__mocks__/globalStateData.mock' import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock' -import { - createMockStore, - mockInitialEcolyoState, -} from '../../../test/__mocks__/store' 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', () => { - let store: any - beforeEach(() => { - store = createMockStore(mockInitialEcolyoState) - useSelectorSpy.mockClear() - }) +jest.mock('cozy-ui/transpiled/react/I18n', () => { + return { + useI18n: jest.fn(() => { + return { + t: (str: string) => str, + } + }), + } +}) +describe('ActionCard component', () => { it('should be rendered correctly', () => { - useSelectorSpy.mockReturnValue({ - global: globalStateData, - challenge: userChallengeData[1], + const store = mockStore({ + ecolyo: { + 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( - // <Provider store={store}> - // <ActionCard /> - // </Provider> - // ) + const wrapper = mount( + <Provider store={store}> + <ActionCard + setShowList={jest.fn()} + setSelectedAction={jest.fn()} + action={defaultEcogestureData[1]} + /> + </Provider> + ) + wrapper + .find(Button) + .first() + .simulate('click') + expect(wrapper.find(EcogestureModal).exists()).toBeTruthy() }) }) diff --git a/src/components/Action/ActionChoose.spec.tsx b/src/components/Action/ActionChoose.spec.tsx new file mode 100644 index 0000000000000000000000000000000000000000..98f134dd67fa191efb6b055127690e1d8262637e --- /dev/null +++ b/src/components/Action/ActionChoose.spec.tsx @@ -0,0 +1,53 @@ +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() + }) +}) diff --git a/src/components/Action/ActionDone.spec.tsx b/src/components/Action/ActionDone.spec.tsx index e3baaaa3af83c96f8c9048dac818ed4a0e8317ed..1eae02e8d41e0b1665d0485ed1a2a0bc04e2a73a 100644 --- a/src/components/Action/ActionDone.spec.tsx +++ b/src/components/Action/ActionDone.spec.tsx @@ -7,6 +7,9 @@ import { userChallengeData } from '../../../test/__mocks__/userChallengeData.moc import { profileData } from '../../../test/__mocks__/profile.mock' import ActionDone from './ActionDone' 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([]) jest.mock('cozy-ui/transpiled/react/I18n', () => { @@ -25,6 +28,14 @@ jest.mock('react-router-dom', () => ({ goBack: mockHistoryGoBack, }), })) +const mockupdateUserChallenge = jest.fn() +jest.mock('services/challenge.service', () => { + return jest.fn(() => { + return { + updateUserChallenge: mockupdateUserChallenge, + } + }) +}) describe('ActionDone component', () => { it('should be rendered correctly', () => { const store = mockStore({ @@ -42,6 +53,11 @@ describe('ActionDone component', () => { expect(wrapper).toMatchSnapshot() }) 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({ ecolyo: { challenge: userChallengeData[1], @@ -49,6 +65,8 @@ describe('ActionDone component', () => { profile: profileData, }, }) + mockupdateUserChallenge.mockResolvedValueOnce(userChallengeData[1]) + useDispatchSpy.mockReturnValue(jest.fn()) const wrapper = mount( <Provider store={store}> <ActionDone currentChallenge={userChallengeData[1]} /> @@ -58,5 +76,10 @@ describe('ActionDone component', () => { .find(Button) .first() .simulate('click') + await act(async () => { + await new Promise(resolve => setTimeout(resolve)) + wrapper.update() + }) + expect(updateChallengeSpy).toBeCalledTimes(1) }) }) diff --git a/src/components/Action/ActionModal.spec.tsx b/src/components/Action/ActionModal.spec.tsx new file mode 100644 index 0000000000000000000000000000000000000000..bbbd8a9cb1ad53610155d9a1c8e7e811ee06877b --- /dev/null +++ b/src/components/Action/ActionModal.spec.tsx @@ -0,0 +1,90 @@ +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) + }) +}) diff --git a/src/components/Action/ActionOnGoing.spec.tsx b/src/components/Action/ActionOnGoing.spec.tsx new file mode 100644 index 0000000000000000000000000000000000000000..7d7b699f9196f79c7522c84a98bb975910aebb8d --- /dev/null +++ b/src/components/Action/ActionOnGoing.spec.tsx @@ -0,0 +1,69 @@ +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() + }) +}) diff --git a/src/components/Action/ActionView.spec.tsx b/src/components/Action/ActionView.spec.tsx index 77fec030e40c05a53237282d92a3b71054ed1232..59a9c30e04a15a7c15f1af271c006e7269e1c811 100644 --- a/src/components/Action/ActionView.spec.tsx +++ b/src/components/Action/ActionView.spec.tsx @@ -1,18 +1,16 @@ import React from 'react' import { mount } from 'enzyme' import { Provider } from 'react-redux' -import * as reactRedux from 'react-redux' import { globalStateData } from '../../../test/__mocks__/globalStateData.mock' import { userChallengeData } from '../../../test/__mocks__/userChallengeData.mock' import ActionView from 'components/Action/ActionView' -import { - createMockStore, - mockInitialEcolyoState, -} from '../../../test/__mocks__/store' +import configureStore from 'redux-mock-store' import { UserActionState } from 'enum/userAction.enum' import ActionChoose from './ActionChoose' import ActionOnGoing from './ActionOnGoing' 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', () => { return { @@ -23,15 +21,9 @@ jest.mock('cozy-ui/transpiled/react/I18n', () => { }), } }) -const useSelectorSpy = jest.spyOn(reactRedux, 'useSelector') +const mockStore = configureStore([]) 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', () => { const userChallenge = { ...userChallengeData[1], @@ -40,9 +32,13 @@ describe('ActionView component', () => { state: UserActionState.UNSTARTED, }, } - useSelectorSpy.mockReturnValue({ - global: globalStateData, - challenge: userChallenge, + const store = mockStore({ + ecolyo: { + challenge: userChallenge, + global: { ...globalStateData, fluidTypes: [0, 1, 2] }, + profile: profileData, + modal: modalStateData, + }, }) const wrapper = mount( <Provider store={store}> @@ -60,9 +56,13 @@ describe('ActionView component', () => { state: UserActionState.NOTIFICATION, }, } - useSelectorSpy.mockReturnValue({ - global: globalStateData, - challenge: userChallenge, + const store = mockStore({ + ecolyo: { + challenge: userChallenge, + global: { ...globalStateData, fluidTypes: [0, 1, 2] }, + profile: profileData, + modal: modalStateData, + }, }) const wrapper = mount( <Provider store={store}> @@ -79,9 +79,13 @@ describe('ActionView component', () => { state: UserActionState.ONGOING, }, } - useSelectorSpy.mockReturnValue({ - global: globalStateData, - challenge: userChallenge, + const store = mockStore({ + ecolyo: { + challenge: userChallenge, + global: { ...globalStateData, fluidTypes: [0, 1, 2] }, + modal: modalStateData, + profile: profileData, + }, }) const wrapper = mount( <Provider store={store}> diff --git a/src/components/Action/__snapshots__/ActionBegin.spec.tsx.snap b/src/components/Action/__snapshots__/ActionBegin.spec.tsx.snap new file mode 100644 index 0000000000000000000000000000000000000000..65dfa39a442baa95024c6ec96b16cf6bfb2bb7bc --- /dev/null +++ b/src/components/Action/__snapshots__/ActionBegin.spec.tsx.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ActionBegin component should render correctly 1`] = `ReactWrapper {}`; diff --git a/src/components/Action/__snapshots__/ActionCard.spec.tsx.snap b/src/components/Action/__snapshots__/ActionCard.spec.tsx.snap new file mode 100644 index 0000000000000000000000000000000000000000..a2089e3e24e321d653d4799370b223331174aaa0 --- /dev/null +++ b/src/components/Action/__snapshots__/ActionCard.spec.tsx.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ActionCard component should be rendered correctly 1`] = `ReactWrapper {}`; diff --git a/src/components/Action/__snapshots__/ActionChoose.spec.tsx.snap b/src/components/Action/__snapshots__/ActionChoose.spec.tsx.snap new file mode 100644 index 0000000000000000000000000000000000000000..39bd87fbebd0d4d6412934043880296db9a8ece9 --- /dev/null +++ b/src/components/Action/__snapshots__/ActionChoose.spec.tsx.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ActionChoose component should render correctly 1`] = `ReactWrapper {}`; diff --git a/src/components/Action/__snapshots__/ActionModal.spec.tsx.snap b/src/components/Action/__snapshots__/ActionModal.spec.tsx.snap new file mode 100644 index 0000000000000000000000000000000000000000..dedc0fd60ecea5dcd8ce982ec5fefd7fe4d9560b --- /dev/null +++ b/src/components/Action/__snapshots__/ActionModal.spec.tsx.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ActionModal component should render correctly 1`] = `ReactWrapper {}`; diff --git a/src/components/Action/__snapshots__/ActionOnGoing.spec.tsx.snap b/src/components/Action/__snapshots__/ActionOnGoing.spec.tsx.snap new file mode 100644 index 0000000000000000000000000000000000000000..052482d29f5edf445fe51c194a36ee10cf952ce6 --- /dev/null +++ b/src/components/Action/__snapshots__/ActionOnGoing.spec.tsx.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ActionOnGoing component should render correctly 1`] = `ReactWrapper {}`;