https://github.com/nshimiye/react-mock
Combining the power of Pretenderjs and Fakerjs
https://github.com/nshimiye/react-mock
fakerjs mocking-library pretenderjs
Last synced: 5 months ago
JSON representation
Combining the power of Pretenderjs and Fakerjs
- Host: GitHub
- URL: https://github.com/nshimiye/react-mock
- Owner: nshimiye
- License: mit
- Created: 2018-01-30T17:01:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-02T18:16:24.000Z (over 5 years ago)
- Last Synced: 2025-03-18T13:09:54.373Z (10 months ago)
- Topics: fakerjs, mocking-library, pretenderjs
- Language: TypeScript
- Size: 9.13 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 29
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: code-of-conduct.md
Awesome Lists containing this project
README
# React Mock
[](https://github.com/prettier/prettier)
[](https://travis-ci.org/nshimiye/react-mock)
[](https://coveralls.io/github/nshimiye/react-mock?branch=master)
[](https://david-dm.org/nshimiye/react-mock?type=dev)
[](https://greenkeeper.io/)
[](https://paypal.me/nshimiyetech)
Mocking api calls from React Applications, using Pretenderjs and Fakerjs.
# Simple Example usage
```javascript
import { Server, Faker, uid } from 'react-mock'
const endPoint = '/api/v1/todos'
const todoSchema = {
author: Faker.internet.email(),
content: () => Faker.lorem.sentence(),
createdAt: () => Faker.date.past()
}
const requestHandler = (request, generator) => {
const todoList = generator.next(10, todoSchema);
return [200, { 'Content-Type': 'application/json' }, JSON.stringify(todoList)];
}
Server.mockGet(endPoint, requestHandler)
Server.on() // to start mocking /api/v1/todos API
```
```javascript
axios.get('/api/v1/todos').then(({ data }) => {
// data is an array of 10 todo objects
})
```
# Real use case
* Suppose you want to simulate the fetching of 10 guides
* You know the **API Endpoint**
* You know the **Format** of each guide object
* You know the **Format** of the Error response
```javascript
// /src/mock-server
import { Server, Faker, uid } from 'react-mock'
const apiRoute = '/api/v1/guides'
const requestHandler = (request, generator) => {
const guides = generator.next(10); // @returns { : schema1, : schema2 }
// const error = generator.error();
return [200, { 'Content-Type': 'application/json' }, JSON.stringify(guides)];
}
const schema = {
description: Faker.lorem.sentence(),
createdAt: Faker.date.past(),
favoredCount: Faker.random.number(),
isPublic: random.boolean(),
author: {
id: uid.next(),
name: Faker.name.findName(),
picture: Faker.internet.avatar()
}
};
const errorFormat = {
message: Faker.lorem.sentence()
}
Server.mockGet(apiRoute, requestHandler, schema, errorFormat)
Server.on() // @returns Promise that resolves with null or rejects with Error
// Server.off()
```