@@ -52,43 +52,76 @@ bun run prepare
52
52
53
53
## Architecture and Code Structure
54
54
55
- ### Core API
55
+ ### Core API (v2.0.0)
56
56
57
57
The library exports a single file ` lib/flutter_hooks_test.dart ` containing:
58
58
59
- 1 . ** ` buildHook<T, P> ` ** - Main function to test hooks
59
+ 1 . ** ` buildHook<T>() ` ** - Test hooks without props
60
60
- Generic ` T ` : Return type of the hook
61
- - Generic ` P ` : Props type for parameterized hooks
62
- - Returns ` _HookTestingAction<T> ` with methods:
61
+ - Returns ` HookResult<T> ` with methods:
63
62
- ` current ` : Access current hook value
64
- - ` rebuild([props] ) ` : Trigger rebuild with optional new props
63
+ - ` rebuild() ` : Trigger rebuild and update history
65
64
- ` unmount() ` : Unmount the hook
65
+ - ` all ` : Build history for debugging
66
+ - ` buildCount ` : Number of builds
66
67
67
- 2 . ** ` act ` ** - Wraps state changes to ensure proper Flutter test lifecycle
68
+ 2 . ** ` buildHookWithProps<T, P>() ` ** - Test hooks with props
69
+ - Generic ` T ` : Return type, ` P ` : Props type
70
+ - Returns ` HookResultWithProps<T, P> ` with additional:
71
+ - ` rebuildWithProps(P props) ` : Rebuild with new props
72
+
73
+ 3 . ** ` act ` ** - Wraps state changes to ensure proper Flutter test lifecycle
74
+
75
+ 4 . ** ` waitFor ` utilities** - Async testing helpers
76
+ - ` waitFor(condition) ` : Wait for condition to be true
77
+ - ` waitForValueToChange(getValue) ` : Wait for value change
78
+ - ` result.waitForNextUpdate() ` : Wait for hook rebuild
79
+ - ` result.waitForValueToMatch(predicate) ` : Wait for specific condition
68
80
- Similar to React's ` act ` function
69
81
- Required when changing hook state
70
82
71
- ### Testing Pattern
83
+ ### Testing Patterns (v2.0.0)
72
84
73
85
``` dart
74
- // Basic hook test structure
75
- final result = await buildHook((_ ) => useMyHook());
86
+ // Basic hook test (no props)
87
+ final result = await buildHook(() => useMyHook());
76
88
await act(() => result.current.doSomething());
77
89
expect(result.current.value, expectedValue);
78
90
91
+ // Hook with props
92
+ final result = await buildHookWithProps(
93
+ (count) => useCounter(count),
94
+ initialProps: 5,
95
+ );
96
+ await result.rebuildWithProps(10);
97
+ expect(result.current.value, 10);
98
+
79
99
// With wrapper widget
80
100
final result = await buildHook(
81
- (_ ) => useMyHook(),
101
+ () => useMyHook(),
82
102
wrapper: (child) => Provider(child: child),
83
103
);
104
+
105
+ // Async testing with waitFor
106
+ await act(() => result.current.startAsync());
107
+ await waitFor(() => !result.current.isLoading);
108
+ expect(result.current.data, isNotNull);
109
+
110
+ // History tracking
111
+ expect(result.buildCount, 1);
112
+ expect(result.all.length, 1);
113
+ await result.rebuild();
114
+ expect(result.buildCount, 2);
84
115
```
85
116
86
- ### Internal Implementation
117
+ ### Internal Implementation (v2.0.0)
87
118
88
119
- Uses ` TestWidgetsFlutterBinding ` for test environment
89
- - Creates a minimal widget tree with ` HookBuilder `
90
- - Manages completer-based async operations for mount/unmount
91
- - Preserves hook state across rebuilds using keys
120
+ - Creates minimal widget tree with ` HookBuilder `
121
+ - ` _BaseHookResult<T> ` base class eliminates code duplication
122
+ - Build history tracking with automatic value capture
123
+ - Separate result classes for hooks with/without props
124
+ - Helper functions (` _applyWrapper ` ) and constants (` _kEmptyWidget ` )
92
125
93
126
## Testing Approach
94
127
@@ -104,40 +137,46 @@ final result = await buildHook(
104
137
- Pre-commit hooks format code automatically
105
138
- CI runs on Ubuntu with asdf version management
106
139
107
- ## Important Conventions
140
+ ## Important Conventions (v2.0.0)
108
141
109
- 1 . ** Type Safety** : Always specify generic types when using ` buildHook `
110
- 2 . ** Act Wrapper** : Always wrap state changes in ` act() `
111
- 3 . ** Async Handling** : Most operations return Futures - use ` await `
112
- 4 . ** Testing** : Test both happy paths and edge cases (mount/unmount/rebuild)
142
+ 1 . ** API Selection** : Use ` buildHook() ` for hooks without props, ` buildHookWithProps() ` for hooks with props
143
+ 2 . ** Type Safety** : Generic types are automatically inferred in most cases
144
+ 3 . ** Act Wrapper** : Always wrap state changes in ` act() `
145
+ 4 . ** Async Testing** : Use ` waitFor ` utilities for async operations
146
+ 5 . ** History Tracking** : Use ` result.all ` and ` result.buildCount ` for debugging
147
+ 6 . ** Rebuilds** : Call ` rebuild() ` after state changes to update history
148
+ 7 . ** Testing** : Test mount/unmount/rebuild scenarios and async state changes
113
149
114
150
## Version Requirements
115
151
116
152
- Dart SDK: ` >=2.17.0 <4.0.0 `
117
153
- Flutter: ` >=3.20.0 `
118
154
- Uses Flutter hooks ` ^0.21.2 `
119
155
120
- ## Release Process
156
+ ## Release Process (v2.0.0+)
121
157
122
- Releases are fully automated via GitHub Actions:
158
+ Releases are fully automated via GitHub Actions with OIDC authentication :
123
159
124
160
### Creating a Release
125
161
126
162
1 . ** Update version** : Update version in ` pubspec.yaml `
127
- 2 . ** Update changelog** : Run ` git cliff --unreleased --tag v1.0.1 --output CHANGELOG.md `
128
- 3 . ** Commit changes** : ` git commit -am "chore(release): prepare for v1.0.1" `
129
- 4 . ** Create tag** : ` git tag v1.0.1 `
130
- 5 . ** Push** : ` git push origin main --tags `
163
+ 2 . ** Commit changes** : ` git commit -am "chore: bump version to v2.0.1" `
164
+ 3 . ** Create tag** : ` git tag v2.0.1 `
165
+ 4 . ** Push** : ` git push origin main --tags `
166
+
167
+ Changelog is automatically generated from conventional commits.
131
168
132
169
### Automated Release Steps
133
170
134
171
When a tag matching ` v[0-9]+.[0-9]+.[0-9]+* ` is pushed:
135
172
136
- 1 . ** CI Validation** : All tests, formatting, and analysis must pass
137
- 2 . ** Dry Run** : Package publication is tested
138
- 3 . ** Release Notes** : Auto-generated using git-cliff from conventional commits
139
- 4 . ** GitHub Release** : Created with generated release notes
140
- 5 . ** pub.dev Publication** : Package is published automatically
173
+ 1 . ** Environment Setup** : Flutter, Dart, Bun, and Melos
174
+ 2 . ** Dependencies** : Install and bootstrap packages with Melos and Bun
175
+ 3 . ** CI Validation** : All tests, formatting, and analysis must pass
176
+ 4 . ** Dry Run** : Package publication tested with OIDC authentication
177
+ 5 . ** Release Notes** : Auto-generated using git-cliff from conventional commits
178
+ 6 . ** GitHub Release** : Created with generated release notes
179
+ 7 . ** pub.dev Publication** : Published automatically with OIDC (no token required)
141
180
142
181
### Commit Convention
143
182
0 commit comments