#community-help

Mocking Typesense-JS Requests in React Component Tests

TLDR Pantelis needed help with intercepting Typesense-JS requests to return stubbed data in a React component test. Jason suggested using axios-mock-adapter and provided an example.

Powered by Struct AI

1

1

6
3mo
Solved
Join the chat
Jul 03, 2023 (3 months ago)
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
08:54 PM
Hey there! I am using the typesense-js client to make requests and i want to test my react component with mock data. Are you aware of any way to intercept the typesense request through the client and return stubbed data? Maybe an example with jest?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:57 PM
typesense-js uses axios, so you can use axios-mock-adapter to mock responses.
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
09:00 PM
thanks for the answer. The api call though happens in my component which i render in my test using react-testing-library. Will it manage to intercept it?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:03 PM
I think it will…
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
09:03 PM
you re the best! thanks

1

1