Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types)(middleware/devtools): fix action type in devtools's setState #1183

Merged
merged 12 commits into from
Aug 18, 2022
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,18 @@ const createBearSlice = (set, get) => ({
})
```

You can also log action's type along with its payload:

```jsx
const createBearSlice = (set, get) => ({
addFishes: (count) =>
set((prev) => ({ fishes: prev.fishes + count }), false, {
type: 'bear/addFishes',
count,
}),
})
```

If an action type is not provided, it is defaulted to "anonymous". You can customize this default value by providing an `anonymousActionType` parameter:

```jsx
Expand Down
5 changes: 4 additions & 1 deletion src/middleware/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ type StoreDevtools<S> = S extends {
}
? {
setState(
...a: [...a: TakeTwo<A>, actionType?: string | { type: unknown }]
...a: [
...a: TakeTwo<A>,
action?: string | { type: unknown; [key: string]: unknown }
]
): Sr
}
: never
Expand Down
13 changes: 12 additions & 1 deletion tests/devtools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,29 @@ describe('If there is no extension installed...', () => {
})

describe('When state changes...', () => {
it("sends { type: setStateName || 'anonymous` } as the action with current state", () => {
it("sends { type: setStateName || 'anonymous`, ...rest } as the action with current state", () => {
const api = create(
devtools(() => ({ count: 0, foo: 'bar' }), {
name: 'testOptionsName',
enabled: true,
})
)

api.setState({ count: 10 }, false, 'testSetStateName')
expect(extension.send).toHaveBeenLastCalledWith(
{ type: 'testSetStateName' },
{ count: 10, foo: 'bar' }
)

api.setState({ count: 15 }, false, {
type: 'testSetStateName',
payload: 15,
})
expect(extension.send).toHaveBeenLastCalledWith(
{ type: 'testSetStateName', payload: 15 },
{ count: 15, foo: 'bar' }
)

api.setState({ count: 5, foo: 'baz' }, true)
expect(extension.send).toHaveBeenLastCalledWith(
{ type: 'anonymous' },
Expand Down