From e6c4366c94a539c2a93c577755a4c153bc58677b Mon Sep 17 00:00:00 2001
From: markgalante <50637189+markgalante@users.noreply.github.com>
Date: Tue, 23 Jun 2020 20:46:57 +0200
Subject: [PATCH] initial commit
---
Counter.js | 37 ++++++++++++++++---------------------
main.js | 37 +++++++++++++++++++++++--------------
sagas.js | 22 ++++++++++++++++++++++
sagas.spec.js | 29 +++++++++++++++++++++++++++++
4 files changed, 90 insertions(+), 35 deletions(-)
create mode 100644 sagas.js
create mode 100644 sagas.spec.js
diff --git a/Counter.js b/Counter.js
index 04222ed..48794f5 100644
--- a/Counter.js
+++ b/Counter.js
@@ -1,25 +1,20 @@
/*eslint-disable no-unused-vars */
-import React, { Component, PropTypes } from 'react'
+import React, { Component } from "react";
-const Counter = ({ value, onIncrement, onDecrement }) =>
-
-
- {' '}
-
-
-
- Clicked: {value} times
-
-
+const Counter = ({ value, onIncrement, onDecrement, onIncrementAsync }) => (
+
+
+
{" "}
+
+
+
Clicked: {value} times
+
+);
-Counter.propTypes = {
- value: PropTypes.number.isRequired,
- onIncrement: PropTypes.func.isRequired,
- onDecrement: PropTypes.func.isRequired
-}
+// Counter.propTypes = {
+// value: PropTypes.number.isRequired,
+// onIncrement: PropTypes.func.isRequired,
+// onDecrement: PropTypes.func.isRequired
+// }
-export default Counter
+export default Counter;
diff --git a/main.js b/main.js
index e9cdd5f..c173fbd 100644
--- a/main.js
+++ b/main.js
@@ -1,25 +1,34 @@
-import "babel-polyfill"
+import "babel-polyfill";
-import React from 'react'
-import ReactDOM from 'react-dom'
-import { createStore, applyMiddleware } from 'redux'
+import React from "react";
+import ReactDOM from "react-dom";
+import { createStore, applyMiddleware } from "redux";
+import createSagaMiddleware from "redux-saga";
-import Counter from './Counter'
-import reducer from './reducers'
+import Counter from "./Counter";
+import reducer from "./reducers";
+import rootSaga from "./sagas";
+// import { helloSaga } from "./sagas";
-const store = createStore(reducer)
+const sagaMiddleware = createSagaMiddleware();
-const action = type => store.dispatch({type})
+const store = createStore(reducer, applyMiddleware(sagaMiddleware));
+
+sagaMiddleware.run(rootSaga);
+
+const action = type => store.dispatch({ type });
function render() {
ReactDOM.render(
action('INCREMENT')}
- onDecrement={() => action('DECREMENT')} />,
- document.getElementById('root')
- )
+ onIncrement={() => action("INCREMENT")}
+ onDecrement={() => action("DECREMENT")}
+ onIncrementAsync={() => action("INCREMENT_ASYNC")}
+ />,
+ document.getElementById("root")
+ );
}
-render()
-store.subscribe(render)
+render();
+store.subscribe(render);
diff --git a/sagas.js b/sagas.js
new file mode 100644
index 0000000..5fb6e87
--- /dev/null
+++ b/sagas.js
@@ -0,0 +1,22 @@
+import { takeEvery, put, all, call } from "redux-saga/effects";
+
+export const delay = ms => new Promise(res => setTimeout(res, ms));
+
+export function* helloSaga() {
+ yield console.log("Hello World!");
+}
+
+//WORKER SAGA: will perform the async increment task:
+export function* incrementAsync() {
+ yield call(delay, 1000);
+ yield put({ type: "INCREMENT" });
+}
+
+//WATCHER SAGA: spawns a new incrementAsync task on each INCREMENT_ASYNC:
+export function* watchIncrementAsync() {
+ yield takeEvery("INCREMENT_ASYNC", incrementAsync);
+}
+
+export default function* rootSaga() {
+ yield all([helloSaga(), watchIncrementAsync()]);
+}
diff --git a/sagas.spec.js b/sagas.spec.js
new file mode 100644
index 0000000..6594566
--- /dev/null
+++ b/sagas.spec.js
@@ -0,0 +1,29 @@
+import test from "tape";
+
+import { put, call } from "redux-saga/effects";
+
+import { incrementAsync, delay } from "./sagas";
+
+test("incrementAsync Saga test", assert => {
+ const gen = incrementAsync();
+
+ assert.deepEqual(
+ gen.next().value,
+ call(delay, 1000),
+ "incrementAsync Saga must call delay(1000)"
+ );
+
+ assert.deepEqual(
+ gen.next().value,
+ put({ type: "INCREMENT" }),
+ "incrementAsync Saga must dispatch an action"
+ );
+
+ assert.deepEqual(
+ gen.next(),
+ { done: true, value: undefined },
+ "incrementAsync Saga must be done"
+ );
+
+ assert.end();
+});