Skip to content

Commit

Permalink
feat(ch05): add example for no side effects
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Dec 28, 2019
1 parent c5b901e commit 5c94e6c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/ch-05_pure-functions/5.1_pure-functions.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export const double = x => x * 2;

export const addToCart = (cart, product, quantity) => {
return {
...cart,
products: cart.products.concat([{product, quantity}])
}
};
11 changes: 10 additions & 1 deletion src/ch-05_pure-functions/5.1_pure-functions.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from "chai";
import {double} from "./5.1_pure-functions";
import {addToCart, double} from "./5.1_pure-functions";

describe("Pure Functions", () => {
it("should always return the same output for the same input", () => {
Expand All @@ -8,4 +8,13 @@ describe("Pure Functions", () => {
expect(double(3)).to.equal(6);
expect(double(3)).to.equal(6);
});

it("should have no side effects", () => {
const originalCart = {
products: []
};
const newCart = addToCart(originalCart, {name: "product A"}, 3);

expect(originalCart).to.be.not.deep.equal(newCart);
})
});

0 comments on commit 5c94e6c

Please sign in to comment.