From 24cc027bdc476d7b280d0a4c7cca0cfd39555588 Mon Sep 17 00:00:00 2001 From: Peter Hudec Date: Wed, 17 Jul 2024 11:38:33 +0100 Subject: [PATCH] Added the Task/RecentResult component --- src/client/components/Task/RecentResult.js | 28 +++++ src/client/reducers.js | 3 + .../cypress/specs/Task/RecentResult.cy.jsx | 108 ++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/client/components/Task/RecentResult.js create mode 100644 test/component/cypress/specs/Task/RecentResult.cy.jsx diff --git a/src/client/components/Task/RecentResult.js b/src/client/components/Task/RecentResult.js new file mode 100644 index 00000000000..20ee811ecb7 --- /dev/null +++ b/src/client/components/Task/RecentResult.js @@ -0,0 +1,28 @@ +import { TASK__START } from '../../actions' +import multiinstance from '../../utils/multiinstance' + +export default multiinstance({ + name: 'Task/RecentResult', + actionPattern: /.*/, + idProp: 'name', + reducer: (state, { type, id, onSuccessDispatch, result }) => { + switch (type) { + case TASK__START: + return { + ...state, + [id]: { + ...state?.[id], + successActionType: onSuccessDispatch, + }, + } + case state?.[id]?.successActionType: + return { + ...state, + [id]: { result }, + } + default: + return state + } + }, + component: ({ children, id, ...props }) => children(props[id]?.result), +}) diff --git a/src/client/reducers.js b/src/client/reducers.js index 3b85e2eb1fa..b5e53b489d0 100644 --- a/src/client/reducers.js +++ b/src/client/reducers.js @@ -190,6 +190,8 @@ import companyActivityReducerNoAs from './modules/Companies/CompanyActivity/redu import { ResendExportWin } from './modules/ExportWins/Form/ResendExportWin' +import RecentTaskResult from './components/Task/RecentResult' + export const reducers = { tasks, [FLASH_MESSAGE_ID]: flashMessageReducer, @@ -211,6 +213,7 @@ export const reducers = { ...Form.reducerSpread, ...FieldAddAnother.reducerSpread, ...ResendExportWin.reducerSpread, + ...RecentTaskResult.reducerSpread, [DNB_CHECK_ID]: dnbCheckReducer, [INVESTMENT_OPPORTUNITIES_LIST_ID]: investmentOpportunitiesListReducer, [INVESTMENT_OPPORTUNITIES_DETAILS_ID]: investmentOpportunitiesDetailsReducer, diff --git a/test/component/cypress/specs/Task/RecentResult.cy.jsx b/test/component/cypress/specs/Task/RecentResult.cy.jsx new file mode 100644 index 00000000000..6acbcd4fb6f --- /dev/null +++ b/test/component/cypress/specs/Task/RecentResult.cy.jsx @@ -0,0 +1,108 @@ +/* eslint-disable prettier/prettier */ +import React from 'react' + +import RecentResult from "../../../../../src/client/components/Task/RecentResult" +import Task from "../../../../../src/client/components/Task" + +describe('Task/RecentResult', () => { + it('Should provide most recent result of a given task', () => { + const TASK_CALLS = [ + { + name: 'double', + id: 'aaa', + payload: 1, + expectedResult: 2, + }, + { + name: 'double', + id: 'bbb', + payload: 3, + expectedResult: 6, + }, + { + name: 'plusMillion', + id: 'aaa', + payload: 1, + expectedResult: 1_000_001, + }, + { + name: 'double', + id: 'aaa', + payload: 10, + expectedResult: 20, + }, + { + name: 'plusMillion', + id: 'bbb', + payload: 111, + expectedResult: 1_000_111, + }, + { + name: 'double', + id: 'bbb', + payload: 444, + expectedResult: 888, + }, + { + name: 'plusMillion', + id: 'aaa', + payload: 222, + expectedResult: 1_000_222, + }, + ] + + const TASKS = TASK_CALLS.reduce((a, {name, id}) => ({ + ...a, + [`${name}-${id}`]: {name, id}, + }), {}) + + cy.mountWithProvider( + <> + + {t => + // A utility to start tasks +
{ + e.preventDefault() + t(e.target.taskName.value, e.target.taskId.value).start({ + payload: parseInt(e.target.payload.value, 10), + onSuccessDispatch: `ACTION_NAME-${Math.random()}`, + }) + }}> + + + + +
+ } +
+ {/* Render most recent result of each task */} + + , + { + tasks: { + double: (payload) => payload * 2, + plusMillion: (payload) => payload + 1_000_000, + }, + } + ) + + TASK_CALLS.forEach(({name, id, payload, expectedResult}) => { + cy.get('input[name="taskName"]').clear().type(name) + cy.get('input[name="taskId"]').clear().type(id) + cy.get('input[name="payload"]').clear().type(payload) + cy.get('button').click() + cy.contains(`${name}-${id}: ${expectedResult}`) + }) + }) +})