Nonstop Pet Mewing

    This package has been deprecated

    Author message:

    Please use xhr-mock and faker instead of this react-mock

    react-mock
    TypeScript icon, indicating that this package has built-in type declarations

    6.0.1 • Public • Published

    React Mock

    styled with prettier Build Status Coverage Status Dev Dependencies Greenkeeper badge Donate

    Mocking api calls from React Applications, using Pretenderjs and Fakerjs.

    Simple Example usage

    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
    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
    // <app-root>/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 { <id1>: schema1, <id2>: 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()

    Keywords

    none

    Install

    npm i react-mock

    DownloadsWeekly Downloads

    226

    Version

    6.0.1

    License

    MIT

    Unpacked Size

    9.44 MB

    Total Files

    98

    Last publish

    Collaborators

    • nshimiye