Mock Ajax (Reactjs + Mobx)

I want to make some fake my ajax calls, so when on localhost I can return dummy data from a fake method instead of real calls to my server (where the structure may be missing).

What is the best way to do this in responsejs + mobx?

I was thinking of creating a fake store, as these are all my actions with my ajax calls, but this will always include the creation of the entire store with all the functions.

Then I might extract the calls from the store from ajax, and then the layout of these calls, but I'm not sure if this is the best way.

+5
source share
5 answers

Use json server to fake api calls and shorten to manage state, I used this in one of my projects. It is quite simple and requires very little template code. Redux updates the global store through actions, and components can subscribe to save changes.

See documentation here

https://github.com/typicode/json-server

http://redux.js.org/

An example from my application- You can create a fake store by creating db.json , and then you can use either the default routes provided by json-sever, or create your own.

It could be your db.json

 { "questions": [ { "id": 2, "question": "A train running at the speed of 60 km/hr crosses a pole in 9 seconds. What is the length of the train?", "options": [ "120 metres", "180 metres", "324 metres", "150 metres" ], "answer": 4 }, { "id": 3, "question": "The length of the bridge, which a train 130 metres long and travelling at 45 km/hr can cross in 30 seconds, is", "options": [ "200 m", "225 m", "245 m", "250 m" ], "answer": 3 } ] } 

run this api layout by running this json-server --watch db.json in your db.json file folder.

Then use http: // localhost: 3000 / questions

Comment if you want to get more detailed information about any of the above materials. I can provide you with sample code since I implemented this full stream.

+1
source

Have you tried nock ?

Nock is an HTTP bullying and expectation library for Node.js

Nock can be used to test modules that execute HTTP requests in isolation.

For example, if a module makes HTTP requests to the CouchDB server or makes HTTP requests to the Amazon API, you can test this module in isolation.

0
source

It should not matter if you use it to test localhost dev or unit. The library should simply mock requests and not care about your environment.

The main idea to do this is to set up a check that says: "If I am on a local host and then scoff at all my requests." You can mock individual requests or simply mimic them as a single function, providing a regular expression as a host name or path.

https://github.com/node-nock/nock#specifying-hostname https://github.com/node-nock/nock#specifying-path

The library provided by @crystal will allow you to do this in 2 minutes of changing README.md.

It allows you to individually request a mockery and seems to allow all of the few requests mocking, for example. you can tell Regex to match all the strings for the host name or path and return a generic response for all of them if you want.

Use a mocking library, do not recreate it. It's also much better to use this HTTP forming library than to mock your store. What happens if you need to send a request outside your store? This will hit your server, which will defeat the whole goal.

0
source

Another alternative to the one already proposed is to quickly implement the node server, which runs locally (in development mode), and which responds to some dummy JSON data for each endpoint.

Thus, you keep your external code clean, regardless of the technology stack used, and delegate implementation details on your server. The only thing that is required in your interface (provided that you use a build tool like webpack or similar to bind your JavaScript) is to use environment variables to use the endpoints of your local or prod server.

This is usually done using this webpack plugin: https://webpack.js.org/plugins/environment-plugin/

 let apiEndpoint; if (process.env.NODE_ENV === 'production') { apiEndpoint = 'https://yourProdServer/api' } else { apiEndpoint = 'https://localhost:8080/api' } 

Then in your npm scripts configure something like:

 "dev": "webpack", "prod": "NODE_ENV=production webpack" 
0
source

Do you know superagent ?

This is a very interesting http client and has a very cool mocker plugin .

It works fine on a nodejs server or in a web browser. I use it in a daily base. Very easy and well documented.

You can create your own callbacks to process requests for specific URLs or URL patterns.

0
source

Source: https://habr.com/ru/post/1269263/


All Articles