Repository: pmndrs/zustand
File: tests/devtools.test.tsx (TypeScript)

Code before:
      type: 'testSetStateName',
      payload: 15,
    })
    expect(extension.send).toHaveBeenLastCalledWith(
      { type: 'testSetStateName', payload: 15 },
      { count: 15, foo: 'bar' }
    )
  })

  it("sends { type: 'anonymous' } as the action with current state", () => {
    const { setState } = createTestState()
    setState({ count: 5, foo: 'baz' }, true)
    expect(extension.send).toHaveBeenLastCalledWith(
      { type: 'anonymous' },
      { count: 5, foo: 'baz' }
    )
  })

  function createTestState() {
    return create(
      devtools(() => ({ count: 0, foo: 'bar' }), {
        name: 'testOptionsName',
        enabled: true,
      })
    )
  }
})

describe('when it receives an message of type...', () => {
  describe('ACTION...', () => {
    it('does nothing', () => {
      const initialState = { count: 0 }
      const api = create(devtools(() => initialState, { enabled: true }))
      const setState = jest.spyOn(api, 'setState')

      ;(extensionSubscriber as (message: any) => void)({
        type: 'ACTION',
        payload: '{ "type": "INCREMENT" }',
      })

      expect(api.getState()).toBe(initialState)
      expect(setState).not.toBeCalled()
    })

    it('unless action type is __setState', () => {
      const initialState = { count: 0 }
      const api = create(devtools(() => initialState, { enabled: true }))

      ;(extensionSubscriber as (message: any) => void)({
        type: 'ACTION',
        payload: '{ "type": "__setState", "state": { "foo": "bar" } }',

Review comment (refactor):
I think this refactor is a quite unncessary and non-minimal, the only change required should be this...

```diff
  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: 10 }, false, { type: 'testSetStateName', bar: 'foo' })
+     expect(extension.send).toHaveBeenLastCalledWith(
+       { type: 'testSetStateName', bar: 'foo' },
+       { count: 10, foo: 'bar' }
+     )
+
      api.setState({ count: 5, foo: 'baz' }, true)
      expect(extension.send).toHaveBeenLastCalledWith(
        { type: 'anonymous' },
        { count: 5, foo: 'baz' }
      )
    })
  })
```

Code after:
      { 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' },
      { count: 5, foo: 'baz' }
    )
  })
})

describe('when it receives an message of type...', () => {
  describe('ACTION...', () => {
    it('does nothing', () => {
      const initialState = { count: 0 }
      const api = create(devtools(() => initialState, { enabled: true }))
      const setState = jest.spyOn(api, 'setState')

      ;(extensionSubscriber as (message: any) => void)({
        type: 'ACTION',
        payload: '{ "type": "INCREMENT" }',
      })

      expect(api.getState()).toBe(initialState)
      expect(setState).not.toBeCalled()
    })

    it('unless action type is __setState', () => {
      const initialState = { count: 0 }
      const api = create(devtools(() => initialState, { enabled: true }))

      ;(extensionSubscriber as (message: any) => void)({
        type: 'ACTION',
        payload: '{ "type": "__setState", "state": { "foo": "bar" } }',
      })

      expect(api.getState()).toStrictEqual({ ...initialState, foo: 'bar' })
    })

    it('does nothing even if there is `api.dispatch`', () => {
      const initialState = { count: 0 }
      const api = create(devtools(() => initialState, { enabled: true }))