-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathpurity_part_2.js
65 lines (53 loc) · 1.74 KB
/
purity_part_2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Benefit 1: Pure functions are predictable */
const PI = 3.14;
const calculateArea = (radius) => radius * radius * PI;
/*
This impure function is reliant on the global variable PI to make the calculation
The PI is a global variable, so anyone can update it, and automatically change the result of calculateArea function
Inpure functions are unpredictable
*/
/* ------------------------------------------------------ */
/* Benefit 2: Pure functions are easy to test */
/* Imagine test this impure function */
async function amountExceedsBuyLimit(amount) {
const limit = await getBuyLimits(); /* assume API call */
return amount > limit;
}
/* We need to mock the API call to test our function */
describe('amountExceedsBuyLimit', () => {
let scope, limits;
/* Mock API call */
before(() => {
scope = nock('https://myApi.com');
scope
.get('/buyLimit')
.matchHeader('Content-Type', 'application/json')
.reply(200, {
buyLimit: 5000,
});
});
/* Test the function */
it('is false', () => {
const amountExceedsLimit = amountExceedsBuyLimit(200);
expect(amountExceedsLimit).to.be.false();
});
it('is true', () => {
const amountExceedsLimit = amountExceedsBuyLimit(10000);
expect(amountExceedsLimit).to.be.true();
});
});
/* Now imagine test this pure function */
async function amountExceedsBuyLimit(amount, limit) {
return amount > limit;
}
/* Pretty simple */
describe('amountExceedsBuyLimit', () => {
it('is false', () => {
const amountExceedsLimit = amountExceedsBuyLimit(200, 5000);
expect(amountExceedsLimit).to.be.false();
});
it('is true', () => {
const amountExceedsLimit = amountExceedsBuyLimit(10000, 5000);
expect(amountExceedsLimit).to.be.true();
});
});