Skip to content

Commit

Permalink
fix(types)(middleware/devtools): fix action type in devtools's setSta…
Browse files Browse the repository at this point in the history
…te (#1183)

* added payload to setState() in devtools middleware

* adjusted tests

* changed payload to any property name

* adjusted docs

* prettier readme.md

* adjusted for extends

* adjusted immer's setState

* Revert "adjusted immer's setState"

This reverts commit 50ce327.

* changed order of immer and devtools in tests

* added test for action with payload type in immer/devtools

* removed unnecessary test

* Update tests/middlewareTypes.test.tsx

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
  • Loading branch information
lucasrabiec and dai-shi authored Aug 18, 2022
1 parent f0b6023 commit ed12c7e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
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
6 changes: 3 additions & 3 deletions src/middleware/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type TakeTwo<T> = T extends []
type WithDevtools<S> = Write<Cast<S, object>, StoreDevtools<S>>

type StoreDevtools<S> = S extends {
setState: (...a: infer A) => infer Sr
setState: (...a: infer Sa) => infer Sr
}
? {
setState(
...a: [...a: TakeTwo<A>, actionType?: string | { type: unknown }]
setState<A extends string | { type: unknown }>(
...a: [...a: TakeTwo<Sa>, action?: A]
): 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
26 changes: 16 additions & 10 deletions tests/middlewareTypes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,24 @@ describe('counter state spec (double middleware)', () => {
__DEV__ = savedDEV
})

it('devtools & immer', () => {
it('immer & devtools', () => {
__DEV__ = false
const useBoundStore = create<CounterState>()(
devtools(
immer((set, get) => ({
count: 0,
inc: () =>
set((state) => {
state.count = get().count + 1
}),
})),
{ name: 'prefix' }
immer(
devtools(
(set, get) => ({
count: 0,
inc: () =>
set(
(state) => {
state.count = get().count + 1
},
false,
{ type: 'inc', by: 1 }
),
}),
{ name: 'prefix' }
)
)
)
const TestComponent = () => {
Expand Down

0 comments on commit ed12c7e

Please sign in to comment.