DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
        • types
          • Store
          • ajaxSettings
          • request
          • requestHandler
          • response
        • properties
          • delay
          • on
          • rand
          • store
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-fixture

  • npm package badge
  • Star
  • Edit on GitHub

can-fixture intercepts an AJAX request and simulates the response with a file or function.

fixture(ajaxSettings, requestHandler(...))

If an XHR request matches ajaxSettings, calls requestHandler with the XHR requests data. Makes the XHR request respond with the return value of requestHandler or the result of calling its response argument.

The following traps requests to GET /todos and responds with an array of data:

fixture({method: "get", url: "/todos"},
        function(request, response, headers, ajaxSettings){
    return {
        data: [
            {id: 1, name: "dishes"},
            {id: 2, name: "mow"}
        ]
    };
})

When adding a fixture, it will remove any identical fixtures from the list of fixtures. The last fixture added will be the first matched.

Parameters

  1. ajaxSettings {ajaxSettings}:

    An object that is used to match values on an XHR object, namely the url and method. url can be templated like /todos/{_id}.

  2. requestHandler {requestHandler(request, response, requestHeaders, ajaxSettings)}:

    Handles the request and provides a response. The next section details this function's use.

fixture(ajaxSettings, url)

Redirects the request to another url. This can be useful for simulating a response with a file.

fixture({url: "/tasks"}, "fixtures/tasks.json");

Placeholders available in the ajaxSettings url will be available in the redirect url:

fixture({url: "/tasks/{id}"}, "fixtures/tasks/{id}.json");

fixture(ajaxSettings, data)

Responds with the JSON.stringify result of data.

fixture({url: "/tasks"}, {tasks: [{id: 1, complete: false}]});

fixture(ajaxSettings, delay)

Delays the ajax request from being made for delay milliseconds.

fixture({url: "/tasks"}, 2000);

This doesn't simulate a response, but is useful for simulating slow connections.

fixture(ajaxSettings, null)

Removes the matching fixture from the list of fixtures.

fixture({url: "/tasks"}, "fixtures/tasks.json");

$.get("/tasks") // requests fixtures/tasks.json

fixture({url: "/tasks"}, null);

$.get("/tasks") // requests /tasks

fixture(methodAndUrl, url|data|requestHandler)

A short hand for creating an ajaxSettings with a method and url.

fixture("GET /tasks", requestHandler );

// is the same as

fixture({method: "get", url: "/tasks"}, requestHandler );

The format is METHOD URL.

fixture(url, url|data|requestHandler)

A short hand for creating an ajaxSettings with just a url.

fixture("/tasks", requestHandler);

// is the same as

fixture({url: "/tasks"}, requestHandler);

fixture(fixtures)

Create multiple fixtures at once.

fixture({
    "POST /tasks": function(){
        return {id: Math.random()}
    },
    "GET /tasks": {data: [{id: 1, name: "mow lawn"}]},
    "/people": "fixtures/people.json"
});

Parameters

  1. fixtures {Object<methodAndUrl,String|Object|requestHandler(request, response, requestHeaders, ajaxSettings)|Store()>}:

    A mapping of methodAndUrl to some response argument type.

fixture(restfulUrl, store)

Wire up a restful API scheme to a store.

var todoAlgebra = new set.Algebra(
    set.props.id("id")
);
var todoStore = fixture.store([
  { id: 1, name: 'Do the dishes'},
  { id: 2, name: 'Walk the dog'}
], todoAlgebra);

fixture("/api/todos/{id}", todoStore); // can also be written fixture("/api/todos", todoStore);

This is a shorthand for wiring up the todoStore as follows:

fixture({
    "GET /api/todos": todoStore.getListData,
    "GET /api/todos/{id}": todoStore.getData,
    "POST /api/todos": todoStore.createData,
    "PUT /api/todos/{id}": todoStore.updateData,
    "DELETE /api/todos/{id}": todoStore.destroyData
});

Parameters

  1. restfulUrl {String}:

    The url that may include a template for the place of the ID prop. The list url is assumed to be restfulUrl with the /{ID_PROP} part removed, if provided; otherwise the item url is assumed to have the /{ID_PROP} part appended to the end.

  2. store {Store()}:

    A store produced by store.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.14.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news