Skip to content

Instantly share code, notes, and snippets.

@th3mon
Last active September 21, 2017 13:47
Show Gist options
  • Save th3mon/cb18e120706eb5f580ba2a7887c20267 to your computer and use it in GitHub Desktop.
Save th3mon/cb18e120706eb5f580ba2a7887c20267 to your computer and use it in GitHub Desktop.
Redux, reducer
'use strict';
console.clear();
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;
}
};
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: 'INCREMENT' }));
expect(counter(undefined, {})).toEqual(0);
console.log('Tests passed!');
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.min.js"></script>
<script id="jsbin-source-javascript" type="text/javascript">'use strict';
console.clear();
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
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: 'INCREMENT' })
);
expect(
counter(undefined, {})
).toEqual(0);
console.log('Tests passed!');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment