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
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