From 222f7855ac0161c9fdb8882f7acd79fe7f7c5117 Mon Sep 17 00:00:00 2001 From: Matteo Velludini Date: Mon, 24 Oct 2016 12:29:45 +0200 Subject: [PATCH 1/2] added task functionality --- web/client/actions/tasks.js | 48 ++++++++++++++++++++++++++++++++++++ web/client/reducers/tasks.js | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 web/client/actions/tasks.js create mode 100644 web/client/reducers/tasks.js diff --git a/web/client/actions/tasks.js b/web/client/actions/tasks.js new file mode 100644 index 0000000000..defa4131e0 --- /dev/null +++ b/web/client/actions/tasks.js @@ -0,0 +1,48 @@ +/** + * Copyright 2016, GeoSolutions Sas. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ +const TASK_STARTED = 'TASK_STARTED'; +const TASK_SUCCESS = 'TASK_SUCCESS'; +const TASK_ERROR = 'TASK_ERROR'; + +function taskSuccess(result, name, actionPayload) { + return { + type: TASK_SUCCESS, + result, + name, + actionPayload + }; +} + +function taskStarted(name) { + return { + type: TASK_STARTED, + name + }; +} + +function taskError(error, name) { + return { + type: TASK_ERROR, + error, + name + }; +} + +function startTask(task, taskPayload, name, actionPayload) { + return (dispatch) => { + dispatch(taskStarted(name)); + task(taskPayload, (result) => { + dispatch(taskSuccess(result, name, actionPayload)); + }, (error) => { + dispatch(taskError(error, name, actionPayload)); + }); + }; +} + + +module.exports = {TASK_STARTED, TASK_SUCCESS, TASK_ERROR, startTask}; diff --git a/web/client/reducers/tasks.js b/web/client/reducers/tasks.js new file mode 100644 index 0000000000..bb71dd35fb --- /dev/null +++ b/web/client/reducers/tasks.js @@ -0,0 +1,48 @@ +/** + * Copyright 2016, GeoSolutions Sas. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +var { TASK_STARTED, TASK_SUCCESS, TASK_ERROR } = require('../actions/tasks'); +var assign = require('object-assign'); + +function tasks(state = {}, action) { + switch (action.type) { + case TASK_STARTED: { + return assign({}, state, { + [action.name]: { + running: true + } + }); + } + case TASK_SUCCESS: { + return assign({}, state, { + [action.name]: { + actionPayload: action.actionPayload, + name: action.name, + result: action.result, + running: false, + success: true + } + }); + } + case TASK_ERROR: { + return assign({}, state, { + [action.name]: { + actionPayload: action.actionPayload, + error: action.error, + name: action.name, + running: false, + success: false + } + }); + } + default: + return state; + } +} + +module.exports = tasks; From 1678c7e43dcf6871f8021b97b395ddd22b106ac6 Mon Sep 17 00:00:00 2001 From: Matteo Velludini Date: Mon, 24 Oct 2016 15:31:56 +0200 Subject: [PATCH 2/2] added tests for actions and reducers for tasks --- web/client/actions/__tests__/tasks-test.js | 52 +++++++++++++++++ web/client/actions/tasks.js | 7 ++- web/client/reducers/__tests__/tasks-test.js | 62 +++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 web/client/actions/__tests__/tasks-test.js create mode 100644 web/client/reducers/__tests__/tasks-test.js diff --git a/web/client/actions/__tests__/tasks-test.js b/web/client/actions/__tests__/tasks-test.js new file mode 100644 index 0000000000..bab4882614 --- /dev/null +++ b/web/client/actions/__tests__/tasks-test.js @@ -0,0 +1,52 @@ +/** + * Copyright 2015, GeoSolutions Sas. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +var expect = require('expect'); +var { + taskSuccess, + taskStarted, + taskError, + TASK_STARTED, + TASK_SUCCESS, + TASK_ERROR +} = require('../tasks'); + +describe('Test correctness of the tasks actions', () => { + it('test taskSuccess action', () => { + let result = {value: "true"}; + let name = "myName"; + let actionPayload = null; + const retVal = taskSuccess(result, name, actionPayload); + expect(retVal).toExist(); + expect(retVal.type).toBe(TASK_SUCCESS); + expect(retVal.result).toBe(result); + expect(retVal.name).toBe(name); + expect(retVal.actionPayload).toBe(null); + }); + + it('test taskError action', () => { + let error = {value: "true"}; + let name = "myName"; + let actionPayload = null; + const retVal = taskError(error, name, actionPayload); + expect(retVal).toExist(); + expect(retVal.type).toBe(TASK_ERROR); + expect(retVal.error).toBe(error); + expect(retVal.name).toBe(name); + expect(retVal.actionPayload).toBe(null); + }); + + + it('test taskStarted action', () => { + let name = "myName"; + const retVal = taskStarted(name); + expect(retVal).toExist(); + expect(retVal.type).toBe(TASK_STARTED); + expect(retVal.name).toBe(name); + }); +}); diff --git a/web/client/actions/tasks.js b/web/client/actions/tasks.js index defa4131e0..ec23f2e38a 100644 --- a/web/client/actions/tasks.js +++ b/web/client/actions/tasks.js @@ -25,11 +25,12 @@ function taskStarted(name) { }; } -function taskError(error, name) { +function taskError(error, name, actionPayload) { return { type: TASK_ERROR, error, - name + name, + actionPayload }; } @@ -45,4 +46,4 @@ function startTask(task, taskPayload, name, actionPayload) { } -module.exports = {TASK_STARTED, TASK_SUCCESS, TASK_ERROR, startTask}; +module.exports = {TASK_STARTED, TASK_SUCCESS, TASK_ERROR, startTask, taskSuccess, taskError, taskStarted}; diff --git a/web/client/reducers/__tests__/tasks-test.js b/web/client/reducers/__tests__/tasks-test.js new file mode 100644 index 0000000000..86f7c6ec04 --- /dev/null +++ b/web/client/reducers/__tests__/tasks-test.js @@ -0,0 +1,62 @@ +/** + * Copyright 2016, GeoSolutions Sas. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ +var expect = require('expect'); + +var {TASK_STARTED, TASK_SUCCESS, TASK_ERROR } = require('../../actions/tasks'); +var tasks = require('../tasks'); + +describe('Test the tasks reducer', () => { + + it('test tasks started', () => { + let name = 'started'; + let testAction = { + type: TASK_STARTED, + name: name + }; + let state = tasks({}, testAction); + expect(state).toExist(); + + expect(state[name].running).toBe(true); + }); + + it('test tasks success', () => { + let result = {value: "true"}; + let actionPayload = null; + + let name = 'started'; + let testAction = { + type: TASK_SUCCESS, + name, + result, + actionPayload + }; + let state = tasks({}, testAction); + expect(state[name].name).toBe(name); + expect(state[name].actionPayload).toBe(actionPayload); + expect(state[name].result).toBe(result); + }); + + it('test task error', () => { + let name = 'started'; + let error = {value: "true"}; + let actionPayload = null; + + let testAction = { + type: TASK_ERROR, + name: name, + error, + actionPayload + }; + + let state = tasks({}, testAction); + expect(state[name].name).toBe(name); + expect(state[name].actionPayload).toBe(actionPayload); + expect(state[name].error).toBe(error); + }); + +});