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
Jul 03, 2023 (3 months ago)
Pantelis
Pantelis
08:54 PMHey 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
Jason
08:57 PMtypesense-js uses
axios
, so you can use axios-mock-adapter
to mock responses.08:57
Jason
08:57 PMHere’s an example from the test suite: https://github.com/typesense/typesense-js/blob/d1dc2e1ce4aee5ca9175a2e0fe1ffff3480f78ae/test/Typesense/Collections.spec.js#L6
Pantelis
Pantelis
09:00 PMthanks 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
Jason
09:03 PMI think it will…
Pantelis
Pantelis
09:03 PMyou re the best! thanks
1
1
Typesense
Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI
Indexed 2779 threads (79% resolved)
Similar Threads
Issue with Typesense Adapter in React Search Box
Nikunj encountered an error with Typesense adapter, and Jason suggested using 'typesense-js' for building their custom UI.
10
7mo
Solved
Implementing Typesense in React without Typesense Instantsearch Adapter
Aravinda is trying to implement typesense in a React project without using Typesense Instantsearch Adapter. Jason suggests using the typesense-js library, referring to the docs for code samples.
3
12mo
Solved
Custom Hook with Typesense.js for Product Search
Jameshwart seeks a way to set a state before starting a search function in typesense.js. Jason suggests using hooks exposed by axios, mentioning possible GraphQL integration with Typesense.
12
5mo