stream-chat-react icon indicating copy to clipboard operation
stream-chat-react copied to clipboard

Expose mocks for testing

Open lr-mjaouen opened this issue 2 years ago • 5 comments

Motivation I may have miss it? but would it be possible to expose mocks for testing? Or what are your recommendation for testing? How do you bypass following warnings or furnish the providers (so far I mock component & hooks from gs to just render anything & do nothing),

The useChannelStateContext hook was called outside of the ChannelStateContext provider. Make sure this hook is called within a child of the Channel component. The errored call is located in the Window component.
The useChannelStateContext hook was called outside of the ChannelStateContext provider. Make sure this hook is called within a child of the Channel component. The errored call is located in the Window component.

Thank you.

Proposed solution channel mock builder exposed, Client / Channel components mocks? (like Apollo client for instance https://www.apollographql.com/docs/react/development-testing/testing/)

MessageList/VirtualMessageList mocked component?

Acceptance Criteria

lr-mjaouen avatar Mar 03 '23 20:03 lr-mjaouen

Hello @lr-mjaouen , we do not expose mocks. Could you provide an example, where you find yourself in need of such mocks, please?

MartinCupela avatar Mar 06 '23 22:03 MartinCupela

Hello, Mainly for testing purposes. (I remember in previous versions mocking the channel previously) Also for testing custom components (CustomInput, CustomMessage, CustomTimeSeparator etc). Exposing for instance aMessage({overrides}), aChannel({overrides}) would be useful and I saw you already have it? And maybe helpers too, like a context/Channel component already provided etc. Now moment I mock it myself (I'm not able to mock it properly anymore & pass it to de Channel component for instance), I just hard mock the components/hooks directly to avoid issues in my tests (hook usage without context warning etc).

vi.mock('stream-chat-react', async () => {
    const streamChatActual = await vi.importActual<typeof import('stream-chat-react')>('stream-chat-react');
    return {
        ...streamChatActual,
        MessageList: vi.fn(() => <></>),
        VirtualizedMessageList: vi.fn(() => <></>),
        Window: vi.fn(() => <></>),
        MessageInput: vi.fn(() => <></>),
        Channel: vi.fn(({ children }) => <>{children}</>),
        useChannelActionContext: vi.fn(),
    };
});

Not optimal. Could be useful to have default mocks (Messages, Channels, Attachements etc).

lr-mjaouen avatar Mar 10 '23 21:03 lr-mjaouen

@lr-mjaouen for now we do not plan to support maintenance of mocks. The situation may change in the future with priorities shifting. We will keep this as a feature request.

MartinCupela avatar Mar 13 '23 13:03 MartinCupela

I've run into this issue as well, it would really help us to be able to get access to these ready-made mocks rather than having to try and create them ourselves. The use case for us specifically is where we are wanting to test components that are overriding the defaults like a custom message input.

charliedavison avatar Aug 01 '23 13:08 charliedavison

💯 It's super hard to maintain mocks on our end. Each major upgrade is very painful because we always have to mock even more components/behaviours.

sailor avatar Jun 04 '25 12:06 sailor