Skip to content

Commit fa8f1e4

Browse files
authored
Merge branch 'main' into enhance-documentation
2 parents a6ca2cf + 7d74fb7 commit fa8f1e4

File tree

6 files changed

+63
-22
lines changed

6 files changed

+63
-22
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @backhooks/core
22

3+
## 0.2.1
4+
5+
### Patch Changes
6+
7+
- 386ea5d: Updated README with new resetGlobalContext function
8+
- ea23d07: Added the possibility to reset the global context
9+
310
## 0.2.0
411

512
### Minor Changes

packages/core/README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,54 @@ npm install @backhooks/core
1414

1515
This allows you to create a hook. Example of creating a simple hook:
1616

17-
````ts
17+
```ts
1818
const [useCount, configureCountHook] = createHook({
19-
data () {
19+
data() {
2020
return {
21-
count: 0
22-
}
21+
count: 0,
22+
};
23+
},
24+
execute(state) {
25+
state.count++;
26+
return state.count;
2327
},
24-
execute (state) {
25-
state.count++
26-
return state.count
27-
}
28-
})
28+
});
29+
```
2930

3031
`runHookContext: <T>(fn: () => T): Promise<T>`
3132

3233
This allows you to run a hook context. Any hook called within the `runHookContext` callback will have a specific state attached to it:
3334

3435
```ts
3536
runHookContext(() => {
36-
console.log(useCount()) // 1
37-
console.log(useCount()) // 2
38-
console.log(useCount()) // 3
39-
})
37+
console.log(useCount()); // 1
38+
console.log(useCount()); // 2
39+
console.log(useCount()); // 3
40+
});
4041

4142
runHookContext(() => {
42-
console.log(useCount()) // 1
43-
console.log(useCount()) // 2
44-
console.log(useCount()) // 3
45-
})
46-
````
43+
console.log(useCount()); // 1
44+
console.log(useCount()); // 2
45+
console.log(useCount()); // 3
46+
});
47+
```
48+
49+
`resetGlobalContext()`
50+
51+
This allows you to reset the global context. It can be very useful for testing purposes
52+
53+
```ts
54+
beforeEach(() => {
55+
resetGlobalContext();
56+
});
57+
58+
test("it should count", async () => {
59+
const count = useCount();
60+
expect(count).toBe(1); // true
61+
});
62+
63+
test("it should also count", async () => {
64+
const count = useCount();
65+
expect(count).toBe(1); // also true
66+
});
67+
```

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@backhooks/core",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"scripts": {
55
"build": "tsc",
66
"test": "jest"

packages/core/src/core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Optional } from "utility-types";
44

55
const asyncLocalStorage = new AsyncLocalStorage();
66

7-
const globalStore = {};
7+
let globalStore = {};
88

99
interface CreateHookOptions<
1010
State extends Record<string, any>,
@@ -77,3 +77,7 @@ export const runHookContext = async <T>(
7777
const result = (await asyncLocalStorage.run({}, fn)) as T;
7878
return result;
7979
};
80+
81+
export const resetGlobalContext = () => {
82+
globalStore = {};
83+
};

packages/core/tests/core.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { runHookContext, createHook } from "../src/index";
1+
import { runHookContext, createHook, resetGlobalContext } from "../src/index";
22

33
const [useRandom, configureRandomHook] = createHook({
44
data() {
@@ -103,3 +103,12 @@ test("it should be able to create a hook without name", async () => {
103103
const result = useHook();
104104
expect(result).toBe("ok");
105105
});
106+
107+
test("it should be able to reset the global context", async () => {
108+
const random = useRandom();
109+
const random2 = useRandom();
110+
expect(random).toBe(random2);
111+
resetGlobalContext();
112+
const random3 = useRandom();
113+
expect(random).not.toBe(random3);
114+
});

0 commit comments

Comments
 (0)