@@ -14,33 +14,54 @@ npm install @backhooks/core
1414
1515This allows you to create a hook. Example of creating a simple hook:
1616
17- ```` ts
17+ ``` ts
1818const [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
3233This 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
3536runHookContext (() => {
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
4142runHookContext (() => {
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+ ```
0 commit comments