diff --git a/packages/marketplace/src/components/pages/admin-dev-management/__tests__/__snapshots__/admin-dev-management.test.tsx.snap b/packages/marketplace/src/components/pages/admin-dev-management/__tests__/__snapshots__/admin-dev-management.test.tsx.snap index aa218a3e65..c2bebee8b3 100644 --- a/packages/marketplace/src/components/pages/admin-dev-management/__tests__/__snapshots__/admin-dev-management.test.tsx.snap +++ b/packages/marketplace/src/components/pages/admin-dev-management/__tests__/__snapshots__/admin-dev-management.test.tsx.snap @@ -759,7 +759,7 @@ exports[`AdminDevManagement should match a snapshot when LOADING false 1`] = ` /> - - - - + /> + diff --git a/packages/marketplace/src/components/ui/__tests__/__snapshots__/developer-set-status.tsx.snap b/packages/marketplace/src/components/ui/__tests__/__snapshots__/developer-set-status.tsx.snap index 175c07b23f..993f644331 100644 --- a/packages/marketplace/src/components/ui/__tests__/__snapshots__/developer-set-status.tsx.snap +++ b/packages/marketplace/src/components/ui/__tests__/__snapshots__/developer-set-status.tsx.snap @@ -1,48 +1,33 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`SetDeveloperStatusModal should match snapshot 1`] = ` - - - - Are you sure you want to - deactivate - ‘ - ’? - + + } +> - - Confirm - - - Cancel - - + developer={ + Object { + "id": "", + "isInactive": false, + } } - /> - + onSuccess={[Function]} + visible={true} + > + + + `; diff --git a/packages/marketplace/src/components/ui/__tests__/developer-set-status.tsx b/packages/marketplace/src/components/ui/__tests__/developer-set-status.tsx index f229e87bc0..84828266aa 100644 --- a/packages/marketplace/src/components/ui/__tests__/developer-set-status.tsx +++ b/packages/marketplace/src/components/ui/__tests__/developer-set-status.tsx @@ -1,55 +1,73 @@ import React from 'react' -import { shallow } from 'enzyme' +import * as ReactRedux from 'react-redux' +import { mount } from 'enzyme' +import configureStore from 'redux-mock-store' +import appState from '@/reducers/__stubs__/app-state' import { SetDeveloperStatusModal, SetDeveloperStatusProps, onAfterCloseHandler, onSuccessHandler, + onConfirmButtonClick, } from '../developer-set-status' +import { developerSetStatusSetInitFormState, developerSetStatusRequest } from '@/actions/developer-set-status' +import { developerStub } from '@/sagas/__stubs__/developer' + +const props: SetDeveloperStatusProps = { + developer: { id: '', isInactive: false }, + onSuccess: () => jest.fn(), + visible: true, +} describe('SetDeveloperStatusModal', () => { + let store + let spyDispatch + beforeEach(() => { + /* mocking store */ + const mockStore = configureStore() + store = mockStore(appState) + spyDispatch = jest.spyOn(ReactRedux, 'useDispatch').mockImplementation(() => store.dispatch) + }) + it('should match snapshot', () => { - const props = { - developer: { id: '', isInactive: false }, - developerName: '', - onSuccess: () => jest.fn(), - formState: 'PENDING', - visible: true, - afterClose: () => jest.fn(), - developerSetStatusRequest: () => jest.fn(), - resetDeveloperSetStatusReducer: () => jest.fn(), - } as SetDeveloperStatusProps - - const wrapper = shallow() + const wrapper = mount( + + + , + ) expect(wrapper).toMatchSnapshot() }) -}) -describe('onAfterCloseHandler', () => { - it('should return a function when executing', () => { - const mockAfterClose = jest.fn() + describe('onAfterCloseHandler', () => { + it('should return a function when executing', () => { + const mockAfterClose = jest.fn() - const onAfterCloseHandlerFn = onAfterCloseHandler({ afterClose: mockAfterClose, isLoading: false }) - expect(onAfterCloseHandlerFn).toBeDefined() + const onAfterCloseHandlerFn = onAfterCloseHandler(false, mockAfterClose) + expect(onAfterCloseHandlerFn).toBeDefined() - onAfterCloseHandlerFn() - expect(mockAfterClose).toBeCalled() + onAfterCloseHandlerFn() + expect(mockAfterClose).toBeCalled() + }) }) -}) -describe('onSuccessHandler', () => { - it('should return a function when executing', () => { - const mockOnSuccess = jest.fn() - const mockResetDeveloperSetStatusReducer = jest.fn() + describe('onSuccessHandler', () => { + it('should return a function when executing', () => { + const mockOnSuccess = jest.fn() + const onSuccessHandlerFn = onSuccessHandler(mockOnSuccess, spyDispatch) + expect(onSuccessHandlerFn).toBeDefined() - const onSuccessHandlerFn = onSuccessHandler({ - onSuccess: mockOnSuccess, - resetDeveloperSetStatusReducer: mockResetDeveloperSetStatusReducer, + onSuccessHandlerFn() + expect(mockOnSuccess).toBeCalled() + expect(spyDispatch).toBeCalledWith(developerSetStatusSetInitFormState()) }) - expect(onSuccessHandlerFn).toBeDefined() + }) - onSuccessHandlerFn() - expect(mockOnSuccess).toBeCalled() - expect(mockResetDeveloperSetStatusReducer).toBeCalled() + describe('onConfirmButtonClick', () => { + it('should run correctly', () => { + const mockIsInactive = false + const fn = onConfirmButtonClick(developerStub, spyDispatch, mockIsInactive) + fn() + expect(spyDispatch).toBeCalledWith(developerSetStatusRequest({ ...developerStub, isInactive: !mockIsInactive })) + }) }) }) diff --git a/packages/marketplace/src/components/ui/developer-set-status.tsx b/packages/marketplace/src/components/ui/developer-set-status.tsx index 1200074b54..ebd0f51ef7 100644 --- a/packages/marketplace/src/components/ui/developer-set-status.tsx +++ b/packages/marketplace/src/components/ui/developer-set-status.tsx @@ -1,50 +1,58 @@ import * as React from 'react' import { Dispatch } from 'redux' -import { connect } from 'react-redux' +import { useSelector, useDispatch } from 'react-redux' import { Button, Modal, ModalBody, ModalHeader, ModalFooter, SubTitleH6, ModalProps } from '@reapit/elements' import CallToAction from '@/components/ui/call-to-action' -import { ReduxState, FormState } from '@/types/core' import { developerSetStatusRequest, developerSetStatusSetInitFormState } from '@/actions/developer-set-status' import { DeveloperModel } from '@reapit/foundations-ts-definitions' +import { selectDeveloperSetStatusFormState } from '@/selector/developer-set-status' -export interface SetDeveloperStatusModal { +export type SetDeveloperStatusProps = Pick & { onSuccess: () => void developer: DeveloperModel } -export interface SetDeveloperStatusMappedProps { - formState: FormState +export const onAfterCloseHandler = (isLoading: boolean, afterClose?: () => void) => { + return () => { + if (!isLoading && afterClose) { + afterClose() + } + } } -export interface SetDeveloperStatusMappedActions { - developerSetStatusRequest: (developer: DeveloperModel) => void - resetDeveloperSetStatusReducer: () => void + +export const onSuccessHandler = (onSuccess: () => void, dispatch: Dispatch) => { + return () => { + dispatch(developerSetStatusSetInitFormState()) + onSuccess() + } } -export type SetDeveloperStatusProps = Pick & - SetDeveloperStatusMappedProps & - SetDeveloperStatusMappedActions & - SetDeveloperStatusModal +export const onConfirmButtonClick = (developer: DeveloperModel, dispatch: Dispatch, isInactive?: boolean) => { + return () => { + dispatch(developerSetStatusRequest({ ...developer, isInactive: !isInactive })) + } +} -export const SetDeveloperStatusModal = ({ +export const SetDeveloperStatusModal: React.FC = ({ afterClose, visible, onSuccess, - formState, developer, - developerSetStatusRequest, - resetDeveloperSetStatusReducer, -}: SetDeveloperStatusProps) => { +}) => { + const dispatch = useDispatch() + const formState = useSelector(selectDeveloperSetStatusFormState) const isLoading = Boolean(formState === 'SUBMITTING') const isSucceeded = Boolean(formState === 'SUCCESS') const { isInactive, name } = developer + return ( - + <> {isSucceeded ? ( Developer ‘{name}’ has been {isInactive ? 'activated' : 'deactivated'} successfully. @@ -53,7 +61,7 @@ export const SetDeveloperStatusModal = ({ <> developerSetStatusRequest({ ...developer, isInactive: !isInactive })} + onClick={onConfirmButtonClick(developer, dispatch, isInactive)} > Confirm @@ -86,25 +94,4 @@ export const SetDeveloperStatusModal = ({ ) } -export const onAfterCloseHandler = ({ afterClose, isLoading }) => () => { - if (!isLoading && afterClose) { - afterClose() - return - } -} - -export const onSuccessHandler = ({ onSuccess, resetDeveloperSetStatusReducer }) => () => { - resetDeveloperSetStatusReducer() - onSuccess() -} - -export const mapStateToProps = (state: ReduxState): SetDeveloperStatusMappedProps => ({ - formState: state.developerSetStatus.formState, -}) - -export const mapDispatchToProps = (dispatch: Dispatch): SetDeveloperStatusMappedActions => ({ - developerSetStatusRequest: (developer: DeveloperModel) => dispatch(developerSetStatusRequest(developer)), - resetDeveloperSetStatusReducer: () => dispatch(developerSetStatusSetInitFormState()), -}) - -export default connect(mapStateToProps, mapDispatchToProps)(SetDeveloperStatusModal) +export default SetDeveloperStatusModal diff --git a/packages/marketplace/src/selector/__tests__/developer-set-status.test.ts b/packages/marketplace/src/selector/__tests__/developer-set-status.test.ts new file mode 100644 index 0000000000..bc011050b5 --- /dev/null +++ b/packages/marketplace/src/selector/__tests__/developer-set-status.test.ts @@ -0,0 +1,20 @@ +import { ReduxState } from '@/types/core' +import { selectDeveloperSetStatusFormState } from '../developer-set-status' + +describe('selectAppDeleteFormState', () => { + it('should run correctly', () => { + const input = { + developerSetStatus: { + formState: 'PENDING', + }, + } as ReduxState + const result = selectDeveloperSetStatusFormState(input) + expect(result).toEqual('PENDING') + }) + + it('should run correctly and return []', () => { + const input = {} as ReduxState + const result = selectDeveloperSetStatusFormState(input) + expect(result).toEqual(undefined) + }) +}) diff --git a/packages/marketplace/src/selector/developer-set-status.ts b/packages/marketplace/src/selector/developer-set-status.ts new file mode 100644 index 0000000000..d09167bd7e --- /dev/null +++ b/packages/marketplace/src/selector/developer-set-status.ts @@ -0,0 +1,5 @@ +import { ReduxState, FormState } from '@/types/core' + +export const selectDeveloperSetStatusFormState = (state: ReduxState): FormState => { + return state.developerSetStatus?.formState +}