Skip to content

Instantly share code, notes, and snippets.

@giopunt
Created July 3, 2017 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giopunt/812bc857967066c62a96f0060dcf00c0 to your computer and use it in GitHub Desktop.
Save giopunt/812bc857967066c62a96f0060dcf00c0 to your computer and use it in GitHub Desktop.
BASIC REDUX IMPMENTATION // source https://jsbin.com/honisu
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>BASIC REDUX IMPMENTATION</title>
<script src="https://unpkg.com/expect/umd/expect.min.js"></script>
</head>
<body>
<h1>BASIC REDUX IMPMENTATION</h1>
<p>DevTools (F12) -> see the magic</p>
<script id="jsbin-javascript">
"use strict";
var counter = function counter(state, action) {
if (state === undefined) state = 0;
switch (action.type) {
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
default:
return state;
}
return state;
};
expect(counter(0, {
type: "INCREMENT"
})).toEqual(1);
expect(counter(1, {
type: "INCREMENT"
})).toEqual(2);
expect(counter(2, {
type: "DECREMENT"
})).toEqual(1);
expect(counter(1, {
type: "DECREMENT"
})).toEqual(0);
expect(counter(1, {
type: "SOMETHING_ELSE"
})).toEqual(1);
console.log("ALL TEST PASSED !!");
</script>
<script id="jsbin-source-javascript" type="text/javascript">const counter = (state = 0, action) => {
switch(action.type){
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
default:
return state;
}
return state;
}
expect(
counter(0, {
type: "INCREMENT"
})
).toEqual(1)
expect(
counter(1, {
type: "INCREMENT"
})
).toEqual(2)
expect(
counter(2, {
type: "DECREMENT"
})
).toEqual(1)
expect(
counter(1, {
type: "DECREMENT"
})
).toEqual(0)
expect(
counter(1, {
type: "SOMETHING_ELSE"
})
).toEqual(1)
console.log("ALL TEST PASSED !!")</script></body>
</html>
"use strict";
var counter = function counter(state, action) {
if (state === undefined) state = 0;
switch (action.type) {
case "INCREMENT":
return state + 1;
case "DECREMENT":
return state - 1;
default:
return state;
}
return state;
};
expect(counter(0, {
type: "INCREMENT"
})).toEqual(1);
expect(counter(1, {
type: "INCREMENT"
})).toEqual(2);
expect(counter(2, {
type: "DECREMENT"
})).toEqual(1);
expect(counter(1, {
type: "DECREMENT"
})).toEqual(0);
expect(counter(1, {
type: "SOMETHING_ELSE"
})).toEqual(1);
console.log("ALL TEST PASSED !!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment