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

Mock RTK WebSockets #129

Closed
kasir-barati opened this issue Dec 4, 2023 · 4 comments
Closed

Mock RTK WebSockets #129

kasir-barati opened this issue Dec 4, 2023 · 4 comments

Comments

@kasir-barati
Copy link

kasir-barati commented Dec 4, 2023

Hi dear reader

I was looking at this lib and could not find any clue on how you should mock your WebSocket when you are using a library like Redux and SocketIO. Let's say you have a RTK query like this:

endpoints: (build) => ({
  someSubscription: build.query<ResponseSchema[], void>({
    queryFn: async () => {
      return { data: null }
    },
    async onCacheEntryAdded(_, { updateCachedData, cacheDataLoaded, cacheEntryRemoved }) {
      const socket = io('ws://localhost:3000/', {
        withCredentials: true,
        transports: ['websocket'],
      })
      socket.on("message", () => {/* cache received msg in */})
      socket.close()
    },
  }),
})

How can I mock the cached data? is it even possible to do it with this lib? if not with what I can do that?

@kasir-barati
Copy link
Author

I think we need to leverage and utilise two things at the same time, the first one I think is this addon and the other one might be: https://github.com/thoov/mock-socket

After a few weeks I ended up asking same question and landing back to this page again but not luck still 😭

@yannbf
Copy link
Collaborator

yannbf commented May 16, 2024

Hey there! MSW didn't support mocking web sockets before but it seems like it does now! If you're willing to make a contribution to this repo with a reasonable, simple websocket example, we can try to set mocks for it and it can serve as reference for others!

cc @kettanaito

@kettanaito
Copy link
Member

Second what @yannbf is saying here! We are bringing a first-class WebSocket support to MSW, which is already available in a public beta.

npm i msw@next

I've got a few usage examples as well, feel free to reference those.

@kettanaito
Copy link
Member

MSW supports mocking WebSockets since [email protected].

Minimal usage example:

import { ws } from 'msw'

const chat = ws.link('wss://api.example.com/chat')

export const YourStory = {
  parameters: {
    msw: {
      handlers: [
        chat.addEventListener('connection', ({ client }) => {
          client.send('Hello from mock!')
        })
      ]
    }
  }
}

@kettanaito kettanaito changed the title Do not know how I can mock my RTK websocket with this lib Mock RTK WebSockets Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants