Giter Site home page Giter Site logo

angular-multimocks's Introduction

Angular Multimocks

Travis Status

Build Status

Demo

http://nabil-boag.github.io/angular-multimocks/

Angular Multimocks lets you test how your app behaves with different responses from an API.

Angular Multimocks allows you to define sets of mock API responses for different scenarios as JSON files. A developer of an e-commerce app could set up scenarios for a new customer, one who is registered and one who has an order outstanding.

Angular Multimocks allows you to switch between scenarios using a query string parameter:

?scenario=foo

You can use Angular Multimocks to quickly test your app works in all situations while developing or to provide mock data for a suite of automated acceptance tests.

Example Use Case

You have an application which calls to http://example.com/cart to get a list of items in the customer's shopping cart. You'd like to be able to easily switch between different API responses so that you can test the various use cases. You may want responses for the following:

Scenario URL
Shopping cart is empty /cart?scenario=emptyCart
Shopping cart with a quick buy option /cart?scenario=quickBuyCart
Shopping cart with out of stock items /cart?scenario=outOfStockCart

Demo App

See app/src/demo/ for a demo app. Inside the demo app, run grunt to generate the mocks, then open index.html in your browser.

Usage

NPM

npm install --save angular-multimocks

Include angular-multimocks.js or angular-multimocks.min.js in your application:

<script src="node_modules/angular-multimocks/app/package/js/angular-multimocks.min.js"></script>

Dependencies

Angular Multimocks depends on Angular Mocks so include it in your application. For example:

<script src="node_modules/angular-mocks/angular-mocks.js"></script>

Add the scenario module to your application:

angular
  .module('demo', ['scenario'])
  // more code here...

Mock Format

Resource files look like this:

{
  "httpMethod": "GET",
  "statusCode": 200,
  "uri": "/customer/cart",
  "response": {
    "id": "foo"
  }
}

The uri property defines the URI that is being mocked in your application and can contain a regex:

"uri": "/customer/\\d*/cart"

Delayed responses

In some scenarios you may want to simulate a server/network delay. This is done by intercepting the HTTP response and delaying it. Mocks accept an optional responseDelay property that will delay the HTTP response for the specified time in milliseconds:

"responseDelay": 500

The manifest file mockResources.json defines the available scenarios and describes which version of each resource should be used for each scenario.

{
  "_default": [
    "root/_default.json",
    "account/anonymous.json",
    "orders/_default.json"
  ],
  "loggedIn": [
    "account/loggedIn.json"
  ]
}

All scenarios inherit resources defined in _default unless they provide an override. Think of _default as the base class for scenarios.

The example above defines 2 scenarios _default and loggedIn. loggedIn has the default versions of the root and orders resources, but overrides account, using the version in account/loggedIn.json.

Global delay override

You can override all delays in a request by adding an optional parameter to the query string.

global_delay=0

Generating Mocks

Angular Multimocks provides Grunt and Gulp tasks that will compile resources into an AngularJS module definition. Adding these tasks to your build process will help to generate mocks after making changes.

Install the module using npm:

npm install --save-dev angular-multimocks

Grunt task

Add it to your Grunt configuration:

// load the task
grunt.loadNpmTasks('angular-multimocks');

// configuration for scenarios
grunt.initConfig({
  multimocks: {
    myApp: {
      src: 'mocks',
      dest: 'build/multimocks.js',
      template: 'myTemplate.tpl' // optional
    }
  },
  // other config here...
});

Gulp task

// Load the gulp task
var multimocksGulp = require('angular-multimocks/app/package/tasks/gulp/multimocksGulp');

// Define multimocks
gulp.task('multimocks', function () {
  // Call the multimocks gulp task with apropriate configuration
  multimocksGulp({
    src: 'mocks',
    dest: 'mocks/multimocks.js'
  });
});

Once either the Gulp or Grunt task is run, build/multimocks.js will be generated containing all your mock data. Include that in your app:

<script src="build/multimocks.js"></script>

Output Scenarios In Multiple Files

If the generated build/multimocks.js is too large, you may experience memory issues when running your application.

You can choose to build multiple files, one for each scenario by specifying multipleFiles: true and dest as a directory.

Your Grunt configuration should look something like:

// load the task
grunt.loadNpmTasks('angular-multimocks');

// configuration for scenarios
multimocks: {
  myApp: {
    src: 'mocks',
    dest: 'build/multimocks',
    multipleFiles: true,
    template: 'myTemplate.tpl' // optional
  }
},

When the task is run a file will be generated for each scenario. Include all the generated files in your app:

<script src="build/scenarios/_default.js"></script>
<script src="build/scenarios/foo.js"></script>
<script src="build/scenarios/bar.js"></script>

Task options

  • src - The directory to load mock files from (required)
  • dest - The destination file/directory to output compiled mocks (required)
  • multipleFiles - Generates one file per resource type (default: false)
  • template - The template to use when generating mocks
  • verbose - The logging level to use when running the generate task

HAL Plugin

If your API conforms to HAL, Angular Multimocks can generate links for you to speed development.

Enable the plugin in your Gruntfile.js:

multimocks: {
  myApp: {
    src: 'mocks',
    dest: 'build/multimocks',
    plugins: ['hal']
  }
}

Organise your mock response files into a file structure with a directory for each resource, e.g.:

.
├── account
│   ├── loggedIn.json
│   └── anonymous.json
├── orders
│   └── _default.json
├── root
│   └── _default.json
└── mockResources.json

Angular Multimocks will add a _links object to each response with all the known resources declared as available links:

{
  "httpMethod": "GET",
  "statusCode": 200,
  "response": {
    "id": "foo",
    "_links": {
      "root": {
        "rel": "root",
        "method": "GET",
        "href": "http://example.com/"
      },
      "account": {
        "rel": "account",
        "method": "GET",
        "href": "http://example.com/account"
      },
      "orders": {
        "rel": "orders",
        "method": "GET",
        "href": "http://example.com/orders"
      }
    }
  }
}

A uri will be generated for each resource. This value is used for the href field of each object in _links.

multimocksDataProvider

Angular Multimocks also declares a provider, multimocksDataProvider, which allows you to set mock data by passing an object to the setMockData method.

multimocksDataProvider also gives you the ability to overwrite the default headers returned by Angular Multimocks. Below we're setting the headers to specify that the content type is HAL JSON.

.config(['mutimocksDataProvider', function (multimocksDataProvider) {
  multimocksDataProvider.setHeaders({
    'Content-Type': 'application/hal+json'
  });
}]);

Contributing

We ❤️ pull requests!

To contribute:

  • Fork the repo
  • Run npm install
  • Run grunt workflow:dev or npm run dev to watch for changes, lint, build and run tests as you're working
  • Write your unit tests for your change
  • Test with the demo app
  • Run grunt package or npm run package to update the distribution files

angular-multimocks's People

Contributors

afternoon avatar bnppl avatar chrisiconolly avatar douglaseggleton avatar edconolly avatar hbk619 avatar lukebarton avatar morrislaptop avatar nabil-boag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-multimocks's Issues

Easily disable API delayed responses

It's great being about to include API delays to get a feel for the app in the real world, but it's also a pain disabling them come test time. Why don't we have something that makes it easy to ignore the delayed responses. Either turn responseDelay into a provider and allow some configuration, or grab something from the URL perhaps /cart?scenario=emptyCart&responseDelay=false

Let scenarios be composable from other scenarios

It would be great to have scenarios not just be made up of resources but other scenarios as well.

Maybe something like

"emptyShoppingCart": [
    "extends": [
       "fooScenario",
       "barScenario"
    ]
    "cart/noItems.json"
  ],

0.6.8

Seem like when running grunt multimocks
grunt.initConfig({
multimocks: {
myApp: {
src: 'mocks',
dest: 'build/multimocks.js',
multipleFiles: false
}
},
// other config here...
});

It asumes your structure is multimocks/mocks also it gives you a warning saying "path must be a string" and then it exits... so it doesn't work to generate mocks, at all

Grunt task should take config option to enable HAL-specific functionality

The Grunt task currently assumes your API follows HAL patterns. This functionality is useful, but not appropriate to many projects.

The actual scenario data loader operates on URIs, not links, so shouldn't need any modification.

Provide demo app

It's currently difficult to see how everything fits together, and there's no way to develop a check my changes without bringing into another project.

Gulp File

Can we use multimocks with Gulpfile?

Make multimocks work with lodash v3

We have a requirement to use lodash v3, given the new dist folder structure for lodash this will currently break multimocks.

Instead of including

node_modules/lodash/lodash.js

We'll have to figure out a way to bundle our own, or list the files projects need to bring in.

Tests for Grunt task

  • Simple API
  • Output multiple files
  • HAL API
  • Resource defaults (HAL and simple APIs)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.