Skip to content

Commit 6d8630d

Browse files
committed
docs: refactor
1 parent 6589047 commit 6d8630d

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

packages/docs/cookbook/testing.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ expect(store.someAction).toHaveBeenCalledTimes(1)
168168

169169
### Selective action stubbing
170170

171-
Sometimes you may want to stub only specific actions while allowing others to execute normally. You can achieve this by passing an object with `include` or `exclude` arrays to the `stubActions` option:
171+
Sometimes you may want to stub only specific actions while allowing others to execute normally. You can achieve this by passing an array of action names to the `stubActions` option:
172172

173173
```js
174174
// Only stub the 'increment' and 'reset' actions
175175
const wrapper = mount(Counter, {
176176
global: {
177177
plugins: [
178178
createTestingPinia({
179-
stubActions: { include: ['increment', 'reset'] }
179+
stubActions: ['increment', 'reset']
180180
})
181181
],
182182
},
@@ -193,32 +193,40 @@ store.fetchData() // executed normally
193193
expect(store.fetchData).toHaveBeenCalledTimes(1)
194194
```
195195

196-
Alternatively, you can exclude specific actions from stubbing:
196+
For more complex scenarios, you can pass a function that receives the action name and store instance, and returns whether the action should be stubbed:
197197

198198
```js
199-
// Stub all actions except 'fetchData'
199+
// Stub actions based on custom logic
200200
const wrapper = mount(Counter, {
201201
global: {
202202
plugins: [
203203
createTestingPinia({
204-
stubActions: { exclude: ['fetchData'] }
204+
stubActions: (actionName, store) => {
205+
// Stub all actions that start with 'set'
206+
if (actionName.startsWith('set')) return true
207+
208+
// Stub actions based on initial store state
209+
if (store.isPremium) return false
210+
211+
return true
212+
}
205213
})
206214
],
207215
},
208216
})
209217

210218
const store = useSomeStore()
211219

212-
// This action will execute normally
213-
store.fetchData() // executed normally
220+
// Actions starting with 'set' are stubbed
221+
store.setValue(42) // stubbed
214222

215-
// Other actions will be stubbed
216-
store.increment() // stubbed
217-
store.reset() // stubbed
223+
// Other actions may execute based on the initial store state
224+
store.fetchData() // executed or stubbed based on initial store.isPremium
218225
```
219226

220227
::: tip
221-
If both `include` and `exclude` are provided, `include` takes precedence. If neither is provided or both arrays are empty, all actions will be stubbed (equivalent to `stubActions: true`).
228+
- An empty array `[]` means no actions will be stubbed (all actions execute normally)
229+
- The function is evaluated once at store setup time, receiving the store instance in its initial state
222230
:::
223231

224232
You can also manually mock specific actions after creating the store:

packages/docs/zh/cookbook/testing.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ expect(store.someAction).toHaveBeenCalledTimes(1)
175175

176176
### 选择性 action 存根 %{#selective-action-stubbing}%
177177

178-
有时你可能只想存根特定的 action,而让其他 action 正常执行。你可以通过向 `stubActions` 选项传递一个包含 `include``exclude` 数组的对象来实现
178+
有时你可能只想存根特定的 action,而让其他 action 正常执行。你可以通过向 `stubActions` 选项传递一个 action 名称数组来实现
179179

180180
```js
181181
// 只存根 'increment' 和 'reset' action
182182
const wrapper = mount(Counter, {
183183
global: {
184184
plugins: [
185185
createTestingPinia({
186-
stubActions: { include: ['increment', 'reset'] }
186+
stubActions: ['increment', 'reset']
187187
})
188188
],
189189
},
@@ -200,32 +200,40 @@ store.fetchData() // 正常执行
200200
expect(store.fetchData).toHaveBeenCalledTimes(1)
201201
```
202202

203-
或者,你可以排除特定的 action 不被存根
203+
对于更复杂的场景,你可以传递一个函数,该函数接收 action 名称和 store 实例,并返回是否应该存根该 action
204204

205205
```js
206-
// 存根所有 action 除了 'fetchData'
206+
// 基于自定义逻辑存根 action
207207
const wrapper = mount(Counter, {
208208
global: {
209209
plugins: [
210210
createTestingPinia({
211-
stubActions: { exclude: ['fetchData'] }
211+
stubActions: (actionName, store) => {
212+
// 存根所有以 'set' 开头的 action
213+
if (actionName.startsWith('set')) return true
214+
215+
// 根据初始 store 状态存根 action
216+
if (store.isPremium) return false
217+
218+
return true
219+
}
212220
})
213221
],
214222
},
215223
})
216224

217225
const store = useSomeStore()
218226

219-
// 这个 action 将正常执行
220-
store.fetchData() // 正常执行
227+
// 以 'set' 开头的 action 被存根
228+
store.setValue(42) // 存根
221229

222-
// 其他 action 将被存根
223-
store.increment() // 存根
224-
store.reset() // 存根
230+
// 其他 action 可能根据初始 store 状态执行
231+
store.fetchData() // 根据初始 store.isPremium 执行或存根
225232
```
226233

227234
::: tip
228-
如果同时提供了 `include``exclude``include` 优先。如果两者都没有提供或两个数组都为空,所有 action 都将被存根(等同于 `stubActions: true`)。
235+
- 空数组 `[]` 表示不存根任何 action(所有 action 正常执行)
236+
- 函数在 store 设置时被评估一次,接收处于初始状态的 store 实例
229237
:::
230238

231239
你也可以在创建 store 后手动模拟特定的 action:

0 commit comments

Comments
 (0)