pure-effect 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,13 +26,13 @@ const validateRegistration = (input) => {
|
|
|
26
26
|
// These functions do NOT run the DB call. They return a Command object.
|
|
27
27
|
// The 'next' function defines what happens with the result of the async call.
|
|
28
28
|
const findUser = (email) => {
|
|
29
|
-
const
|
|
29
|
+
const cmdFindUser = () => db.findUser(email); // The work to do later
|
|
30
30
|
const next = (user) => Success(user); // Wrap result in Success
|
|
31
31
|
return Command(cmd, next);
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
const saveUser = (input) => {
|
|
35
|
-
const
|
|
35
|
+
const cmdSaveUser = () => db.saveUser(input);
|
|
36
36
|
const next = (saved) => Success(saved);
|
|
37
37
|
return Command(cmd, next);
|
|
38
38
|
};
|
|
@@ -51,7 +51,7 @@ const registerUserFlow = (input) =>
|
|
|
51
51
|
)(input);
|
|
52
52
|
|
|
53
53
|
// The Imperative Shell
|
|
54
|
-
async function
|
|
54
|
+
async function registerUser() {
|
|
55
55
|
const input = { email: 'new@test.com', password: 'password123' };
|
|
56
56
|
|
|
57
57
|
// logic is just a data structure until we pass it to runEffect
|
|
@@ -66,8 +66,6 @@ async function main() {
|
|
|
66
66
|
console.error('Error:', result.error);
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
|
-
|
|
70
|
-
main();
|
|
71
69
|
```
|
|
72
70
|
|
|
73
71
|
## Testing Without Mocks
|
|
@@ -75,8 +73,6 @@ main();
|
|
|
75
73
|
The biggest benefit of **Pure Effect** is testability. Because `registerUserFlow` returns a data structure (a tree of objects) instead of running a Promise, you can test your logic without mocking the database.
|
|
76
74
|
|
|
77
75
|
```js
|
|
78
|
-
import assert from 'assert';
|
|
79
|
-
|
|
80
76
|
// 1. Test Validation Failure
|
|
81
77
|
const badInput = { email: 'bad-email', password: '123' };
|
|
82
78
|
const result = registerUserFlow(badInput);
|
|
@@ -90,7 +86,12 @@ const step1 = registerUserFlow(goodInput);
|
|
|
90
86
|
|
|
91
87
|
// Check if the first thing the code does is try to find a user
|
|
92
88
|
assert.equal(step1.type, 'Command');
|
|
93
|
-
assert.equal(step1.cmd.name, '
|
|
89
|
+
assert.equal(step1.cmd.name, 'cmdFindUser');
|
|
90
|
+
|
|
91
|
+
// Check if the next thing the code will do is to save a user
|
|
92
|
+
const step2 = step1.next(null);
|
|
93
|
+
assert.equal(step2.type, 'Command');
|
|
94
|
+
assert.equal(step2.cmd.name, 'cmdSaveUser');
|
|
94
95
|
// ✅ We verified the *intent* of the code without touching a real DB.
|
|
95
96
|
```
|
|
96
97
|
|